#! /bin/sh
#     ccompile
#     compiles MLP grammar/dictionary
# -- NTN 12/12/1997
echo "           MEDICAL LANGUAGE PROCESSING"
echo

if test "$1" = "-usage"
then
   echo "Usage: ccompile <gram/dict-text-source>"
   echo "       <gram/dict-text-source> with no .txt extension."
   echo "Usage: ccompile -usage [to get usage]"
   exit
fi

if test -f $1.txt -a "`head -2 $1.txt`"
then
   gram=$1
else
   echo "     >>> Input grammar/dictionary [.txt assumed]: \c"
   read gram
fi

if test ! -f $gram.txt -o ! "`head -2 $gram.txt`"
then
   echo "-- Error: $1.txt not found or empty"
   echo "Usage: ccompile <gram/dict-text-source>"
   echo "       <gram/dict-text-source> with no .txt extension."
   echo "Usage: ccompile -usage [to get usage]"
   exit
fi
kinc=`grep "^\*include" $gram.txt`
if test "$kinc" = ""
then
   ggram=$gram
else
   \rm new$gram.txt
   $NBIN/graminserts $gram.txt > new$gram.txt
   echo "     **$kinc into $gram.txt***"
   ggram=new$gram
fi
if [ `grep "^\*WD" $gram.txt | wc -l` -ne 0 ]
then
   echo "              Compiling "$gram.wdo
   echo
   echo "      Input text of dictionary  " $gram.txt
   GRAMOT="$gram.wdo"
   echo "      Output dictionary         " $gram.wdo
   \rm $gram.lis $gram.wdo
else
   if [ `grep "^\*RESTR" $gram.txt | wc -l` -ne 0 ]
   then
      echo "              Compiling "$gram.obg
      GRAMOT="$gram.obg"
      GRAMOS="$gram.sym"
      export GRAMOS
      echo
      echo "      Input text of grammar     " $gram.txt
      echo "      Output grammar            " $gram.obg
      echo "      Output symbol table       " $gram.sym
      \rm $gram.lis $gram.obg $gram.sym
   else
      echo "Error: File $gram.txt is ill-formed !"
      exit
   fi
fi
GRAMIN=$CRLS ; export GRAMIN
PARINP="$ggram.txt"
export PARINP
export GRAMOT
echo "      Compilation listing       " $gram.lis
echo "      created from source       " $ggram.txt
# $CPPPARSER | sed 's/[ ]*$//' > $gram.lis
# $PARSER | sed 's/[ ]*$//' > $gram.lis
$COMPILER | sed 's/[ ]*$//' > $gram.lis
chmod gou+rw $ggram.{lis,obg,sym}
echo
echo "          --- Compilation completed ---"
