#!/bin/sh
#
# COPYRIGHT (c) 2019 The SML/NJ Fellowship.
#
# @configure_input@
#
# a general-purpose script for installing executable wrappers for SML/NJ
# programs.
#
#	install-sml-wrapper.sh <program-name> <install-dir>

INSTALL="@INSTALL@"
INSTALL_DATA="@INSTALL_DATA@"
HEAP_SUFFIX="@SMLNJ_HEAP_SUFFIX@"
SIZE_OPT="@SMLNJ_SIZE@"

if test $# -lt 2 ; then
  echo "usage: install-sml-wrapper.sh <program-name> <install-dir>"
  exit 1
fi

SRC=$1; shift
TARGET=`basename $SRC`
HEAP_IMAGE=$SRC.$HEAP_SUFFIX
INSTALL_DIR=$1; shift
INSTALL_HEAP_DIR=$INSTALL_DIR/.heap
INSTALL_HEAP_IMAGE=$INSTALL_HEAP_DIR/$TARGET.$HEAP_SUFFIX

if test ! -f $HEAP_IMAGE ; then
  echo "heap image $HEAP_IMAGE not found"
  exit 1
fi

# create the wrapper script
#
cat > $TARGET <<XXXX
#!/bin/sh
#
exec @SMLNJ_CMD@ $SIZE_OPT @SMLcmdname=\$0 @SMLload=$INSTALL_HEAP_IMAGE \$@
XXXX

#install the script and heap image
#
if test ! -d $INSTALL_DIR ; then
  mkdir -p $INSTALL_DIR || exit 1
fi
$INSTALL $TARGET $INSTALL_DIR/$TARGET || exit 1
if test ! -d $INSTALL_HEAP_DIR ; then
  mkdir -p $INSTALL_HEAP_DIR || exit 1
fi
$INSTALL_DATA $HEAP_IMAGE $INSTALL_HEAP_IMAGE || exit 1

# remove the local copy of the script
rm -f $TARGET

exit 0

