#!/bin/sh

# This script may fail, if one or more of "rm", "ln", "chmod", and "ed" 
# aren't found in the expected place. 

if test -z $1 || test -z $2 || test -z $3;
then echo "Usage: install <mosmldir> <holdir> <gnumake-path>"
     exit 1
fi


# Link to the components of the MoscowML compiler.
echo "Making links to MoscowML."
(cd $2/bin; 
 rm -f ./mosml ./mosmlc ./camlrunm ./cutdeps;
 ln -s $1/bin/mosml ./mosml;
 ln -s $1/bin/mosmlc ./mosmlc;
 ln -s $1/bin/camlrunm ./camlrunm;
 ln -s $1/tools/cutdeps ./cutdeps)


# Tell Holmake about archOS, HOLDIR, MOSMLHOME, and GNUMAKE.
echo "Setting up Holmake."
ed -s $2/tools/Holmake.src <<XXXX
/^MOSMLHOME=/a
$1
.
^1,.j
/^archOS=/a
`$2/tools/archOS`
.
^1,.j
/^HOLDIR=/a
$2
.
^1,.j
/^GNUMAKE=/a
$3
.
^1,.j
w $2/bin/Holmake
q
XXXX
chmod a+x $2/bin/Holmake


# Tell Holmake.enquote where HOLDIR is.
echo "Setting up a quotation preprocessing Holmake."
ed -s $2/tools/Holmake.enquote.src <<XXXX
/^HOLDIR=/a
$2
.
^1,.j
w $2/bin/Holmake.enquote
q
XXXX
chmod a+x $2/bin/Holmake.enquote


# Tell the system makefile where HOLDIR and MOSMLHOME are.
echo "Setting up the system Makefile."
ed -s $2/tools/Makefile.src <<XXXX
/^HOLDIR=/a
$2
.
^1,.j
/^MOSMLDIR=/a
$1
.
^1,.j
w $2/Makefile
q
XXXX


# Tell the std.prelude where HOLDIR is.
echo "Setting up the standard prelude."
ed -s $2/tools/std.prelude.src <<XXXX
/^val HOLDIR = Path.toString(Path.fromString/a
"$2/");
.
^1,.j
w $2/std.prelude
q
XXXX


#Generate a script to invoke mosmlc with. This one preprocesses
#quotations.
echo "Generating the mosmlc preprocessor script."
ed -s $2/tools/mosmlc.enquote.src <<XXXX
/^HOLDIR=/a
$2
.
^1,.j
w $2/bin/mosmlc.enquote
q
XXXX
chmod a+x $2/bin/mosmlc.enquote


#Generate a standard script to invoke HOL with. This one doesn't
# preprocess quotations.
echo "Generating the HOL script."
ed -s <<XXXX
a
#!/bin/sh
# The bare hol98 script

archOS=`$2/tools/archOS`; export archOS
HOLDIR=$2; export HOLDIR

$2/bin/mosml -quietdec -P full $2/std.prelude \$*
.
w $2/bin/hol
q
XXXX
chmod a+x $2/bin/hol


#Generate a standard script to invoke HOL with. This one preprocesses
#quotations.
echo "Generating the HOL script (with quote preprocessing)."
ed -s <<XXXX
a
#!/bin/sh
# The hol98 script (with quote preprocessing)

archOS=`$2/tools/archOS`; export archOS;
HOLDIR=$2; export HOLDIR

$2/src/quote-filter/enquote.\$archOS | $2/bin/hol $2/tools/use.sml \$*
.
w $2/bin/hol.enquote
q
XXXX
chmod a+x $2/bin/hol.enquote


#Tell the source used in building the HOL help database where MOSMLHOME is.
echo "Setting up the help database Makefile."
ed -s $2/tools/Makefile.dbase.src <<XXXX
/^MOSMLHOME=/a
$1
.
^1,.j
w $2/help/src/Makefile
q
XXXX

#Set up the makefile for the robdd library.
echo "Setting up the robdd library Makefile."
ed -s $2/src/robdd/GNUmakefile.src <<XXXX
/^MOSMLHOME:=/a
$1
.
^1,.j
/^HOLDIR:=/a
$2
.
^1,.j
w $2/src/robdd/GNUmakefile
q
XXXX

echo "Finished!"
