#!/bin/sh

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

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

#-------------------------------------------------------------
# configure
#
#-------------------------------------------------------------

if test -z "$PWD"; then 
    PWD=`pwd`
fi


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


IFS="${IFS=   }"; saveifs="$IFS"; IFS="${IFS}/"
lastdir=
UP_DIR=
for dir in $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 gcc on the path
#-------------------------------------

if test -z "$CC"; then
    CC=gcc
fi
echo "using C compiler $CC"

#-------------------------------------
# Look for expectk on the path
#-------------------------------------

echo "checking for possible expectk executables..."
if test -n "$EXPECTK"; then
    POSSIBLE_EXPECTKS=$EXPECTK
fi

IFS="${IFS=   }"; saveifs="$IFS"; IFS="${IFS}:"
for dir in $PATH; do
    test -z "$dir" && dir=.
    if test -f $dir/expectk; then
        POSSIBLE_EXPECTKS="$POSSIBLE_EXPECTKS $dir/expectk"
	break
    fi
done
IFS="$saveifs"

POSSIBLE_EXPECTKS="$POSSIBLE_EXPECTKS $UP_DIR/expect-5.7/expectk"

#-------------------------------------
# Integrity test on each possible EXPECTK
# Test if EXPECTK is really a expectk suitable for running TkHolWorkbench.
#
# We need Tk version = 3.6
#-------------------------------------

echo "checking for expectk..."
for POSSIBLE_EXPECTK in $POSSIBLE_EXPECTKS; do
	case $POSSIBLE_EXPECTK in
        	$UP_DIR/expect-5.7/expectk)
			TCL_LIBRARY=$UP_DIR/tcl7.3/library
			export TCL_LIBRARY
			TK_LIBRARY=$UP_DIR/tk3.6/library
			export TK_LIBRARY
            	;;
		*)
			unset TCL_LIBRARY
			unset TK_LIBRARY
            	;;
	esac

    if test -f $POSSIBLE_EXPECTK; then
        echo "checking for Tk version of $POSSIBLE_EXPECTK"
        echo "if {\$tk_version!="3.6"} { exit 1 }" > ./expectk.test.tmp 
        echo "exit 0" >> ./expectk.test.tmp
        if $POSSIBLE_EXPECTK < ./expectk.test.tmp > /dev/null; then
	    sleep 0
        else
	    echo "Hmmm... $POSSIBLE_EXPECTK is not Tk version 3.6. Looking for others..."
            continue
	fi

    	echo "checking for expectk commands in $POSSIBLE_EXPECTK"
    	echo "if {[llength [info commands exp_version]]==0 || [llength [info commands trap]]==0} { exit 1 }" > ./expectk.test.tmp
    	echo "exit 0" >> ./expectk.test.tmp
    	if $POSSIBLE_EXPECTK < ./expectk.test.tmp > /dev/null; then
	    sleep 0
    	else
	    echo "Hmmm... $POSSIBLE_EXPECTK is not an expectk executable. Looking for others..." }
            continue
        fi

        EXPECTK=$POSSIBLE_EXPECTK
        break
    fi

done

rm -f expectk.test.tmp

#-------------------------------------
# If we haven't found a copy of "expectk" to use,
# we go looking for versions of Tcl and Tk which we
# can use to compile Expect.
#
# We need Tk version = 3.6
# We need Tcl version = 7.3
#-------------------------------------


if test -z "$EXPECTK"; then
    echo "Hmmm... couldn't find a suitable expectk executable."
    echo "checking for possible wish executables..."

    if test -n "$WISH"; then
        POSSIBLE_WISHS=$EXPECTK
    fi

    IFS="${IFS=   }"; saveifs="$IFS"; IFS="${IFS}:"
    for dir in $PATH; do
        test -z "$dir" && dir=.
        if test -f $dir/wish; then
            POSSIBLE_WISHS="$POSSIBLE_WISHS $dir/wish"
	    break
        fi
    done
    IFS="$saveifs"

    unset TCL_LIBRARY
    unset TK_LIBRARY
    for POSSIBLE_WISH in $POSSIBLE_WISHS; do
        if test -f $POSSIBLE_WISH; then
            echo "checking Tk version of $POSSIBLE_WISH..."
            echo "if {\$tk_version!="3.6"} { exit 1 }" > ./wish.test.tmp 
            echo "exit 0" >> ./wish.test.tmp
            if $POSSIBLE_WISH < ./wish.test.tmp > /dev/null; then
	        sleep 0
            else
	        echo "Hmmm... $POSSIBLE_WISH is not Tk version 3.6. Looking for others..."
                continue
	    fi
        fi
	WISH=$POSSIBLE_WISH
	break
    done
       
    if test -n "$WISH"; then
        echo "finding the root installation directory for $WISH..."
    	echo "puts [file dirname [file dirname [set tk_library]]]" > ./wish.test.tmp
    	echo "exit 0" >> ./wish.test.tmp
        $WISH -f ./wish.test.tmp > ./wish.output
        TK_ROOT_INSTALLATION_DIR=`cat ./wish.output`
	POSSIBLE_LIBTCL=$TK_ROOT_INSTALLATION_DIR/lib/libtcl.a
	POSSIBLE_LIBTK=$TK_ROOT_INSTALLATION_DIR/lib/libtk.a
	POSSIBLE_INCLUDETCL=$TK_ROOT_INSTALLATION_DIR/include
	POSSIBLE_INCLUDETK=$TK_ROOT_INSTALLATION_DIR/include
	cat << \EOF > tkMain.test.tmp.c
