#!/bin/sh

#-------------------------------------
# 
#-------------------------------------

if test -f ".tkholinstall.sh"; then
    echo "reusing configuration settings found in .tkholinstall.sh"
     . .tkholinstall.sh
elif test -f "../.tkholinstall.sh"; then
    echo "reusing configuration settings found in ../.tkholinstall.sh"
     . ../.tkholinstall.sh

fi

#-------------------------------------------------------------
# configure tksml
#
#-------------------------------------------------------------

if (cd tksml; ./configure); then
   echo "tksml configuration succeeded"
else
   echo "tksml configuration failed"
fi
. tksml/.tksmlinstall.sh
#-------------------------------------------------------------
#
#-------------------------------------------------------------

echo "determining root directory..."
PWD=`pwd`
if test -z "$tkhol_INSTALL_DIR"; then
    tkhol_INSTALL_DIR=$PWD
fi
echo "using root directory $tkhol_INSTALL_DIR"


IFS="${IFS=   }"; saveifs="$IFS"; IFS="${IFS}/"
lastdir=
UP_DIR=
for dir in $tkhol_INSTALL_DIR; do
    if test -n "$lastdir"; then 
	UP_DIR="$UP_DIR/$lastdir" 
    fi
    lastdir="$dir"
done
IFS="$saveifs"

#-------------------------------------
# 
#-------------------------------------

if test -z "$ARCH"; then
    echo "checking system architecture..."
    ARCH=`arch`
fi
echo "using system architecture $ARCH"


#-------------------------------------
# Look for perl
#-------------------------------------

if test -z "$PERL"; then
    echo "checking for perl on path..."
    IFS="${IFS=   }"; saveifs="$IFS"; IFS="${IFS}:"
    for dir in $PATH; do
        test -z "$dir" && dir=.
        if test -f $dir/perl; then
            PERL=$dir/perl
	    break
        fi
    done
    IFS="$saveifs"
fi

if test -z "$PERL"; then
   echo ""
   echo "Hmm..., \"perl\" was not found on your path."
   echo "TkHol uses perl from time to time - for example to assist"
   echo "in bringing up help windows.  It would be best if you download"
   echo "and install it.  Perl is a GNU program."
   echo ""
   PERL=perl
fi
echo "using perl lexical analyser $PERL"


#-------------------------------------
# Look for a WWW browser
#-------------------------------------

if test -z "$WWW_BROWSER"; then
    echo "checking for Netscape or Mosaic on path..."
    IFS="${IFS=   }"; saveifs="$IFS"; IFS="${IFS}:"
    for dir in $PATH; do
        test -z "$dir" && dir=.
        if test -f $dir/Mosaic; then
            WWW_BROWSER=$dir/Mosaic
	    break
        fi
        if test -f $dir/xmosaic; then
            WWW_BROWSER=$dir/xmosaic
	    break
        fi
        if test -f $dir/netscape; then
            WWW_BROWSER=$dir/netscape
	    break
        fi
    done
    IFS="$saveifs"
fi


#-------------------------------------
# Look for hol90 on the path
#-------------------------------------

if test -z "$HOL90"; then
    echo "checking for hol90 on path..."
    IFS="${IFS=   }"; saveifs="$IFS"; IFS="${IFS}:"
    for dir in $PATH; do
        test -z "$dir" && dir=.
	if test -f $dir/hol90; then
            HOL90=$dir/hol90
	    break
        fi
    done
    IFS="$saveifs"
fi
if test -n "$HOL90"; then
    echo "using hol90 $HOL90"
fi

if test -z "$HOL90"; then
        echo ""
        echo "*** WARNING: hol90 could not be found on your path"
 	echo "*** WARNING: "
        echo "*** WARNING: If you want to use TkHolWorkbench, you will need to"
	echo "*** WARNING: specify a hol90 executable before executing make, by running"
	echo "*** WARNING:     ./setup"
 	echo "*** WARNING: "
 	echo "*** WARNING: TkHolWorkbench can no longer be used with hol88"
	echo ""
fi
HOL90_PLUS=$HOL90.tkhol

#-------------------------------------
# Run hol90 (if we've found it!).  Find
# the value of HOLdir to discover where
# the hol90 WWW documentation is kept.
#-------------------------------------


