#!/bin/ksh
#

ELAB=""
FULL=""
ALLOC=1M

ARCH=mipseb
OPSYS=irix5
RUN_DIR=../../bin/.run
OBJS_DIR=../runtime/objs
HEAP_IMAGE=""
RUN=""
RUN_ARGS=""
SET_FLAGS=""

#
# use the arch-n-opsys script to determine the ARCH/OS if possible
#
if [ -f ../../bin/.arch-n-opsys ]; then
  ARCH_N_OPSYS=`../../bin/.arch-n-opsys`
  if [ "$?" = "0" ]; then
    eval $ARCH_N_OPSYS
  fi
fi

while [ "$#" != "0" ]
do
  arg=$1; shift
  case $arg in
    -elab) ELAB="@SMLelab" ;;
    -full) FULL="@SMLfull" ;;
    -arch)
      if [ "$#" = "0" ]; then
        echo "xmakeml: missing argument for \"-arch\" option"
	exit 1
      fi
      ARCH=$1; shift
    ;;
    -opsys)
      if [ "$#" = "0" ]; then
        echo "xmakeml: missing argument for \"-opsys\" option"
	exit 1
      fi
      OPSYS=$1; shift
    ;;
    -o)
      if [ "$#" = "0" ]; then
        echo "xmakeml: missing argument for \"-o\" option"
	exit 1
      fi
      HEAP_IMAGE=$1; shift
    ;;
    -run)
      if [ "$#" = "0" ]; then
        echo "xmakeml: missing argument for \"-run\" option"
	exit 1
      fi
      RUN=$1; shift
    ;;
    -alloc)
      if [ "$#" = "0" ]; then
        echo "xmakeml: missing argument for \"-alloc\" option"
	exit 1
      fi
      ALLOC=$1; shift
    ;;
    -boot)
      if [ "$#" = "0" ]; then
        echo "xmakeml: missing argument for \"-boot\" option"
	exit 1
      fi
      BOOT_DIR=$1; shift
    ;;
    @SML*)
      RUN_ARGS="$RUN_ARGS $arg"
    ;;
    *) echo "xmakeml: unknown argument \"$arg\""
       exit 1
    ;;
  esac
done

case $OPSYS in
  solaris) MAKE=/usr/ccs/bin/make ;;
esac

if [ "$RUN" = "" ]; then
  RUN_CMD="run.$ARCH-$OPSYS"
  if [[ -x "$RUN_DIR/$RUN_CMD" ]]; then
    RUN="$RUN_DIR/$RUN_CMD"
  elif [[ -x "$OBJS_DIR/$RUN_CMD" ]]; then
    RUN="$OBJS_DIR/$RUN_CMD"
  else
    echo "xmakeml: unable to find run-time system \"$RUN_CMD\""
    exit 1
  fi
fi

if [ "$HEAP_IMAGE" = "" ]; then
  HEAP_IMAGE="sml.$ARCH"
fi

if [ "$BOOT_DIR" = "" ]; then
  BOOT_DIR="../../bin.$ARCH"
fi

if [[ ! -d ./bin.$ARCH ]]; then
  ln -s $BOOT_DIR ./bin.$ARCH
fi

#echo "$RUN @SMLboot=$BOOT_DIR @SMLalloc=$ALLOC $RUN_ARGS $ELAB $FULL -b $BOOT_DIR"
#$RUN @SMLboot=$BOOT_DIR @SMLalloc=$ALLOC $RUN_ARGS $ELAB $FULL -b $BOOT_DIR <<XXXX
echo "heap image is $HEAP_IMAGE"
echo "$RUN @SMLboot=$BOOT_DIR @SMLalloc=$ALLOC $RUN_ARGS $ELAB $FULL"
# $RUN @SMLboot=$BOOT_DIR @SMLalloc=$ALLOC $RUN_ARGS $ELAB $FULL <<XXXX
echo "$SET_FLAGS"
$SET_FLAGS
val use = Compiler.Interact.use_file
val use_stream = Compiler.Interact.use_stream;
val _ = (exportML "$HEAP_IMAGE";
	 print Compiler.banner;
         print " [new runtime]\n");
XXXX

if [[ -L ./bin.$ARCH ]]; then
  rm -f ./bin.$ARCH
fi