#include <stdio.h>
#include <tcl.h>
#include <tk.h>
static Tk_Window mainWindow;
static Tcl_Interp *interp;
int
main()
{
    interp = Tcl_CreateInterp();
    mainWindow = Tk_CreateMainWindow(interp, NULL, NULL, "Tk");
    Tk_MainLoop();
    Tcl_Eval(interp, "exit");
}
EOF
#		$POSSIBLE_LIBTCL 
#		$POSSIBLE_LIBTK 
#		-I$POSSIBLE_INCLUDETCL 
#		-I$POSSIBLE_INCLUDETK 

	echo "trying to compile a simple Tk program..."
	if $CC ./tkMain.test.tmp.c -o ./tkMain.test.tmp \
		-ltk -ltcl -lX11 -lm > ./tkMain.test.tmp.out; then
	    echo "compilation of a simple Tk program succeeded. Good."
	    LIBTCL=$POSSIBLE_LIBTCL
	    LIBTK=$POSSIBLE_LIBTK
	    INCLUDETCL=$POSSIBLE_INCLUDETCL
	    INCLUDETK=$POSSIBLE_INCLUDETK
	else 
	    echo "compilation of a simple Tk program failed...Not so good."
	    echo ""
            echo "  \"configure\" found an installation of Tk which appeared to"
	    echo "  be appropriate for compiling Expect.  However the compilation"
	    echo "  of a simple Tk/Tcl program failed, probably because Tk is installed"
	    echo "  in a non-standard place ($TK_ROOT_INSTALLATION_DIR??).  You will "
	    echo "  need to either compile Expect yourself, or follow the instructions "
	    echo "  below to compile all of Tk, Tcl and Expect."
	    echo ""
	fi
    fi

fi

rm -f ./wish.test.tmp tkMain.test.tmp tkMain.test.tmp.c tkMain.test.tmp.out


#-------------------------------------
# If we haven't found a copy of "expectk" to use, and now
# we tell the user this.  They will need to recompile either
# all of Tcl, Tk and Expect or just Expect, depending
# on whether a Tcl/Tk installation has been found.
#-------------------------------------


if test -z "$EXPECTK"; then
    echo ""
    if test -z "$LIBTK"; then
	LIBTCL=../tcl7.3/libtcl.a
	LIBTK=../tk3.6/libtk.a
	INCLUDETCL=../tcl7.3
	INCLUDETK=../tk3.6
    	echo "\"configure\" could not find a copy of Expect suitable for use with this"
   	echo "package.  This is not a problem.  You will need to compile the "
	echo "Tcl, Tk and Expect packages found in this directory.  Do this by using"
    else
    	echo "\"configure\" could not find a copy of Expect suitable for use with"
   	echo "this package.  The good news is that it has found a version of Tcl/Tk"
        echo "installed on your system, so you don't have to recompile those packages."
        echo "To compile Expect, just use"
    fi
    echo "    make expectk"
    echo "from this directory.  Then rerun this program using"
    echo "    ./configure"
    echo ""
    echo "NOTE: This will create directories parallel with this one which"
    echo "will be usable with later versions of TkHolWorkbench."
    echo ""
    echo "Creating Makefile..."
    echo "s,^LIBTCL=\$,LIBTCL=$LIBTCL," > sed.tmp
    echo "s,^LIBTK=\$,LIBTK=$LIBTK," >> sed.tmp
    echo "s,^INCLUDETCL=\$,INCLUDETCL=$INCLUDETCL," >> sed.tmp
    echo "s,^INCLUDETK=\$,INCLUDETK=$INCLUDETK," >> sed.tmp
    sed -f sed.tmp < Makefile.MASTER > Makefile
    echo "Aborting setup..."
    rm -f sed.tmp
    exit 1
fi

#-------------------------------------
# Look for flex
#-------------------------------------

