#       mfp
#
#	module frame to pascal source
#
#       KEEPFLG         optional keep intermediate files flag
#       VDNVERSION      optional module version
#
	USAGE="usage: $0 [-f|-q|-s] [-notrace] [-k] source [includes]"
#

set -e

VDNVERSION=""
TRACEFLG=""
while :
do
	case "$1" in

	-f )
		VDNVERSION="f"
		shift
		;;
	-q )
		VDNVERSION="q"
		shift
		;;
	-s )
		VDNVERSION="s"
		shift
		;;
	-notrace )
		TRACEFLG="-notrace"
		shift
		;;
	-k )
		KEEPFLG="-k"
		shift
		;;
	-* )
		echo "$USAGE"
		exit 1
		;;
	* )
		break
		;;
	esac
done
case "$VDNVERSION" in

f )
	DEBUGFLG=""
	;;
q )
	DEBUGFLG=""
	;;
s )
	DEBUGFLG="-t"
	;;
* )
	echo "$USAGE"
	echo "please specify VDNVERSION -f (fast) or -q (quick) or -s (slow)"
	exit 1
	;;
esac
if [ $# -lt 1 ]
then
	echo "$USAGE"
	exit 1
fi
SOURCE="$1"; shift
BASE=`/bin/basename $SOURCE`

CONS=""
TYPS=""
INCLUDES=""
for ARG
do
	case "$ARG" in

	*.con )
		CONS="$CONS $ARG"
		;;
	*.typ )
		TYPS="$TYPS $ARG"
		;;
	* )
		INCLUDES="$INCLUDES $ARG"
		;;
	esac
done

PCOPTS=""
PCTYPS="$BASE.ptp"
echo "longint=integer;"		>  $PCTYPS
echo "shortint=-32768..32767;"	>> $PCTYPS
echo "double=real;"		>> $PCTYPS



echo "$BASE"

mfspl $DEBUGFLG $SOURCE   $BASE.def $BASE.use $BASE.cod

mfdef $DEBUGFLG $BASE.def $BASE.glv $BASE.glp
mfuse $DEBUGFLG $BASE.use $BASE.exv $BASE.exp
mfcod $DEBUGFLG $BASE.cod $BASE.con $BASE.typ $BASE.var $BASE.stm

mfglp $DEBUGFLG $BASE.glp $BASE.gpg $BASE.gpc
mfexp $DEBUGFLG $BASE.exp $BASE.epc 'EXTERN'
mfstm $DEBUGFLG $BASE.stm $BASE.fwd $BASE.pra

mfchk           $BASE.gpc $BASE.fwd

if [ \( "$VDNVERSION" = "s" \) -a \( "$TRACEFLG" != "-notrace" \) ]
then
	if /usr/bin/fgrep 't15print' $BASE.gpc $BASE.epc > /dev/null
	then    :
	else    echo 'PROCEDURE t15print(tt:tracetext);EXTERN;' >> $BASE.epc
	fi

	mftrc $DEBUGFLG $BASE.pra $BASE.prb 't15print' $BASE 16
else
	/bin/mv $BASE.pra $BASE.prb
fi

mftop $DEBUGFLG $BASE.gpg "" $BASE.top $BASE.bot

mfcat $BASE.cns 'CONST' $CONS $BASE.con
mfcat $BASE.tps 'TYPE' $PCTYPS $TYPS $BASE.typ
mfcat $BASE.vrs 'VAR' $BASE.exv $BASE.glv $BASE.var

/bin/cat	$PCOPTS $BASE.top $INCLUDES $BASE.cns $BASE.tps $BASE.vrs \
		$BASE.epc $BASE.fwd $BASE.prb $BASE.bot > $BASE.p



if [ "$KEEPFLG" = "" ]
then
	/bin/rm -f      $BASE.ptp $BASE.def $BASE.use $BASE.cod \
			$BASE.glv $BASE.glp $BASE.exv $BASE.xxv $BASE.exp \
			$BASE.con $BASE.typ $BASE.var $BASE.stm \
			$BASE.gpg $BASE.gpc $BASE.epc $BASE.fwd \
			$BASE.pra $BASE.prb $BASE.prc \
			$BASE.top $BASE.bot \
			$BASE.cns $BASE.tps $BASE.vrs 
fi

