
#
# Get prototyped functions.  These are used by the jam5conv.awk script to
# decide how to arrange the arguments and their formatting.
#
WORKDIR=.
JPLSRCFILES=NONE
FORMSRCFILES=NONE
FUNCFILE=./funclist.c
JPLFINDIR=./jplconv
FORMFINDIR=./formconv
ERRORFILE=jam5conv.err
PARMERROR=0
JPLEXT=jpl
JAMEXT=jam
ERRORMESS=""
if [ $# -lt 2 ]
then
	PARMERROR=1
fi 
while [ $# -gt 1 ] 
do
	if [ $1 = "-j" ]
	then
		JPLSRCFILES=$2
	else if [ $1 = "-f" ]
	then
		FORMSRCFILES=$2
	else if [ $1 = "-e" ]
	then
		JPLEXT=$2
	else if [ $1 = "-E" ]
	then
		JAMEXT=$2
	else if [ $1 = "-w" ]
	then
		WORKDIR=$2
	else if [ $1 = "-c" ]
	then
		FUNCFILE=$2
	else if [ $1 = "-d" ]
	then
		JPLFINDIR=$2
	else if [ $1 = "-D" ]
	then
		FORMFINDIR=$2
	else
		ERRORMESS="illegal flag  $1"
		PARMERROR=1
	     fi
	     fi
	    fi
	    fi
	  fi
	  fi
	fi
	fi
	shift 
	shift
done
if [ $# -eq 1 ]
then
	ERRORMESS="Invalid number of parameters."
	PARMERROR=1
fi
if [ $PARMERROR -gt 0 ]
then
	echo "JAM5CONV 1.2 - Converts code in JAM screens and jpl files to JAM 7 syntax"
	echo "Note-this utility is NOT officially supported by JYACC!"
	echo 
	echo "Usage: jam5conv [-j <JPL file directory>] [-e <JPL file extension>]"
	echo "		[-f <JAM form directory>] [-E <JAM form extension>]"
	echo "		[-d <directory for converted jpl files>]"
	echo " 		[-D <directory for converted JAM forms>]"
	echo "		[-c <name of C funclist file>] [-w <work directory>]"
	echo
	echo "-j	directory containing jpl files"
	echo "-e	jpl file extension (default is jpl)"
	echo "-d	directory to put converted jpl files"
	echo "-f	directory containing JAM screens"
	echo "-E	JAM form extension (default is jam)"
	echo "-D	directory to put converted jam files"
	echo "-c	C file (default is ./funclist.c)"
	echo "-w	Work directory (default is .)"
	echo
	echo "Example 1: jam5conv -j jpldir -d newjpldir"
	echo "  This would convert all jpl code in directory jpldir and put the new code in directory newjpldir."
	echo
	echo $ERRORMESS
	echo
	exit
fi

echo "Extracting functions from $FUNCFILE ..."

awk -f $0.aw3 $FUNCFILE > $WORKDIR/oldfuncs.2
# strip SM_INTFNC("
sed s/SM.INTFNC\(\"//g $WORKDIR/oldfuncs.2 > $WORKDIR/oldfuncs.3    
# strip )"....
sed s/\)\ *\".*/\ /g $WORKDIR/oldfuncs.3 > $WORKDIR/oldfuncs.4    
# strip *
sed s/\*//g $WORKDIR/oldfuncs.4 > $WORKDIR/oldfuncs.5 
# strip /
sed sd/ddg $WORKDIR/oldfuncs.5 > $WORKDIR/oldfuncs.6
# strip , 
sed s/,/\ /g $WORKDIR/oldfuncs.6 > $WORKDIR/oldfuncs.7
# strip (
sed s/\(/\ /g $WORKDIR/oldfuncs.7 > $WORKDIR/oldfuncs.8
cd $WORKDIR
if [ $JPLSRCFILES != NONE ]
then
	echo "Looking for jpl files..."
	if [ `ls $JPLSRCFILES/*.$JPLEXT | wc -w` -gt 0 ]
	then
		echo Converting to JAM 6 syntax jpl files $JPLSRCFILES ...
		cp $JPLSRCFILES/*.$JPLEXT .
		for F in ./*.$JPLEXT 
		do
			echo $F 
			echo -- prettying code...
			nawk -f $0.aw4 $F > $WORKDIR/new.jpl
			mv $WORKDIR/new.jpl $F
			echo -- converting C and JPL calls... 
			nawk -f $0.awk $F > $WORKDIR/new.jpl  
			if (test -s $WORKDIR/new.jpl)
			then
				mv $WORKDIR/new.jpl $WORKDIR/$F
			else
				echo ERROR replacing C and JPL calls in file $F !
		echo ERROR replacing C and JPL calls in file $F ! >> $ERRORFILE
			fi
			echo -- removing cat, retvar statements ...
			nawk -f $0.aw2 $WORKDIR/$F > $WORKDIR/new.jpl
			if (test -s $WORKDIR/new.jpl)
			then
				mv $WORKDIR/new.jpl $JPLFINDIR/$F
			else
				echo ERROR removing retvar or cat in jpl file $F !
				echo ERROR removing retvar or cat in jpl file $F ! >> $ERRORFILE
			fi
			rm $F
		done
		echo
		echo "Done converting jpl files."
		echo
	else
		echo "No jpl files found."
	fi
fi
if [ $FORMSRCFILES != NONE ]
then
	echo "Looking for JAM screens..."
	if [`ls $FORMSRCFILES/*.$JAMEXT | wc -w` -gt 0 ]
	then
		echo Converting screens ...
		if (test -s $WORKDIR/new.asc)
		then
			rm $WORKDIR/new.asc
		fi
		if (test -s $WORKDIR/new2.asc)
		then
			rm $WORKDIR/new2.asc
		fi
		cp $FORMSRCFILES/*.$JAMEXT .
		for F in ./*.$JAMEXT
		do
			echo $F 
			echo -- converting C and JPL routine calls ...
			$SMBASE/util/f2asc -af $WORKDIR/test.asc $F
			nawk -f $0.awk $WORKDIR/test.asc > $WORKDIR/new.asc  
			if (test -s $WORKDIR/new.asc)
			then
				echo -- removing retvar and cat statements ...
				nawk -f $0.aw2 $WORKDIR/new.asc > $WORKDIR/new2.asc
cp $WORKDIR/new2.asc $WORKDIR/$F.new.asc
				if (test -s $WORKDIR/new2.asc)
				then
					$SMBASE/util/f2asc -bf $WORKDIR/new2.asc 
					mv $F $FORMFINDIR
				else
					echo ERROR replacing retvars and calls in screen $F
			echo ERROR replacing retvars and calls in screen $F >> $ERRORFILE
				fi
			else
				echo ERROR converting C and JPL routine calls in screen $F
				echo ERROR converting C and JPL routine calls in screen $F >> $ERRORFILE
				rm $F
			fi
			rm test.asc
			rm new.asc
			rm new2.asc
		done
		echo
		echo "Done converting JAM screens."
		echo
	else
		echo "No JAM screens found."
	fi
fi
rm oldfuncs.*
echo "Done with the jam5conv utility."
echo
if [ $JPLSRCFILES != NONE ]
then
	echo "All of your converted jpl files are in $JPLFINDIR."
fi
if [ $FORMSRCFILES != NONE ]
then
	echo "All of your converted JAM screens are in $FORMFINDIR."
fi
echo 
