#! /bin/sh

Revision='$Revision: 18013 $'

bindir=/opt/local/bin
datadir=/opt/local/share/pgen
checkerdir=/opt/local/bin

RM=rm
SDF2TBL=${datadir}/Sdf2.baf
TOPSORT=SDF
PARSER="/opt/local/bin/sglr -s ${TOPSORT} -p ${SDF2TBL}"
GENERATOR="${bindir}/parsetablegen"
CHECKER="${checkerdir}/sdfchecker"
ADDPOSINFO="/opt/local/bin/addPosInfo"

modulename="-";

verbose=0;
error=0;
checker=1;

###
#  Handle the command line, &c.
###

myname=`basename $0`
myversion=`echo $Revision| cut -d' ' -f2`

# The argument vector: list of option letters, colons denote option
# arguments.  See Usage function, immediately below, for option
# explanation.
myarguments="bhi:lm:no:stvV"

# Usage: displays helpful usage information
Usage() {
cat << E_O_USAGE >&2
Usage: $myname [options]
Options:
    -b              write output in Binary AsFix (BAF) format
    -h              display help information (usage)
    -i _file_       input from _file_ (default: all file arguments)
    -l              display statistic information
    -m _name_       parse table is generated for module _name_ (default: Main)
    -n              only normalization of grammar
    -o _file_       output to _file_ (default: _inputfile_.tbl)
    -s              check sdf definition and show warnings on stderr
    -t              write output in plain text format
    -v              verbose mode
    -V              reveal program version (i.e. $myversion)
E_O_USAGE
}

Version() {
    echo "$myname v$myversion" >&2
}

# getopt handles command line...
args=`getopt $myarguments $* 2> /dev/null`
if test $? != 0
then
        Usage
        exit 2
fi
set -- $args

# Argument interpretation...
while [ $#  -gt 0 ]
do
    case "$1"
    in
	-b)
            PGOPTS="$PGOPTS $1" ;;
        -h)
            Usage ; exit 0 ;;
        -i)
            shift ; Files="$1 $Files" ;;
        -l)
            PGOPTS="$PGOPTS $1" ;;
        -m)
            shift ; modulename=$1 ;;
        -n)
            PGOPTS="$PGOPTS $1" ;;
        -o)
            shift ; output=$1 ;;
        -s)
	    checker=1 ;;
	-t)
            PGOPTS="$PGOPTS $1" ;;
        -v)
            verbose=1 ; VERBOSE="$1"; PGOPTS="$PGOPTS $1";;
        -V)
            Version ; exit 0 ;;
        --)
            ;;
    # Add additional command line options here...
    *)
            Files="$Files $1" ;;
    esac
    shift
done


###
#  Get to the real work now...
###


Warn () {
	echo $*                                   	>&2
}

Error () {
	Warn $*
	error=1
}

Abort () {
	Error $*
	exit 2
}

Notify () {
	if [ $verbose -gt 0 ] ; then
		Warn $*
	fi
}


GetBaseName () {
	thefile=`basename $1`; shift
	for e in $* ; do
		thebase=`basename $thefile $e`
		if [ $thebase != $thefile ] ; then
			echo $thebase ; return 0
		fi
	done
	echo $thefile ; return 0
}

#
## Main bit...
#

if [ "$modulename" = "-" ] ; then
        GENERATOR="$GENERATOR"
else
        GENERATOR="$GENERATOR -m $modulename"
fi 

if [ -z "$Files" ] ; then
	Files="-"
fi

for f in $Files
do
	if [ "$f" = "-" ] ; then
                af1name=${TMPDIR:-/var/tmp}/stdin.$$.asfix
                afposname=${TMPDIR:-/var/tmp}/stdin.$$.pos.asfix
		tblname=${output:-"-"}
	else
		base=`GetBaseName $f .sdf .sdf2 .SDF .SDF2 .Sdf .Sdf2 .def .DEF`
		dir=`dirname $f`
                af1name=${TMPDIR:-/var/tmp}/$base.$$.asfix
                afposname=${TMPDIR:-/var/tmp}/$base.$$.pos.asfix
		tblname=${output:-$dir/$base.tbl}
	fi
	Notify "Parsing $f..."
	if $PARSER $VERBOSE -i $f -o $af1name
	then
	        if [ $checker -gt 0 ] ;
		then
			Notify "Checking definition: $f"
			$ADDPOSINFO -i $af1name -o $afposname -p $f
			if $CHECKER -i $afposname
			then
			  ${RM} -f $afposname
			else
			  Warn "Error checking $f (see $afposname)"
			  error=1
			fi
                fi

	        Notify "Generating parse table ..."
	        if $GENERATOR $PGOPTS -i $af1name -o $tblname
		then
			${RM} -f $af1name
		else
			Warn "Error rewriting $f (see $af1name)"
			error=1
		fi
	else
		Warn "Error parsing $f (see $af1name)"
		error=1
	fi
done

exit $error;
