#!/bin/bash

# NOTE:
#
# Make sure that CROHMELibDir and LgEvalDir are defined in your shell
# enviroment, e.g. by including:
#	
#	export CROHMELibDir=<path_to_CROHMELib> 
#	export LgEvalDir=<path_to_LgEval>
#	export PATH=$PATH:$CROHMELibDir/bin:$LgEvalDir/bin
# 
# in your .bashrc file for bash shell.

if [ $# -lt 1 ]
then
	echo "CROHMELib *Batch* CROHME .inkml to Label Graph (.lg) Converter"
	echo "Copyright (c) R. Zanibbi, H. Mouchere, 2012-2013"
	echo ""
	echo "Usage: convertCrohmeLg <dir> [<lgdir>]"
	echo ""
	echo "Converts a directory of CROHME .inkml files to .lg files."
	echo "  .lg files are placed in <dir> or in <lgdir> if specified."
	echo ""
	echo "Note: Error messages from failed conversions are written to"
	echo "  ConvertCrohmeLgErrors-<dir>."

	exit 0
fi

rm -f ConvertCrohmeLgErrors-$1

OUTDIR=$1
if [ $# -gt 1 ]
then
	OUTDIR=$2

	if ! [ -d $OUTDIR ] 
	then
		mkdir $OUTDIR
	fi
fi

for file in $1/*.inkml
do
	echo "Converting: $file"
	BNAME=`basename $file .inkml`
	OUT="$OUTDIR/$BNAME.lg"
	if [ $OUT -ot $file ]
	then
		perl $CROHMELibDir/bin/crohme2lg.pl $file 2> conv_temp$$ > temp$$.lg
		X=`cat conv_temp$$`

		# If not empty, record that this file conversion failed, and
		# delete the output file.
		if [ -n "$X" ]
		then
			# Send error message to standard error, store in log file.
			echo "  ERROR: $X" >&2
			echo ">> Crohme to Label Graph CONVERSION ERROR: $file" >> ConvertCrohmeLgErrors-$1
			echo "$X" >> ConvertCrohmeLgErrors-$1
			
			# Keep the file - let the evaluation tools find missing symbols etc.
			#rm -f $OUT
		fi
		mv temp$$.lg $OUT
	else
		echo "  Already converted."
	fi
done
echo "done."
rm -f conv_temp$$ temp$$.lg

