#!/bin/sh

PROLOG='@prolog@'
MLPATH='@mlpath@'

BASENAME='@basename@'
BIBTEX='@bibtex@'
CP='@cp@'
LATEX='@latex@'
RM='@rm@'
SED='@sed@'

prg=`$BASENAME $0`

if [ ! "$1" ]
then
  echo "$prg: Argument (file with specification of logic) missing"
  exit 1
fi

file=`echo $1 | $SED 's/\.lgc$//'`
shift

if [ "$*" ]
then
  echo "$prg: too many arguments ($*)"
  exit
fi

if [ ! -f "$file.lgc" ]
then
  echo "$prg: Specification $file.lgc does not exist."
  exit 1
fi

logfile=$file.mlg

for ext in tex sty aux log bbl blg dvi mlg ilc
do
   if [ -f $file.$ext ]
   then
      $RM -f $file.$ext
   fi
done

if [ ! -f $file.cfg ]
then
   $CP $MLPATH/ml.cfg $file.cfg
fi

$PROLOG 2>>$logfile <<EndPROLOG
['$MLPATH/ml'].
lgc2tex('$file.lgc','$file.sty','$file.cfg').
EndPROLOG

if [ -f "$file.sty" ]
then
  $CP $MLPATH/ml.tex $file.tex
  $CP $MLPATH/ml.sty .
  $CP $MLPATH/ml.bib .
  $CP $MLPATH/proof.sty .
  $RM $logfile
  echo
  echo "Your paper '$file.tex' is now ready for TeXing."
  echo
  echo "Use the commands:"
  echo "   latex $file"
  echo "   bibtex $file"
  echo "   latex $file"
  echo "   latex $file"
  echo "Viewing:"
  echo "   xdvi $file &"
else
  echo "$prg: Some error happened. See $logfile for more information."
  exit 1
fi