if test -n "$HOL90"; then
  if test -z "$HOL90_ROOT_DIR"; then
    echo "determining hol90 root installation directory..."
    echo "val file = open_out \"./hol90.root_pathname\";" > ./hol90.findroot.tmp
    echo "output(file,!Globals.HOLdir);" >> ./hol90.findroot.tmp
    echo "close_out file;" >> ./hol90.findroot.tmp
    $HOL90 < ./hol90.findroot.tmp > ./hol90.output
    HOL90_ROOT_DIR=`cat ./hol90.root_pathname`
    HOL90_URL="file:$HOL90_ROOT_DIR/doc"
    rm -f ./hol90.findroot.tmp ./hol90.root_pathname ./hol90.output
  fi
fi
if test -n "$HOL90_ROOT_DIR"; then
    echo "using hol90 root directory $HOL90_ROOT_DIR"
fi

#-------------------------------------
# Choose a value for HOL from HOL88 and HOL90
#-------------------------------------

if test -n "$HOL90"; then
    HOL=$HOL90
#else
#    HOL=$HOL88
fi

#-------------------------------------
# Work out the best place to go looking for the
# Laboratory of Applied Logic HOL Documentation
# WWW pages.
#-------------------------------------

if test -z "$LAL_URL"; then
	if domainname | grep "\.uk$" > /dev/null; then
	    LAL_URL=http://www.cl.cam.ac.uk/lal_holdoc/hol-documentation.html
	else
	    LAL_URL=http://lal.cs.byu.edu/lal/hol-documentation.html
	fi
fi

#-------------------------------------
# Look for sml on the path.  This is used
# by mlpretty and other programs.
#-------------------------------------
#
#if test -z "$SML"; then
#    echo "checking for sml on path..."
#    IFS="${IFS=   }"; saveifs="$IFS"; IFS="${IFS}:"
#    for dir in $PATH; do
#        test -z "$dir" && dir=.
#	if test -f $dir/sml; then
#            SML=$dir/sml
#	    break
#        fi
#    done
#    IFS="$saveifs"
#fi
#if test -n "$SML"; then
#    echo "using sml $SML"
#fi
#

#-------------------------------------
# 
#-------------------------------------

cp tksml/.tksmlinstall .tkholinstall
echo "set tksml_slave_flags(default_ml) {$HOL90_PLUS}" >> .tkholinstall
echo "set make_flags(default_hol90) {$HOL90}" >> .tkholinstall
echo "set make_flags(hol90_root_dir) {$HOL90_ROOT_DIR}" >> .tkholinstall
echo "set make_flags(hol90_plus) {$HOL90_PLUS}" >> .tkholinstall
#echo "set make_flags(default_sml) {$SML}" >> .tkholinstall
#echo "set make_flags(sml_version) {$SML_VERSION}" >> .tkholinstall
echo "set install_dir_tkhol {$tkhol_INSTALL_DIR}" >> .tkholinstall
echo "set gui_flags(wwwBrowser) {$WWW_BROWSER}" >> .tkholinstall
echo "set gui_flags(perl) {$PERL}" >> ./.tksmlinstall
echo "set HolHelp_flags(lal_URL) {$LAL_URL}" >> .tkholinstall
echo "set HolHelp_flags(hol90_URL) {$HOL90_URL}" >> .tkholinstall
echo "source ./version.tcl" >> .tkholinstall


if test -z "$TCL_LIBRARY"; then
    unset TCL_LIBRARY
    unset TK_LIBRARY
fi

if test -z "$TCL_LIBRARY"; then
    echo "#!/bin/sh" > ./setup
else
    	echo "#!/bin/sh" > ./setup
	echo "TCL_LIBRARY=$TCL_LIBRARY" >> ./setup
	echo "export TCL_LIBRARY" >> ./setup
	echo "TK_LIBRARY=$TK_LIBRARY" >> ./setup
	echo "export TK_LIBRARY" >> ./setup
fi
echo "$EXPECTK \
	-f ./tksetup/setup.tcl \
	-productname TkHolWorkbench \
	-paramdir ./tksml \
	-paramfile tksml.params \
	-paramdir . \
	-paramfile tkhol.params \
	-configfile .tkholinstall \"\$@\"" >> ./setup
chmod ugo+x ./setup

echo "creating modified makefiles and scripts..."

./setup -applychanges

