#!/bin/sh

# Transform an existing saved state of a Curry program into
# a saved state that is executable independent of the
# generation environment, i.e., add path information to the saved state

STANDALONE=no
if [ "xx$1" = "xx-standalone" ] ; then
  STANDALONE=yes
  shift
fi

ERROR=no
if [ "xx$1" = "xx-error" ] ; then
  ERROR=yes
  shift
fi

if [ $# != 1 ] ; then
  echo "Usage: $0 [-standalone|-error] <saved_state_file>"
  echo "-standalone: transform saved state into stand-alone executable"
  echo "-error     : do not suppress messages on standard error output"
  exit 1
fi

# Compute the main directory where PACKS is installed:
PATHNAME=`(cd \`dirname $0\` ; pwd)`
PAKCSHOME=`expr $PATHNAME : '\(.*\)/bin'`
export PAKCSHOME

# Load definition of SPLD
. $PATHNAME/.pakcs_variables

if [ "xx$PAKCSLIBPATH" = xx ] ; then
  PAKCSLIBPATH="$PAKCSHOME/lib:$PAKCSHOME/lib/meta"
  export PAKCSLIBPATH
fi

STATE=$1
TMPSTATE=$STATE$$
if test ! -f $STATE ; then
  echo "ERROR: saved state '$STATE' does not exist!"
  exit 1
fi

if [ $STANDALONE = yes ] ; then
  if [ -x "$SICSTUS" ] ; then
    if [ -z "$SPLD" ] ; then
      echo "ERROR: environment variable SPLD not defined in pakcs/bin/.pakcs_variables!" >&2
      exit 1
    fi
    mv $STATE main.sav
    $SPLD --static --main=restore --resources-from-sav --resources=main.sav=`pwd`/main.sav --output=$STATE
    rm -f main.sav
    chmod 755 $STATE
    echo "Stand-alone executable '$STATE' generated."
    exit 0
  else
    # we have SWI-Prolog and nothing must be done (everything was done in saveprog)
    exit 0
  fi
fi

# Patch the Sicstus saved state to suppress startup infos like version# 
# and add correct PATH information:
mv $STATE $TMPSTATE
TMPFILE=TMPSAVEDSTATE$$
echo "#!/bin/sh" > $TMPFILE
echo "PAKCSHOME=$PAKCSHOME" >> $TMPFILE
echo "export PAKCSHOME" >> $TMPFILE
echo "PAKCSLIBPATH=$PAKCSLIBPATH" >> $TMPFILE
echo "export PAKCSLIBPATH" >> $TMPFILE
echo "PATH=$PATH:$PAKCSHOME/bin:\$PATH" >> $TMPFILE
echo "export PATH" >> $TMPFILE
if [ $ERROR = no ] ; then
  echo "exec 2> /dev/null" >> $TMPFILE
fi
cat $TMPFILE $TMPSTATE > $STATE
rm -f $TMPFILE $TMPSTATE
chmod 755 $STATE
