#!/bin/sh

panc_show_help=false
panc_show_version=false
memory=false
javaopts=false
launch_panc=false
readconf=true
scanoptions=false
meminconf=false
joinconf=false

i=1
j=0
nmem=0
njopts=0
narg=0
argeq="nok"
argeqjo="nok"
javaexe="java"

if [ "`echo $JAVA_HOME`" != "" ] ; then
	javaexe=$JAVA_HOME"/bin/java"
	if [ ! -x $javaexe ] ; then
		echo " ERROR: cannot find executable for java"
		exit 1;
	fi
else
	java -version>/dev/null 2>&1
	if [ $? -ne 0 ] ; then
		echo " ERROR: cannot find executable for java"
		exit 1;
	fi
fi

if [ -z $1 ] ; then
	exit 0
elif [ "$1" = "--version" -o "$1" = "-v" ] ; then
	panc_show_version=true
else
	launch_panc=true
fi

if [ "$1" = "--memory" -o "$1" = "-M" ] ; then
	memory=true
fi
if [ "$1" = "--java-opts" ] ; then
	javaopts=true
fi
if [ "$1" = "--noconf" -o "$1" = "-k" ] ; then
	readconf=false
fi

for arg in "$@"
do
	if [ "$arg" = "--memory" -o "$arg" = "-M" ] ; then
		nmem=$i
		memory=true
	elif [ "`echo $arg | grep '\-\-memory='`" != "" ] ; then
		memory=true
		argeq=$arg
	elif [ "$arg" = "--java-opts" ] ; then
		njopts=$i
		javaopts=true
	elif [ "`echo $arg | grep '\-\-java\-opts='`" != "" ] ; then
		javaopts=true
		argeqjo=$arg
	elif [ "$arg" = "--noconf" -o "$arg" = "-k" ] ; then
		readconf=false
	fi
	tab[i]="$arg"
	if [ $i -lt $# ] ; then
		i=$(($i+1))
	fi
done

if $panc_show_version ; then
    panc_jar_path="`dirname $0`/../lib/panc.jar";
	compiler_class="org.quattor.pan.Compiler"
	if [ ! -r $panc_jar_path ]; then
	    echo "ERROR: cannot find pan compiler jar file; tried " $panc_jar_path
	    exit 1;
	fi;
	$javaexe -cp $panc_jar_path $compiler_class "$@"
elif $launch_panc ; then
    panc_jar_path="`dirname $0`/../lib/panc.jar";
	panc_class="org.quattor.pan.Panc"
	if [ ! -r $panc_jar_path ]; then
	    echo "ERROR: cannot find pan compiler jar file; tried " $panc_jar_path
	    exit 1;
	fi;
	if $readconf ; then
		if [ -e ~/.panc.conf ] ; then
			. ~/.panc.conf
			scanoptions=true
			echo "using conf file ~/.panc.conf"
		elif [ -e /etc/panc.conf ] ; then
			. /etc/panc.conf
			scanoptions=true
			echo "using conf file /etc/panc.conf"
		fi
	fi
	if $scanoptions ; then
		if [ -n "$MEMORY" ] ; then
			if [ $memory == false ] ; then
				memoryvalue="-Xmx"$MEMORY"M"
				meminconf=true
			fi
		fi
		if [ -n "$JAVAOPTS" ] ; then
			if [ $javaopts == false ] ; then
				jovalue=$JAVAOPTS
				joinconf=true
			fi
		fi
		if [ -n "$DEBUG" ] ; then
			if [ $DEBUG = 1 ] ; then
				optdebug="--debug"
				options[j]=$optdebug
				j=$(($j+1))
			fi
		fi
		if [ -n "$VERBOSE" ] ; then
			if [ $VERBOSE = 1 ] ; then
				optverbose="--verbose"
				options[j]=$optverbose
				j=$(($j+1))
			fi
		fi
		if [ -n "$OBJECT_LOAD" ] ; then
			if [ $OBJECT_LOAD = 1 ] ; then
				optobjectload="--object-load"
			else
				 optobjectload="--no-object-load"
			fi
			options[j]=$optobjectload
			j=$(($j+1))
		fi
		if [ -n "$OBJECT_WRITE" ] ; then
			if [ $OBJECT_WRITE = 1 ] ; then
				optobjectwrite="--object-write"
			else
				 optobjectwrite="--no-object-write"
			fi
			options[j]=$optobjectwrite
			j=$(($j+1))
		fi
		if [ -n "$XML_WRITE" ] ; then
			if [ $XML_WRITE = 1 ] ; then
				optxmlwrite="--xml-write"
			else
				 optxmlwrite="--no-xml-write"
			fi
			options[j]=$optxmlwrite
			j=$(($j+1))
		fi
		if [ -n "$OBJECTS" ] ; then
			optobjects="--objects="$OBJECTS
			options[j]=$optobjects
			j=$(($j+1))
		fi
		if [ -n "$OBJECTS_FILE" ] ; then
			optobjectsfile="--objects-file="$OBJECTS_FILE
			options[j]=$optobjectsfile
			j=$(($j+1))
		fi
		if [ -n "$FILE" ] ; then
			optfile="--file="$FILE
			options[j]=$optfile
			j=$(($j+1))
		fi
		if [ -n "$CHECK" ] ; then
			if [ $CHECK = 1 ] ; then
				optcheck="--check"
				options[j]=$optcheck
				j=$(($j+1))
			fi
		fi
		if [ -n "$SESSION_DIR" ] ; then
			optsessiondir="--session-dir="$SESSION_DIR
			options[j]=$optsessiondir
			j=$(($j+1))
		fi
		if [ -n "$INCLUDE_DIR" ] ; then
			optincludedir="--include-dir="$INCLUDE_DIR
			options[j]=$optincludedir
			j=$(($j+1))
		fi
		if [ -n "$OUTPUT_DIR" ] ; then
			optoutputdir="--output-dir="$OUTPUT_DIR
			options[j]=$optoutputdir
			j=$(($j+1))
		fi
		if [ -n "$XML_STYLE" ] ; then
			optxmlstyle="--xml-style="$XML_STYLE
			options[j]=$optxmlstyle
			j=$(($j+1))
		fi
		if [ -n "$DEPENDENCY" ] ; then
			if [ $DEPENDENCY = 1 ] ; then
				optdep="--dependency"
				options[j]=$optdep
				j=$(($j+1))
			fi
		fi
		if [ -n "$MAX_ITERATION" ] ; then
			optmaxit="--max-iteration="$MAX_ITERATION
			options[j]=$optmaxit
			j=$(($j+1))
		fi
		if [ -n "$MAX_RECURSION" ] ; then
			optmaxrec="--max-recursion="$MAX_RECURSION
			options[j]=$optmaxrec
			j=$(($j+1))
		fi
		if [ -n "$GZIP" ] ; then
			if [ $GZIP = 1 ] ; then
				optgzip="--gzip"
				options[j]=$optgzip
				j=$(($j+1))
			fi
		fi
		if [ -n "$DEPRECATION" ] ; then
			optdepr="--deprecation="$DEPRECATION
			options[j]=$optdepr
			j=$(($j+1))
		fi
		
		for arg in "$@"
		do
			options[j]=$arg
			j=$(($j+1))
		done
		if $meminconf ; then
			if [ $joinconf != true -a $javaopts != true ] ; then
				$javaexe -cp $panc_jar_path $memoryvalue $panc_class "${options[@]}"
				if [ $? -ne 0 ] ; then
					exit -1
				fi
			fi
		fi
		if $joinconf ; then
			if $meminconf ; then
				$javaexe -cp $panc_jar_path $memoryvalue $jovalue $panc_class "${options[@]}"
				if [ $? -ne 0 ] ; then
					exit -1
				fi
			elif [ $memory != true ] ; then
				$javaexe -cp $panc_jar_path $jovalue $panc_class "${options[@]}"
				if [ $? -ne 0 ] ; then
					exit -1
				fi
			fi
		fi
		if [ $memory=true -o $javaopts=true ] ; then
			if $memory ; then
				if [ $nmem -eq $# ] ; then
					echo ' Missing argument after '${tab[$i]}
					$javaexe -cp $panc_jar_path $panc_class "-h"
					exit -1
				elif [ $nmem != 0 ] ; then
					narg=$(($nmem+1))
					memoryvalue="-Xmx"${tab[$narg]}"M"
					if [ $javaopts != true ] ; then
						if [ $joinconf ] ; then
							$javaexe -cp $panc_jar_path $memoryvalue $jovalue $panc_class "${options[@]}"
							if [ $? -ne 0 ] ; then
								exit -1
							fi
						else
							$javaexe -cp $panc_jar_path $memoryvalue $panc_class "${options[@]}"
							if [ $? -ne 0 ] ; then
								exit -1
							fi
						fi
					fi
				else
					IFS_old=$IFS
					IFS="="
					k=0
					for memval in $argeq
					do
						k=$(($k+1))
					done
					if [ $k -ne 2 ] ; then
						echo ' Bad argument after --memory= '
						$javaexe -cp $panc_jar_path $panc_class "-h"
						exit -1
					else
						memoryvalue="-Xmx"$memval"M"
						if [ $javaopts != true ] ; then
							if [ $joinconf ] ; then
								$javaexe -cp $panc_jar_path $memoryvalue $jovalue $panc_class "${options[@]}"
								if [ $? -ne 0 ] ; then
									exit -1
								fi
							else
								$javaexe -cp $panc_jar_path $memoryvalue $panc_class "${options[@]}"
								if [ $? -ne 0 ] ; then
									exit -1
								fi
							fi
						fi
					fi
					IFS=$IFS_old
				fi
			fi
			if $javaopts ; then
				if [ $njopts -eq $# ] ; then
					echo ' Missing argument after '${tab[$i]}
					$javaexe -cp $panc_jar_path $panc_class "-h"
					exit -1
				elif [ $njopts != 0 ] ; then
					narg=$(($njopts+1))
					jovalue=${tab[$narg]}
					if [ $memory=true -o $meminconf=true ] ; then
						$javaexe -cp $panc_jar_path $memoryvalue $jovalue $panc_class "${options[@]}"
						if [ $? -ne 0 ] ; then
							exit -1
						fi
					else
						$javaexe -cp $panc_jar_path $jovalue $panc_class "${options[@]}"
						if [ $? -ne 0 ] ; then
							exit -1
						fi
					fi
				else
					IFS_old=$IFS
					IFS="="
					k=0
					for joval in $argeqjo
					do
						k=$(($k+1))
					done
					if [ $k -ne 2 ] ; then
						echo ' Bad argument after --java-opts= '
						$javaexe -cp $panc_jar_path $panc_class "-h"
						exit -1
					else
						jovalue=$joval
					if [ $memory=true -o $meminconf=true ] ; then
						$javaexe -cp $panc_jar_path $memoryvalue $jovalue $panc_class "${options[@]}"
						if [ $? -ne 0 ] ; then
							exit -1
						fi
					else
						$javaexe -cp $panc_jar_path $jovalue $panc_class "${options[@]}"
						if [ $? -ne 0 ] ; then
							exit -1
						fi
					fi
					fi
					IFS=$IFS_old
				fi
			fi
		else
			$javaexe -cp $panc_jar_path $panc_class "${options[@]}";
				if [ $? -ne 0 ] ; then
					exit -1
				fi
		fi
	else
		for arg in "$@"
		do
			if [ "$arg" != "--noconf" -a "$arg" != "-k" ] ; then
				options[j]=$arg
				j=$(($j+1))
			fi
		done
		if $memory ; then
			if [ $nmem -eq $# ] ; then
				echo ' Missing argument after '${tab[$i]}
				$javaexe -cp $panc_jar_path $panc_class "-h"
				exit -1
			elif [ $nmem != 0 ] ; then
				narg=$(($nmem+1))
				memoryvalue="-Xmx"${tab[$narg]}"M"
				if [ $javaopts != true ] ; then
					$javaexe -cp $panc_jar_path $memoryvalue $panc_class "${options[@]}"
					if [ $? -ne 0 ] ; then
						exit -1
					fi
				fi
			else
				IFS_old=$IFS
				IFS="="
				k=0
				for memval in $argeq
				do
					k=$(($k+1))
				done
				if [ $k -ne 2 ] ; then
					echo ' Bad argument after --memory= '
					$javaexe -cp $panc_jar_path $panc_class "-h"
					exit -1
				else
					memoryvalue="-Xmx"$memval"M"
					if [ $javaopts != true ] ; then
						$javaexe -cp $panc_jar_path $memoryvalue $panc_class "${options[@]}"
						if [ $? -ne 0 ] ; then
							exit -1
						fi
					fi
				fi
				IFS=$IFS_old
			fi
		fi
		if $javaopts ; then
			if [ $njopts -eq $# ] ; then
				echo ' Missing argument after '${tab[$i]}
				$javaexe -cp $panc_jar_path $panc_class "-h"
				exit -1
			elif [ $njopts != 0 ] ; then
				narg=$(($njopts+1))
				jovalue=${tab[$narg]}
				if $memory ; then
					$javaexe -cp $panc_jar_path $memoryvalue $jovalue $panc_class "${options[@]}"
					if [ $? -ne 0 ] ; then
						exit -1
					fi
				else
					$javaexe -cp $panc_jar_path $jovalue $panc_class "${options[@]}"
					if [ $? -ne 0 ] ; then
						exit -1
					fi
				fi
			else
				IFS_old=$IFS
				IFS="="
				k=0
				for joval in $argeqjo
				do
					k=$(($k+1))
				done
				if [ $k -ne 2 ] ; then
					echo ' Bad argument after --java-opts= '
					$javaexe -cp $panc_jar_path $panc_class "-h"
					exit -1
				else
					jovalue=$joval
					if $memory ; then
						$javaexe -cp $panc_jar_path $memoryvalue $jovalue $panc_class "${options[@]}"
						if [ $? -ne 0 ] ; then
							exit -1
						fi
					else
						$javaexe -cp $panc_jar_path $jovalue $panc_class "${options[@]}"
						if [ $? -ne 0 ] ; then
							exit -1
						fi
					fi
				fi
				IFS=$IFS_old
			fi
		else
			if [ $memory != true ] ; then
				$javaexe -cp $panc_jar_path $panc_class "${options[@]}";
				if [ $? -ne 0 ] ; then
					exit -1
				fi
			fi
		fi
	fi
fi