if test -z "$FLEX"; then
    echo "checking for flex on path..."
    IFS="${IFS=   }"; saveifs="$IFS"; IFS="${IFS}:"
    for dir in $PATH; do
        test -z "$dir" && dir=.
        if test -f $dir/flex; then
            FLEX=$dir/flex
	    break
        fi
    done
    IFS="$saveifs"
fi
if test -z "$FLEX"; then
   echo ""
   echo "Hmm..., \"flex\" was not found on your path."
   echo "TkHolWorkbench needs flex to complie, and so it must be installed"
   echo "somewhere on your system.  You will probably need to download it"
   echo "and install it."
   echo ""
   FLEX=flex
fi


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

if test -z "$WWW_BROWSER"; then
    echo "checking for flex 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 hol88
#-------------------------------------

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


#-------------------------------------
# Run hol88 (if we've found one!) and call
# "library_pathname()" to work out where the
# lisp object files will be.
#
# Then check they are there.
#-------------------------------------


if test -z "$LispDir" && test -n "$HOL88"; then
    echo "determining hol88 root installation directory..."
    echo "let file = openw \`./hol88.library_pathname\`;;" > ./hol88.findroot.tmp
    echo "write(file,library_pathname());;" >> ./hol88.findroot.tmp
    echo "close file;;" >> ./hol88.findroot.tmp
    echo "quit();;" >> ./hol88.findroot.tmp
    $HOL88 < ./hol88.findroot.tmp > ./hol88.output
    HOL88_LIBRARY_DIR=`cat ./hol88.library_pathname`
    IFS="${IFS=   }"; saveifs="$IFS"; IFS="${IFS}/"
    lastdir=
    LispDir=
    for dir in $HOL88_LIBRARY_DIR; do
        if test -n "$lastdir"; then 
	    LispDir="$LispDir/$lastdir" 
	fi
	lastdir="$dir"
    done
    IFS="$saveifs"
    LispDir="$LispDir/lisp"
    rm -f ./hol88.findroot.tmp ./hol88.library_pathname ./hol88.output



fi


if test -n "$LispDir" && test -n "$HOL88"; then
    echo "checking for lisp .o files in $LispDir..."
    if test -f $LispDir/f-macro.o; then
        sleep 0
    else 
        echo ""
        echo "*** WARNING: The hol88 lisp .o files were not present in"
	echo "*** WARNING:     $LispDir/"
 	echo "*** WARNING: This is probably because they were deleted after compialtion"
        echo "*** WARNING: If you want to use TkHolWorkbench with hol88, you will need to"
	echo "*** WARNING: recompile these, or alternatively specify a directory where"
	echo "*** WARNING: they may be found by running "
	echo "*** WARNING:     ./setup"
	echo "*** WARNING: Either way, you may still use TkHolWorkbench with hol90"
	echo "*** WARNING: if hol90 is installed on your system."
	echo ""
    fi
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



#-------------------------------------
# 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
    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

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

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

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

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




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

echo "set gui_flags(expectk) {$EXPECTK}" > ../.tkholinstall
echo "set gui_flags(TCL_LIBRARY) {$TCL_LIBRARY}" >> ../.tkholinstall
echo "set gui_flags(TK_LIBRARY) {$TK_LIBRARY}" >> ../.tkholinstall
echo "set gui_flags(flex) {$FLEX}" >> ../.tkholinstall
echo "set TkHolSlave_flags(default_hol) {$HOL}" >> ../.tkholinstall
echo "set TkHolSlave_flags(default_hol88) {$HOL88}" >> ../.tkholinstall
echo "set TkHolSlave_flags(default_hol90) {$HOL90}" >> ../.tkholinstall
echo "set gui_flags(LispDir) {$LispDir}" >> ../.tkholinstall
echo "set gui_flags(arch) {$ARCH}" >> ../.tkholinstall
echo "set gui_flags(install_dir) {$INSTALL_DIR}" >> ../.tkholinstall
echo "set gui_flags(maintainer) {Donald.Syme@cl.cam.ac.uk}" >> ../.tkholinstall
echo "set gui_flags(wwwBrowser) {$WWW_BROWSER}" >> ../.tkholinstall
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
    echo "#!/bin/sh" > ./setup
    echo "$EXPECTK -f ./setup.tcl \$*" >> ./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
	echo "$EXPECTK -f ./setup.tcl \$*" >> ./setup
fi
chmod ugo+x ./setup
# chmod ugo+x ./cml2sml/cml2sml

echo "creating modified makefiles and scripts..."

./setup -applychanges

echo ""
echo "Auto-configuration complete. You should now modify the configuration "
echo "defaults by running"
echo "        ./setup"
echo "or simply make TkHolWorkbench using one of"
echo "        make all"
echo "        make hol90"
echo "        make hol88"
echo ""
