#! /bin/sh -
Syntax() {
	echo 1>&2 "Syntax : Splus `basename $0` [args] inputfile"
	echo 1>&2 "  Optional arguments are:"
	echo 1>&2 "  -h, --help          Print this message and quit"
	echo 1>&2 "  -j                  Enable java"
	echo 1>&2 "  --cwd directory     Change to this directory before doing anything else"
	echo 1>&2 "  --input inputfile   Read input from inputfile (or specify inputfile as last argument)"
	echo 1>&2 "  --output file       Send output to file (default <basename inputfile>.txt)"
	echo 1>&2 "  --logfile file      Write session log to file (default <basename inputfile>.slg)"
	echo 1>&2 "                      By <basename inputfile> we mean inputfile stripped of possible period and extension"
	echo 1>&2 "  --appendlog         Append to log file (default is to overwrite)"
	echo 1>&2 "  --nologfile         Do not make a session log"
	echo 1>&2 "  --work directory    Use directory for working database (where=1), making it if needed"
	echo 1>&2 "                      If work is not specified we will make new randomly named directory for the working database"
	echo 1>&2 "  --noclean           Do not remove that new randomly named directory used for working database"
	echo 1>&2 "  --newworkparent dir Directory in which to make the new randomly named working database"
	echo 1>&2 "  --echo              Echo command lines to output (sets options(echo=T)) (this is the default)"
	echo 1>&2 "  --noecho            Do not set options(echo=T)"
	echo 1>&2 "  --verbose           Echo verbose logging information to output (sets options(verbose=T))"
	echo 1>&2 "  --quitonerror       Terminate immediately on engine error. (Default: continue through errors)"
	echo 1>&2 "  --background        Run S-PLUS in the background (returning immediately)"
	echo 1>&2 "  --nobigdata         Do not load bigdata library. "
	echo 1>&2 "  --bigdata           Load bigdata library, adding -j if needed. "
	echo 1>&2 "  --headless          Do not use X Windows connection if running java (i.e., DISPLAY need not be set)"
	echo 1>&2 "  --vanilla           Do not run user-defined startup scripts or functions (--work is ignored)"
	echo 1>&2 "  name=value          Will be added to environment so getenv(\"name\") returns \"value\""
}
Error() {
	echo 1>&2 "Error in `basename $0`: $1"
	exit 1
}
ExtraArg() {
    arg=$1
    case "$2" in
        "") Error "$arg needs to be followed by value" ;;
        -*) Error "$arg needs to be followed by value" ;;
    esac
}
newwork_flag=-newwork
echo_flag=-echo
log_specified=no
out_specified=no
inputfile=""
command="Splus START"
while [ $# -gt 0 ]
do
    case "$1" in
	-h|-help|--help) Syntax
	    exit 0
	    ;;
        -j) command="$command $1"
            ;;
	-logfile|--logfile)
            ExtraArg "$1" "$2"
	    command="$command $1 $2"
            shift
	    log_specified=yes
            ;;
	-nologfile|--nologfile)
	    log_specified=yes
	    ;;
	-noappendlog|--noappendlog) command="$command $1"
	    ;;
	-appendlog|--appendlog) command="$command $1"
	    ;;
        -input|--input)
            ExtraArg "$1" "$2"
	    command="$command $1 $2"
	    inputfile=$2
            shift
            ;;
        -output|--output)
	    ExtraArg "$1" "$2"
	    command="$command $1 $2"
            shift
	    out_specified=yes
            ;;
	-cwd|--cwd)
	    ExtraArg "$1" "$2"
	    command="$command $1 $2"
	    shift
	    ;;
        -work*|--work*)
	    ExtraArg "$1" "$2"
            command="$command $1 $2"
	    newwork_flag=""
	    shift
            ;;
	-newworkparent|--newworkparent)
	    ExtraArg "$1" "$2"
	    command="$command $1 $2"
	    shift
	    ;;
	-newwork|--newwork)
	    ;;
	-echo|--echo)
	    ;;
	-noecho|--noecho) echo_flag=""
	    ;;
	-noclean|--noclean) command="$command $1"
	    ;;
	-background|--background) command="$command $1"
	    ;;
	-verbose|--verbose) command="$command $1"
	    ;;
	-noverbose|--noverbose) command="$command $1"
	    ;;
	-quitonerror|--quitonerror) command="$command $1"
	    ;;
	-noquitonerror|--noquitonerror) command="$command $1"
	    ;;
	-nobigdata|--nobigdata) command="$command $1"
	    ;;
	-bigdata|--bigdata) command="$command $1"
	    ;;
	-headless|--headless) command="$command $1"
	    ;;
	-vanilla|--vanilla) command="$command $1"
	    ;;
        -*) Syntax ;
            Error "Unrecognized argument $1"
            ;;
         *) echo "$1" | grep '=' > /dev/null
            status=$?
            if [ "$status" = "0" ] ; then
	        command="$command `echo $1 | sed -e s/=/=\'/ -e s/\$/\'/`"
	    else
	        [ "X$inputfile" = "X" ] || { Syntax ; Error 'inputfile specified twice?' ; }
	        inputfile="$1"
	        command="$command -input $inputfile"
	    fi
	    ;;
    esac
    shift
done

[ "X$inputfile" = "X" ] && { Syntax ; Error 'inputfile not specified' ; }

command="$command $newwork_flag $echo_flag"
base=`echo $inputfile | sed -e 's/\.[^\.\/]*$//'`
[ "$log_specified" = "yes" ] || command="$command -logfile ${base}.slg"
[ "$out_specified" = "yes" ] || command="$command -output ${base}.txt"

[ "$S_DEBUG" = "yes" ] && echo Will execute: $command
eval $command
