#!/bin/csh -f
# echo 'Arguments are: ' $*
#************************************************************************
#
#			Projet     Formel
#
#			    ML in ML
#
#*************************************************************************
#
#            			Inria
#			Domaine de Voluceau
#			78150  Rocquencourt
#				France
#
#*************************************************************************
#
# camlyacc      camlyacc Script Cshell
#		Calls yacc, then a filter, then the CAML compiler
#
if (! $?CAMLSYS) then
        echo 'Warning: CAMLSYS variable undefined. Using BSD'
        setenv CAMLSYS BSD
endif
set CWD = `pwd`/
set CAMLYACC = "`expr $0 : '\(.*\)/camlyacc'`"
setenv PATH :${CAMLYACC}:/bin:/usr/local/bin:/usr/bin:/usr/ucb:${PATH}
set CLEAN = '/bin/rm -f camlyacc.lock'

onintr ERROR
if ($argv[1] == '-v' ) then
	set flag = ' -v'
	shift
else
	set flag = ''
        set CLEAN = "$CLEAN y.tab.c"
endif
set EDIT = "${CAMLYACC}/${CAMLSYS}/camlyacc.sh"
if ($?GEMACSCOMPILER) then
   	set EDIT = "gemacs -batch -q -l ${CAMLYACC}/${CAMLSYS}/camlyacc.el $CWD"
endif
set FILE = $argv[1]
if (-e camlyacc.lock) then
	echo 'Another camlyacc in progress'
	exit 1
else
	touch camlyacc.lock
endif
set SYNTAX = `sed -n -e '/^let current_syntax = /s/^let current_syntax = \"\(.*\)\";;$/\1/p' $FILE.ly | sed -n -e 1p`
if ("$flag" == ' -v') then
    echo ''; echo SYNTAX is \"$SYNTAX\" FILE is \"$FILE\"
endif
yacc $flag $FILE.ly || goto ERROR
test -s y.tab.c || goto ERROR
if ("$flag" == ' -v') then
echo ''
if ($?GEMACSCOMPILER) then
echo -n 'Calling batch emacs ...'
else
echo -n 'Editing y.tab.c ...'
endif
endif
($EDIT $SYNTAX >! $FILE\_mly.ml) || goto ERROR
if ("$flag" == ' -v') then
echo ''
endif
eval $CLEAN 
exit 0

ERROR:
eval $CLEAN ; exit 1
