#!/bin/sh
#-------------------------------------
# tksml configuration
#-------------------------------------

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

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

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


#-------------------------------------------------------------
# work out up-one-dir and up-two-dirs (UP_DIR, UP2_DIR)
#-------------------------------------------------------------

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

IFS="${IFS=   }"; saveifs="$IFS"; IFS="${IFS}/"
lastdir=
UP2_DIR=
for dir in $UP_DIR; do
    if test -n "$lastdir"; then 
	UP2_DIR="$UP2_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 sh
#-------------------------------------

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

if test -z "$SH"; then
   echo ""
   echo "Hmm..., \"sh\" was not found on your path."
   echo "That's pretty severe.  You have a strange UNIX installation."
   echo "It's pretty impossible for TkHolWorkbench to do anything without a"
   echo "shell interpreter.  Maybe your path is messed up??"
   echo ""
   SH=/bin/sh
fi
echo "using sh $SH"


#-------------------------------------
# Check for DISPLAY set
#-------------------------------------

if test -z "$DISPLAY"; then
    echo "Your DISPLAY variable is not set.  Please set it and rerun configure."
    echo "Configuration aborted"
    rm -f sed.tmp
    exit 1
fi

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

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

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 $UP2_DIR/expect-5.18/expectk"

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

echo "checking for expectk..."
for POSSIBLE_EXPECTK in $POSSIBLE_EXPECTKS; do
	case $POSSIBLE_EXPECTK in
        	$UP2_DIR/expect-5.18/expectk)
			TCL_LIBRARY=$UP2_DIR/tcl7.4/library
			export TCL_LIBRARY
			TK_LIBRARY=$UP2_DIR/tk4.0/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!="4.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 Tk version 4.0. 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 = 4.0
# We need Tcl version = 7.4
#-------------------------------------


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=$WISH
    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!="4.0"} { 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 4.0. 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=$UP2_DIR/tcl7.4/libtcl.a
	LIBTK=$UP2_DIR/tk4.0/libtk.a
	INCLUDETCL=$UP2_DIR/tcl7.4
	INCLUDETK=$UP2_DIR/tk4.0
    	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

echo "using Expect interpreter $EXPECTK"

#-------------------------------------
# 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 compile, and so it must be installed"
   echo "somewhere on your system.  You will probably need to download it"
   echo "and install it.  Flex is a GNU version of Lex, with extra features."
   echo ""
   FLEX=flex
fi
echo "using flex lexical analyser $FLEX"


#-------------------------------------
# Look for sml on the path.  
#-------------------------------------

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
    ML=$SML
    echo "using ML $SML"
fi


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

echo "set gui_flags(expectk) {$EXPECTK}" > ./.tksmlinstall
echo "set gui_flags(sh) {$SH}" >> ./.tksmlinstall
echo "set gui_flags(TCL_LIBRARY) {$TCL_LIBRARY}" >> ./.tksmlinstall
echo "set gui_flags(TK_LIBRARY) {$TK_LIBRARY}" >> ./.tksmlinstall
echo "set gui_flags(arch) {$ARCH}" >> ./.tksmlinstall
echo "set gui_flags(maintainer) {Donald.Syme@cl.cam.ac.uk}" >> ./.tksmlinstall
echo "set make_flags(cc) {$CC}" >> ./.tksmlinstall
echo "set make_flags(flex) {$FLEX}" >> ./.tksmlinstall
echo "set install_dir_tksml {$tksml_INSTALL_DIR}" >> ./.tksmlinstall
echo "set tksml_slave_flags(default_ml) {$ML}" >> ./.tksmlinstall
echo "source ./version.tcl" >> ./.tksmlinstall

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 Tk-SML \
	-paramdir . \
	-paramfile tksml.params \
	-configfile ./.tksmlinstall \"\$@\"" >> ./setup

chmod ugo+x ./setup

echo "creating modified makefiles and scripts..."

./setup -applychanges

