#!/bin/sh
################################################################
# Purpose: 	  Install XPCE/SWI-Prolog executable
# Usage:   	  ./install [-save] [excutable]
# Author:  	  Jan Wielemaker, University of Amsterdam
# Last Modified:  Thu Feb 23 11:34:30 1995
#		  + Added RTSUFFIX support for installing RUNTIME
#		  + Updated for SWI-Prolog 2.0.6
################################################################

PLBASE=/staff/jan/lib/pl-2.1.4
LN_S="ln -s"
RTSUFFIX=""
xpce=bin/xpce$RTSUFFIX
PWD=`pwd`
save=no;

if [ "x$1" = "x-save" ]; then save=yes; shift; fi
if [ $# = 1 ]; then xpce=$1; fi

for d in library boot include; do
    if [ ! -d pl/$d ]; then (cd pl; $LN_S $PLBASE/$d .); fi
done

# find the executable (emulator)

if [ -r bin/xpce$RTSUFFIX.base ]; then
    emulator="bin/xpce$RTSUFFIX.base"
elif [ -r bin/xpce$RTSUFFIX ]; then
    emulator="bin/xpce$RTSUFFIX"
elif [ -r pl/src/xpce$RTSUFFIX ]; then
    emulator="bin/xpce$RTSUFFIX"
    ( cd bin; ln -s ../pl/src/xpce$RTSUFFIX $emulator )
fi

if [ -r $PWD/Pce ]; then
    PCEHOME=""
    topdirs="$HOME /usr/local /home /opt"
    for top in $topdirs; do
	if [ -r $top ]; then
	    can="$PWD"
	    topinode=`ls -di $top | awk '{print $1}'`
	    base=""
	    while [ "$can" != "/" ] && [ "$PCEHOME" = "" ]; do
		caninode=`ls -di $can | awk '{print $1}'`
		if [ "$caninode" = "$topinode" ]; then
		    PCEHOME=`echo $top/$base | sed 's@/$@@'`
		fi
		base=`basename $can`/$base
		can=`dirname $can`
	    done
	fi
    done
    if test "$PCEHOME" = ""; then
	PCEHOME="$PWD"
    else
	if [ ! -r $PCEHOME/Pce ]; then PCEHOME="$PWD"; fi
    fi
else
    echo "$0 should be run from the XPCE home directory"
    exit 1
fi

SWIPL=$PCEHOME/pl; export SWIPL
CANONICAL_PATHS=$PCEHOME; export CANONICAL_PATHS
cat <<EOF
Using the following paths:

        PCEHOME=$PCEHOME
        SWIPL=$SWIPL
        CANONICAL_PATHS=$CANONICAL_PATHS

EOF

if [ "$save" = "yes" ]; then
echo "Check whether we can create a saved state"
$emulator -o bin/pl.qlf -b pl/boot/init.pl

$emulator -x bin/pl.qlf -f none -g true >.plparms << _EOS_
(   feature(save_program, true)
->  format('O_SAVE=yes~n')
;   true
),
halt.
_EOS_
. ./.plparms

if [ "$O_SAVE" != "yes" ]; then
    save=no
    echo "SWI-Prolog cannot save on this machine"
fi
fi

if [ "$save" = "no" ]; then
    cmd="$emulator -O -g pce_host:pce_banner -b $PCEHOME/pl/boot/init.pl -c $PCEHOME/pl/load.pl"
    echo "Creating quick load file using the command"
    echo "	$cmd"
    echo ""
    $cmd
    echo "Done"
    if [ ! "$xpce" = "$emulator" ]; then
        echo ""
	echo "Creating a link from $xpce to $PCEHOME/$emulator"
	dir=`dirname "$xpce"`
	base=`basename "$xpce"`
	(cd $dir; rm -f $base; ln -s $PCEHOME/$emulator $base)
    fi
    rm -f bin/pl.qlf
    exit 0
fi


rm -f $xpce				# Make sure it is gone
$emulator -x bin/pl.qlf -f none << _EOS_
['pl/load'].

format('Saving to ~w~n', ['$xpce']),
save_program('$xpce',
	[ goal		= 'pce_host:pce_banner'
	]).

get(@pce, home, Home),
concat(Home, '/prolog/lib', PceLib),
make_library_index(PceLib).


halt.
_EOS_

rm -f bin/pl.qlf
