#!/bin/sh


MALLOCTYPE=3.1
export MALLOCTYPE

HOST=`hostname`
export HOST

ciao() {
        echo "Goodbye."
        exit 1
}

needsubopt () {
        echo "The $1 option requires an argument."
        ciao
}
showuse() {
echo "axiom"
echo "  [-ht    |-noht]      whether to use HyperDoc"
echo "  [-gr    |-nogr]      whether to use Graphics"
echo "  [-clef  |-noclef]    whether to use Clef"
echo "  [-nonag |-nag]       whether to use NAG"
echo "  [-noiw  |-iw]        start in interpreter in a separate window"
echo "  [-ihere |-noihere]   start an interpreter in this window"
echo "  [-nox]               don't use X Windows"
echo "  [-go  |-nogo]        whether to start system"
echo "  [-ws wsname]         use named workspace"
echo "  [-list]              list workspaces only"
echo "  [-grprog fname]      use named program for Graphics"
echo "  [-nagprog fname]     use named program for Nag"
echo "  [-htprog fname]      use named program for HyperDoc"
echo "  [-clefprog fname]    use named program for Clef"
echo "  [-sessionprog fname] use named program for session"
echo "  [-clientprog fname]  use named program for spadclient"
echo "  [-h]                 show usage"
}

listwspaces()
{
        echo "$1"
        ls -l $2 | grep "sys$"
        echo ""
}


if [ "$*" = "-h" ] ; then
     showuse
fi
SPADDEFAULT=/usr/local/axiom/mnt/linux

if [ "$SPAD" = "" ] ; then
  if [ "$AXIOM" = "" ] ; then
    SPAD=$SPADDEFAULT
    echo "AXIOM variable is not set"
    echo "assuming AXIOM = $SPAD"
    AXIOM=$SPAD
    export AXIOM
  else
    SPAD=$AXIOM
  fi
  export SPAD
else
  if [ "$AXIOM" = "" ] ; then
    echo "AXIOM variable is not set"
    echo "but SPAD = $SPAD"
    echo "Using AXIOM = $SPAD"
    AXIOM=$SPAD
    export AXIOM
  else
    if [ ! "$SPAD" = "$AXIOM" ] ; then
       echo "ignoring SPAD variable"
       SPAD=$AXIOM
    fi
  fi
fi

if [ ! -d "$SPAD" ] ; then
  echo "The directory for Axiom, $SPAD, does not exist."
  ciao
fi

rootwsdir=$SPAD/bin

list=no

go=yes

wsname=AXIOMsys

otheropts=""

while [ "$*" != "" ] ; do

        case $1 in

        -list)  list=yes
                go=no;;
        -go)    go=yes ;;
        -nogo)  go=no ;;

        -ws)
                if [ "$2" = "" ] ; then needsubopt "$1" ; fi
                shift
                wsname="$1"
                ;;

        -nagprog|-grprog|-htprog|-clefprog|-sessionprog|-clientprog)
                if [ "$2" = "" ] ; then needsubopt "$1" ; fi
                otheropts="$otheropts  $1 $2"
                shift
                ;;
        -paste|-rm|-rv)
                if [ "$2" = "" ] ; then needsubopt "$1" ; fi
                otheropts="$otheropts  $1 $2"
                shift
                ;;
        -clef|-noclef|-gr|-nogr|-ht|-noht|-iw|-noiw)
                otheropts="$otheropts $1"
                ;;
        -ihere|-noihere|-nox|-nag|-nonag)
                otheropts="$otheropts $1"
                ;;
        -h)
                go=no
                ;;
        *)      echo "Unknown option: $1"
                echo "To use a specific workspace use, e.g.: spad -ws $1"
                ciao
                ;;
        esac

        shift
done

if [ $list = yes ] ; then
 listwspaces "AXIOM workspaces in \$AXIOM/bin = $rootwsdir: " $rootwsdir
fi

if [ `expr $wsname : '.*/.*'` = 0 ] ; then
        serverws=$rootwsdir/$wsname
else
        serverws=$wsname
fi

if [ ! -x $serverws ] ; then
        echo "Cannot find the executable $serverws"
        showuse
        ciao
fi
if [ $go = no ] ; then
        echo "Would now start the processes."
        echo exec $SPAD/bin/sman $otheropts -ws $serverws
        exit 0
fi
exec $SPAD/bin/sman $otheropts -ws $serverws

