#!/bin/sh
#
# Main script to run the components of PAKCS
#########
# IT WILL NOT WORK IF THE INDIVIDUALS PROGRAMS ARE NOT CALLED VIA
# THEIR ORIGINAL NAMES!
# (i.e., do not use links to the bin-dir with different program names)
#

# Load the definition of JAVA, SICSTUS and others specifying auxiliary tools:
PATHNAME=`(cd \`dirname $0\` > /dev/null ; pwd)`
. $PATHNAME/.pakcs_variables

export PIZZACLASSES # used in Curry2Java
export CLASSPATH    # used in Curry2Java

# Compute the main directory where PAKCS is installed:
PAKCSHOME=`expr $PATHNAME : '\(.*\)/bin'`
export PAKCSHOME

# Define load path for standard libraries provided by PAKCS:
# (used by the various programming tools to search for modules if they
# are not found elsewhere)
PAKCSLIBPATH=$PAKCSHOME/lib:$PAKCSHOME/lib/meta
export PAKCSLIBPATH

# The name of the currently called program:
progname=`basename $0`

# add $PAKCSHOME/bin to path so that command "sicstus" becomes executable:
PATH=$PAKCSHOME/bin:$PATH
export PATH

if [ $progname = pakcs ]
then
  # start the Curry->Prolog compiler:
  exec $PAKCSHOME/curry2prolog/c2p.state ${1+"$@"}

elif [ $progname = curry2prolog ]
then
  # start the Curry->Prolog compiler:
  exec $PAKCSHOME/curry2prolog/c2p.state ${1+"$@"}

elif [ $progname = curry2java ]
then
  # start the Curry->Java compiler:
  exec $PAKCSHOME/tc2java/curry2java ${1+"$@"}

elif [ $progname = tastecurry ]
then
  # start the TasteCurry interpreter:
  exec $PAKCSHOME/tastecurry/tastecurry.state

elif [ $progname = parsecurry ]
then
  # start the Curry front end:
  exec $PAKCSHOME/bin/.parsecurry ${1+"$@"}

elif [ $progname = sicstus ]
then
  # call current version of Sicstus-Prolog
  # (necessary to execute saved states of Sicstus-Prolog)
  if [ -z "$SICSTUS" ] ; then
    echo "ERROR: Variable SICSTUS undefined in PAKCS, can't execute 'sicstus'!" >&2
    exit 1
  else
    STATEARG=`expr "$2" : '\(.*\).cgi'`
    if [ "$1" = "-r" -a -n "$STATEARG" ] ; then
      # Suppress "% restoring..." message by omitting first line in stdout:
      # (works only for batch applications, so we use it only for cgi scripts)
      exec 3>&1 ; exec $SICSTUS ${1+"$@"} 2>&1 1>&3 | sed -e '1d' 1>&2
    else
      exec $SICSTUS ${1+"$@"}
    fi
  fi

elif [ $progname = swiprolog ]
then
  # call current version of SWI-Prolog
  if [ -z "$SWIPROLOG" ] ; then
    echo "ERROR: Variable SWIPROLOG undefined in PAKCS, can't execute 'swiprolog'!" >&2
    exit 1
  else
    exec $SWIPROLOG ${1+"$@"}
  fi

else
  echo "Error: unknown program '$progname'"
  exit 1
fi

