#!/bin/sh
# Compile a Curry program (using the HTML library) into a cgi script

# Standard suffix that will be added to the main script:
CGISUFFIX="_CGIMAIN_$$"
# Name of the main function in the main script (should not be in conflict
# with any exported name of the Curry program)
MAINCALL="main_cgi_9999_$$"

ERROR=
HELP=no
PAKCSOPTIONS=
WRITEERRORS=yes
DEBUG=no
DEBUGFILE=
ULIMIT="-t 60"
MAIN=main
CGI_FILE=
ARGS=

while [ $# -gt 0 -a -z "$ERROR" ]; do
  case $1 in
   -help | -h | -\? ) HELP=yes ;;
   -D*              ) PAKCSOPTIONS="$PAKCSOPTIONS $1" ;;
   -noerror         ) WRITEERRORS=no ;;
   -compact         ) FCYPP="$FCYPP -compactexport " ; export FCYPP ;;
   -debug           ) DEBUG=yes ;;
   -debugfile       ) shift ; DEBUGFILE=$1 ;;
   -ulimit          ) shift; ULIMIT=$1 ;;
   -m               ) shift; MAIN=$1 ;;
   -o               ) shift; CGI_FILE=$1 ;;
   -*               ) ERROR="Unknown option: $1" ;;
   *                ) ARGS="$ARGS $1" ;; # collect non-option arguments
  esac
  shift
done
if test -n "$ARGS" ; then
  set $ARGS
fi

if [ $HELP = yes ] ; then
  set "1" ; shift # to show next usage message
fi

if test -n "$ERROR" ; then
  echo "ERROR: $ERROR"
  set "1" ; shift # to show next usage message
fi

if [ $# != 1 -a $# != 3 ] ; then
  echo "USAGE:     $0 [options] <curry>"
  echo "OPTIONS:"
  echo '-Dname=val : define pakcsrc property "name" as "val"'
  echo "-noerror   : do not write errors in error log file of the web server"
  echo "-compact   : reduce size of generated cgi program by deleting unused functions"
  echo "-debug     : include code for showing failures"
  echo "             (= PAKCS options '+printfail/+allfails')"
  echo "-debugfile f: include code for storing failure trace in file f"
  echo "             (= PAKCS options '+consfail file:f')"
  echo "-ulimit <l>: set 'ulimit <l>' when executing the cgi program"
  echo "-o <cgi>   : name of the file (with suffix .cgi) where the cgi program should"
  echo "             be stored (default: <curry>.cgi)."
  echo "-m <form>  : Curry expression (of type IO HtmlForm) computing the HTML form"
  echo "             (default: main)."
  echo "<curry>    : name of the Curry program (without suffix) containing the script"
  exit 1
fi

# Compute the main directory where PACKS is installed:
PATHNAME=`(cd \`dirname $0\` > /dev/null ; pwd)`
PAKCSHOME=`expr $PATHNAME : '\(.*\)/bin'`
export PAKCSHOME

if [ $# = 3 ] ; then # to support old makecurrycgi format
  CGI_FILE=$2
  MAIN=$3
  set $1
fi

# remove possible suffix:
PROG=`expr $1 : '\(.*\)\.lcurry' \| $1`
PROG=`expr $PROG : '\(.*\)\.curry' \| $PROG`

if test -z "$CGI_FILE" ; then
  CGI_FILE=$PROG.cgi
fi
MAINMOD=$PROG$CGISUFFIX
MAINCURRY=$MAINMOD.curry

# compute (relative) name of cgi program:
CGI_DIR=`dirname $CGI_FILE`
if [ $CGI_DIR = "." ] ; then
  CGI_PROG=$CGI_FILE
else
  CGI_PROG=`expr $CGI_FILE : "$CGI_DIR/\(.*\)"`
fi
# unique key for this cgi script:
CGI_KEY="$CGI_PROG `date`"

# generate main module for cgi script:
rm -f $MAINCURRY
echo "module $MAINMOD($MAINCALL) where" >> $MAINCURRY
echo "import $PROG" >> $MAINCURRY
echo "import HTML" >> $MAINCURRY
echo "$MAINCALL = runCgiWithKey \"$CGI_PROG\" \"$CGI_KEY\" ($MAIN)" >> $MAINCURRY

# compile main module:
echo "Generating saved state for initial expression: $MAIN"
echo
if [ $DEBUG = yes ] ; then
  PRINTFAIL="-set +allfails -set +printfail"
elif [ -n "$DEBUGFILE" ] ; then
  PRINTFAIL="-set +consfail file:$DEBUGFILE"
else
  PRINTFAIL=
fi
if [ $WRITEERRORS = yes ] ; then
  ERRORMODE="-set +error"
else
  ERRORMODE="-set -error"
fi
echo ":save $MAINCALL" | $PAKCSHOME/bin/curry2prolog $PAKCSOPTIONS $PRINTFAIL $ERRORMODE -l $MAINMOD
rm -f $MAINMOD.curry $MAINMOD.fcy $MAINMOD.fint $MAINMOD.pl $MAINMOD.po
STATE=$MAINMOD.state

# now the file $STATE should contain the saved state computing the HTML form:
if test ! -f $STATE ; then
  echo "Error: saved state '$STATE' does not exist!"
  exit 1
fi

TMPFILE=TMPCGIPROLOG$$
echo "#!/bin/sh" > $TMPFILE
if [ $WRITEERRORS = yes ] ; then
  # Patch the Sicstus saved state to show startup time:
  echo "echo \"[\`date\`]: start \$0\" >&2" >> $TMPFILE
fi
if test -n "$ULIMIT" ; then
  echo "ulimit $ULIMIT" >> $TMPFILE
fi
cat $TMPFILE $STATE > $CGI_FILE
rm -f $TMPFILE $STATE
chmod 755 $CGI_FILE

echo
echo "New file \"$CGI_FILE\" with compiled cgi script generated."
