#!@SHELL@
#
# The standard driver for SML/NJ under the new runtime system
#

CMD=`basename $0`

#
# for /bin/ksh, disable reading user's environment file
#
unset ENV

#############################################################################
#
# BEGIN SITE SPECIFIC STUFF
#
#############################################################################

#
# SITE SPECIFIC CONFIGURATION INFO
#

# the path of the directory in which executables (like this file) are kept.
BIN_DIR=@BINDIR@

# the path of the directory in which the runtime system executables are kept.
RUN_DIR=$BIN_DIR/.run

# the path of the directory in which the heap images are kept.
#case `uname -s` in
#  # For UWIN2.0, need a gross hack to change /c/foo/bar into c:/foo/bar
#  UWIN-NT)
#    HEAP_DIR=`awk -F'/' '{printf ("%s:",$2) ; for (i=3; i<=NF; i++) printf ("/%s", $i)}' <<XXX
#$BIN_DIR/.heap
#XXX`
#  ;;
#  *)    
#    HEAP_DIR=$BIN_DIR/.heap
#  ;;
#esac
HEAP_DIR=$BIN_DIR/.heap

#
# the following could be replaced with some site specific code
#
ARCH_N_OPSYS=`$BIN_DIR/.arch-n-opsys`
if [ "$?" != "0" ]; then
  echo "$CMD: unable to determine architecture/operating system"
  exit 1
fi
eval $ARCH_N_OPSYS

#############################################################################
#
# END SITE SPECIFIC STUFF
#
#############################################################################

#
# Process command line arguments
#
ARGS=""
while [ "$#" != "0" ]; do
  arg=$1; shift
  case $arg in
    @SMLrun=*)
      RUN=`echo $arg | sed 's/@SMLrun=//'`
      ;;
    @SMLload=*)
      HEAP=$arg;
      ;;
    @SMLappl)
      if [ "$#" = "0" ]; then
        echo "$CMD: missing argument for @SMLappl option"
        exit 1
      fi
      APPL=$1; shift
      ;;
    @SMLversion)
      echo "$CMD @VERSION@"
      exit 0
      ;;
    *)
      ARGS="$ARGS $arg"
      ;;
  esac
done

if [ "$RUN" = "" ]; then
  #
  # Construct the runtime system path from the ARCH and OS
  #
  case $OPSYS in        
    win32)
      RUN="$RUN_DIR/run.$ARCH-win32.exe"
    ;;
    *)
      RUN="$RUN_DIR/run.$ARCH-$OPSYS"
    ;;
  esac
fi

if [ "$HEAP" = "" ]; then
  #
  # Construct the heap image path from the APPL and ARCH
  #
  if [ "$APPL" = "" ]; then
    APPL=$CMD
  fi
  HEAP="@SMLload=$HEAP_DIR/$APPL"
fi

if [ ! -x "$RUN" ]; then
  echo "$CMD: cannot find runtime system $RUN"
  exit 1
fi

#
# run the sucker!
#
exec $RUN @SMLcmdname=$0 $HEAP $ARGS
