#!/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 2 ] 
then
	echo "CROHMELib *Batch* Label Graph  (.lg) to CROHME .inkml Converter"
	echo "Copyright (c) R. Zanibbi, H. Mouchre, 2012-2013"
	echo ""
	echo "Usage: convertLgCrohme <lg_dir> <inkml_dir>"
	echo ""
	echo "Converts all .lg files in <lg_dir> for CROHME .inkml files"
	echo "  in <inkml_dir> to CROHME inkml file with structure as given"
	echo "  in the .lg files to <lg_dir>_merged. Also produces"
	echo "  '<lg_dir>_merged/file.lg' normalized label graph files"
	echo "  from the generated .inkml files."
	echo ""
	echo "The corresponding .lg and .inkml files must have the same"
	echo "prefix."
	echo ""
	echo "Note: Error messages from failed conversions are written to"
	echo "  ConvertLgCrohmeErrors-<lg_dir>."
	exit 0
fi

BNAME=`basename $1`
CONVDIR=${BNAME}_merged

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


echo "Converted files are being written to $CONVDIR."

# Move to 'conversion' directory, and convert each file.
rm -f ConvertLgCrohmeErrors-$1
for file in $1/*.lg
do
	FILEBASE=`basename $file .lg`
	# Don't reprocess already converted files.
	if ! [ -e $CONVDIR/${FILEBASE}.inkml ]
	then

		#echo "Merging: $file and $2/$FILEBASE.inkml"
		$CROHMELibDir/bin/mergeLgCrohme $file $2/$FILEBASE.inkml 2> conv_temp
		X=`cat conv_temp`

		# If non-empty, record that the file conversion failed,
		# and delete the output file.
		if [ -n "$X" ]
		then
			echo "$X"
			echo ">> Label Graph to CROHME CONVERSION ERROR: $file" >> ConvertLgCrohmeErrors-$1
			echo "$X" >> ConvertLgCrohmeErrors-$1
			echo "" >> ConvertLgCrohmeErrors-$1
		fi	
	
		# DO NOT remove the output file - obtain partial results where
		# possible.
		#	rm -f ${FILEBASE}_crohme.lg ${FILEBASE}_out.inkml
	
		mv ${FILEBASE}_out.inkml $CONVDIR/${FILEBASE}.inkml
		# In mass conversion, remove '_crohme' suffix.
		mv ${FILEBASE}_crohme.lg $CONVDIR/${FILEBASE}.lg
	fi
done

echo "done."
rm -f conv_temp
