:
#
# configure script for tcl 4.0
#
#

# There are to shell scripts that this main script uses.  They should be 
# be shell procs, but not all sh shell hasve procs.  Some environment
# vars are used to communicate with them, as well as paramaters.
# Extract them to a temporary file.

GETYN=/tmp/tmp1.$$
cat <<'E_O_STUFF' >$GETYN
:
# Shell procedure for getting a yes/no answer.  First parameter is the
# string to prompt with, the second is the default value to use.  The 
# result is printed to be grabbed with the shell backquote mechanism

if [ $# -ne 2 ]
then
 echo "usage: GetYN prompt default_value"
 exit 1
fi
YNReturn=$2
echo >/dev/tty
echo "$1 (y/n) [$2] \c" >/dev/tty
read YNin
if [ "$YNin" != "" ]
then
    if [ "$YNin" != "y" ]
    then
        if [ "$YNin" != "n" ]
        then
	    echo "(I am interpreting \"$YNin\" as a no...)" >/dev/tty
	    $YNin="n"
        fi
    fi
    YNReturn=$YNin
fi
echo $YNReturn
E_O_STUFF
chmod u+x $GETYN

OUTMACROS=/tmp/tmp2.$$
cat <<'E_O_STUFF' >$OUTMACROS
:
# Procedure to output all the makefile shell macro definitions that we
# have built up.  First parameter is the file to output to.
echo $dbar          >$1
echo "#"           >>$1
echo "#  WARNING: This macro definition section of this Makefile" >>$1
echo "#           is built by the Configure script."              >>$1
echo "#"           >>$1
echo $bar          >>$1
echo $MF_SYSDEF    >>$1
echo $MF_COMPOPTS  >>$1
echo $MF_RANLIB    >>$1
echo $MF_LINKFLAGS >>$1
echo $MF_LIBS      >>$1
echo $MF_CFLAGS    >>$1
echo $MF_STRIPCMD  >>$1
echo $MF_MCSCMD    >>$1
echo "#"           >>$1
echo $bar          >>$1
E_O_STUFF
chmod u+x $OUTMACROS

export dbar bar 
export MF_SYSDEF MF_COMPOPTS MF_RANLIB MF_LINKFLAGS MF_LIBS MF_CFLAGS
export MF_STRIPCMD MF_MCSCMD

# Read in the last configuration values used.

CONFIG=tclsh/src/config.vars
ORIG_CONFIG=tclsh/src/config.vars.t
CONFIG_H=tclsh/src/config.h
if [ "$1" = "-d" ]
then
  rm -f $CONFIG
fi
if [ ! -f $CONFIG ]
then
 cp $ORIG_CONFIG $CONFIG
fi

. $CONFIG

CWD=`pwd`

TCL_VERSION="4.0"
GOTIT=n
while [ "$GOTIT" = "n" ]
do
  echo "Enter Unix (sysv3.2.0, sysv3.2.2, bsd, hpux, xenix286 or xenix386)"
  echo "[$UNIX_TYPE]: \c"
  read A
  if [ "$A" = "" ]
  then
    if [ "$UNIX_TYPE" != "" ]
    then
        GOTIT=y
    fi
  else
    case "$A" in
    sysv3.2.0) GOTIT=y ; UNIX_TYPE=$A ;;
    sysv3.2.2) GOTIT=y ; UNIX_TYPE=$A ;;
    bsd) GOTIT=y ; UNIX_TYPE=$A ;;
    hpux) GOTIT=y ; UNIX_TYPE=$A ;;
    xenix286) GOTIT=y ; UNIX_TYPE=$A ;;
    xenix386) GOTIT=y ; UNIX_TYPE=$A ;;
    esac
  fi
done

if [ "$UNIX_TYPE" = "bsd" ]
then
  if [ "$HAS_STRERROR" = "" ]
  then
    HAS_STRERROR="y"
  fi
  echo "Does your C library have the strerror function?"
  echo "[$HAS_STRERROR]: \c"
  read A
  if [ "$A" != "" ]
  then
    HAS_STRERROR=$A
  fi
fi

if [ "$TCL_BINDIR" = "" ]
then
  TCL_BINDIR=$CWD
fi
echo "Directory to install Tcl binary into?\n[$TCL_BINDIR]: \c"
read A
if [ "$A" != "" ]
then
  TCL_BINDIR=$A
fi

PKG_TCL_LIBDIR=$CWD
if [ "$TCL_LIBDIR" = "" ]
then
  TCL_LIBDIR=$PKG_TCL_LIBDIR
fi
echo "Directory tcl.a library goes into?\n[$TCL_LIBDIR]: \c"
read A
if [ "$A" != "" ]
then
  TCL_LIBDIR=$A
fi

PACKAGE_TCL_SOURCEDIR=$CWD/tclsh/tclsrc
if [ "$TCL_SOURCEDIR" = "" ]
then
  TCL_SOURCEDIR=$PACKAGE_TCL_SOURCEDIR
fi
echo "Directory .tcl files go into?\n[$TCL_SOURCEDIR]: \c"
read A
if [ "$A" != "" ]
then
  TCL_SOURCEDIR=$A
fi

if [ "$TCL_DEFAULTFILE" = "" ]
then
  TCL_DEFAULTFILE=/etc/default/tcl
fi
echo "Tcl default file for configuration information [$TCL_DEFAULTFILE]: \c"
read A
if [ "$A" != "" ]
then
  TCL_DEFAULTFILE=$A
fi

echo "Tcl init file [$TCL_INITFILE]: \c"
read A
if [ "$A" != "" ]
then
  TCL_INITFILE=$A
fi

if [ "$TCL_HISTORY" = "" ]
then
    TCL_HISTORY="y"
fi
echo "History provides a facility for editing and redoing previously entered"
echo "command lines.  To include it adds about 6K bytes to your tcl program."

TCL_HISTORY=`$GETYN "Do you want history included?" $TCL_HISTORY`

if [ "$TCL_DEBUG" = "" ]
then
    TCL_DEBUG="n"
fi
TCL_DEBUG=`$GETYN "Do you want symbolic debugging included?" $TCL_DEBUG`

if [ "$TCL_DEBUG" = "y" ]
then
    if [ "$TCL_DBGSTRING" = "" ]
    then
        TCL_DBGSTRING="-g"
    fi
    echo "Enter your C compiler and linker debug flags [$TCL_DBGSTRING] \c"
    read A
    if [ "$A" != "" ]
    then
        TCL_DBGSTRING=$A
    fi
fi

if [ "$TCL_MEM_DEBUG" = "" ]
then
    TCL_MEM_DEBUG="n"
fi
if [ "$TCL_MEM_VALIDATE" = "" ]
then
    TCL_MEM_VALIDATE="n"
fi
echo
echo "Memory debugging enables a special memory allocator that helps to find"
echo "memory region overflows.  It is a small performance impact and should"
echo "be used only when developing new C language commands for TCL"
TCL_MEM_DEBUG=`$GETYN "Do you want memory debugging included?" $TCL_MEM_DEBUG`

if [ "$TCL_MEM_DEBUG" = "y" ]
then
    echo
    echo "Memory validation goes a step beyond memory debugging and"
    echo "validates all memory regions on every allocation and" 
    echo "deallocation.  It is a severe preformance impact and should"
    echo "only be used if memory corruption problems are suspected."
    echo "It may be enabled by default or turned on with the memory"
    echo "command."
    TCL_MEM_VALIDATE=`$GETYN "Do you want memory validation enabled?" $TCL_MEM_VALIDATE`
else
    TCL_MEM_VALIDATE="n"
fi


if [ "$TCL_OPTIMIZE" = "" ]
then
    TCL_OPTIMIZE="y"
    if [ "$TCL_DEBUG" = "y" ]
    then
        TCL_OPTIMIZE="n"
    fi
fi
TCL_OPTIMIZE=`$GETYN "Do you want optimization enabled?" $TCL_OPTIMIZE`

if [ "$TCL_DEBUG" = "y" ]
then
    if [ "$TCL_OPTIMIZE" = "y" ]
    then
        echo "Warning: symbolic debugging may malfunction with optimization selected"
    fi
fi

if [ "$TCL_OPTIMIZE" = "y" ]
then
    if [ "$TCL_OPTSTRING" = "" ]
    then
        TCL_OPTSTRING="-O"
    fi
    echo "Enter your C compiler optimization flags [$TCL_OPTSTRING] \c"
    read A
    if [ "$A" != "" ]
    then
        TCL_OPTSTRING=$A
    fi
fi

if [ "$TCL_PROFILING" = "" ]
then
    TCL_PROFILING="n"
fi
TCL_PROFILING=`$GETYN "Do you want profiling enabled" $TCL_PROFILING`

if [ "$TCL_PROFILING" = "y" ]
then
    if [ "$TCL_PROFSTRING" = "" ]
    then
        TCL_PROFSTRING="-p"
    fi
    echo "Enter your C compiler profiling flags [$TCL_PROFSTRING] \c"
    read A
    if [ "$A" != "" ]
    then
        TCL_PROFSTRING=$A
    fi
fi

echo
echo creating $CONFIG
echo "# generated by Configure script -- run Configure to alter" >$CONFIG
echo "CUSTOM_NAME=\""$CUSTOM_NAME\" >>$CONFIG
echo "CUSTOM_VERSION="\"$CUSTOM_VERSION\" >>$CONFIG
echo "CUSTOM_LONGNAME="\"$CUSTOM_LONGNAME\" >>$CONFIG
echo "TCL_SOURCEDIR="\"$TCL_SOURCEDIR\" >>$CONFIG
echo "TCL_INITFILE="\"$TCL_INITFILE\" >>$CONFIG
echo "UNIX_TYPE="\"$UNIX_TYPE\" >>$CONFIG
echo "PACKAGE_TCL_SOURCEDIR=\""$PACKAGE_TCL_SOURCEDIR\" >>$CONFIG
echo "TCL_BINDIR=\""$TCL_BINDIR\" >>$CONFIG
echo "TCL_LIBDIR="\"$TCL_LIBDIR\" >>$CONFIG
echo "TCL_HISTORY=\""$TCL_HISTORY\" >>$CONFIG
echo "TCL_OPTIMIZE=\""$TCL_OPTIMIZE\" >>$CONFIG
echo "TCL_OPTSTRING=\""$TCL_OPTSTRING\" >>$CONFIG
echo "TCL_PROFILING=\""$TCL_PROFILING\" >>$CONFIG
echo "TCL_PROFSTRING=\""$TCL_PROFSTRING\" >>$CONFIG
echo "TCL_DEBUG=\""$TCL_DEBUG\" >>$CONFIG
echo "TCL_DBGSTRING=\""$TCL_DBGSTRING\" >>$CONFIG
echo "TCL_MEM_DEBUG=\""$TCL_MEM_DEBUG\" >>$CONFIG
echo "TCL_MEM_VALIDATE=\""$TCL_MEM_VALIDATE\" >>$CONFIG
echo "TCL_DEFAULTFILE=\""$TCL_DEFAULTFILE\" >>$CONFIG
echo "HAS_STRERROR=\""$HAS_STRERROR\" >>$CONFIG

echo creating $CONFIG_H
echo "/* generated by Configure script -- don't edit by hand */" >$CONFIG_H
echo "#define CUSTOM_NAME \""$CUSTOM_NAME\" >>$CONFIG_H
echo "#define CUSTOM_LONGNAME "\"$CUSTOM_LONGNAME\" >>$CONFIG_H
echo "#define TCL_INITFILE "\"$TCL_INITFILE\" >>$CONFIG_H
echo "#define TCL_VERSION "\"$TCL_VERSION\" >>$CONFIG_H
echo "#define TCL_DEFAULTFILE "\"$TCL_DEFAULTFILE\" >>$CONFIG_H

TCLDEFAULTS=TclDefaults
echo creating $TCLDEFAULTS
echo "# Temporary Tcl defaults, automatically generated by the" >$TCLDEFAULTS
echo "# Configure script on `date` so Tcl can run" >>$TCLDEFAULTS
echo "# before being installed.  \"make install\" will build" >>$TCLDEFAULTS
echo "# the real one in $TCL_DEFAULTFILE if necessary" >>$TCLDEFAULTS
echo >>$TCLDEFAULTS
echo "# colon or space-separated Tcl source path is this or TCLPATH env var" >>$TCLDEFAULTS
echo "set_default TCLPATH $PACKAGE_TCL_SOURCEDIR" >>$TCLDEFAULTS
echo >>$TCLDEFAULTS
echo "# Tcl init file is this or TCLINIT env var" >>$TCLDEFAULTS
echo "set_default TCLINIT $PACKAGE_TCL_SOURCEDIR/$TCL_INITFILE" >>$TCLDEFAULTS
echo >>$TCLDEFAULTS

# Build up variables containing all the Makefile macros to merge with the
# Makefiles.  First do the system dependant values.  Default some first.

MF_RANLIB="RANLIB=true"
VOID="-DVOID=void"
MF_LINKFLAGS="LINKFLAGS="

case "$UNIX_TYPE" in
    sysv3.2.0) MF_SYSDEF="SYSDEF= -DSYSV -DSYSV_3_2_0";
	       SYSV=y;
               MF_LIBS="LIBS=-lc_s -lform";
               MF_OS_OBJS="ndir.o strerror.o strstr.o strtoul.o panic.o";;

    sysv3.2.2) MF_SYSDEF="SYSDEF= -DSYSV -DSYSV_3_2_2";
	       SYSV=y;
               MF_LIBS="LIBS=-lc_s ";
               MF_OS_OBJS="strtoul.o panic.o";;

          bsd) MF_SYSDEF="SYSDEF= -DBSD";
               MF_RANLIB="RANLIB=ranlib";
               MF_OS_OBJS="strtol.o strtoul.o strstr.o putenv.o panic.o ascftime.o";;
         hpux) MF_SYSDEF="SYSDEF= -DSYSV -DHPUX";
               MF_LIBS="LIBS=-lBSD -lPW";
               MF_OS_OBJS="panic.o";;

     xenix286) MF_SYSDEF="SYSDEF= -DSYSV -DXENIX -DXENIX286 -Ml";
               VOID="-DVOID=void";
               MF_RANLIB="RANLIB=ranlib"
               MF_LINKFLAGS="LINKFLAGS= -F 8800";
               MF_LIBS="LIBS=";
               MF_OS_OBJS="ndir.o strerror.o strstr.o strtoul.o strtol.o dup2.o putenv.o panic.o ascftime.o";;

     xenix386) MF_SYSDEF="SYSDEF= -DSYSV -DXENIX -DXENIX386";
               VOID="-DVOID=void";
               MF_RANLIB="RANLIB=ranlib"
               MF_LIBS="LIBS=-ldir";
               MF_OS_OBJS="strstr.o strtoul.o panic.o ascftime.o";;
esac

if [ "$UNIX_TYPE" = "bsd" -a "$HAS_STRERROR" = "n" ]
then
 MF_OS_OBJS="$MF_OS_OBJS strerror.o"
fi


MF_COMPOPTS="COMPOPTS= $VOID"  

if [ "$TCL_HISTORY" = "n" ]
then
    MF_COMPOPTS="$MF_COMPOPTS -DNOHISTORY"
fi

if [ "$TCL_OPTIMIZE" = "y" ]
then
    MF_COMPOPTS="$MF_COMPOPTS $TCL_OPTSTRING"
fi

if [ "$TCL_PROFILING" = "y" ]
then
    MF_COMPOPTS="$MF_COMPOPTS $TCL_PROFSTRING"
fi

MF_MCSCMD="MCS=true"

if [ "$TCL_DEBUG" = "y" -o "$TCL_PROFILING" = "y" ]
then
    MF_COMPOPTS="$MF_COMPOPTS $TCL_DBGSTRING"
    MF_STRIPCMD="STRIP=true"
else
    MF_STRIPCMD="STRIP=strip"
    if [ "$SYSV" = "y" ]
    then
	MF_MCSCMD="MCS=mcs -d"
    fi
fi

if [ "$TCL_MEM_DEBUG" = "y" ]
then
    MF_COMPOPTS="$MF_COMPOPTS -DMEM_DEBUG"
fi

if [ "$TCL_MEM_VALIDATE" = "y" ]
then
    MF_COMPOPTS="$MF_COMPOPTS -DMEM_VALIDATE"
fi

MF_CFLAGS="CFLAGS= -I../.. \${SYSDEF} \${COMPOPTS}"
dbar="#======================================================================="
bar="#-----------------------------------------------------------------------"
# Now build the make files.

BASE_SRC_MAKEFILE=baseline/src/Makefile
echo extracting $BASE_SRC_MAKEFILE
$OUTMACROS $BASE_SRC_MAKEFILE
cat baseline/src/Makefile.t >>$BASE_SRC_MAKEFILE


BASE_TEST_MAKEFILE=baseline/tests/Makefile
echo extracting $BASE_TEST_MAKEFILE
$OUTMACROS $BASE_TEST_MAKEFILE
cat baseline/tests/Makefile.t >>$BASE_TEST_MAKEFILE

EXTEND_SRC_MAKEFILE=extend/src/Makefile
echo extracting $EXTEND_SRC_MAKEFILE
$OUTMACROS $EXTEND_SRC_MAKEFILE
cat extend/src/Makefile.t >>$EXTEND_SRC_MAKEFILE

TCLSH_SRC_MAKEFILE=tclsh/src/Makefile
echo extracting $TCLSH_SRC_MAKEFILE
$OUTMACROS $TCLSH_SRC_MAKEFILE
cat tclsh/src/Makefile.t >>$TCLSH_SRC_MAKEFILE

OSSUP_SRC_MAKEFILE=ossupport/src/Makefile
echo extracting $OSSUP_SRC_MAKEFILE
$OUTMACROS $OSSUP_SRC_MAKEFILE
echo "OBJS=$MF_OS_OBJS" >>$OSSUP_SRC_MAKEFILE
cat ossupport/src/Makefile.t >>$OSSUP_SRC_MAKEFILE

echo extracting top-level Makefile
cat Makefile.t >Makefile

echo "install:	all" >>Makefile

if [ "$TCL_BINDIR" != "$CWD" ]
then
    echo "	-cp tcl $TCL_BINDIR" >> Makefile
    echo "	-chmod a+rx,go-w $TCL_BINDIR/tcl" >> Makefile
fi

ES4=$ES3
if [ "$TCL_SOURCEDIR" != "$PACKAGE_TCL_SOURCEDIR" ]
then
    echo "	-rm -rf $TCL_SOURCEDIR/help" >> Makefile
    echo "	-mkdir $TCL_SOURCEDIR/help" >> Makefile
    echo "	-./tcl -f tclsh/tclsrc/copydir.tcl tclsh/tclsrc $TCL_SOURCEDIR" >> Makefile
    echo "	-rm -f $TCL_SOURCEDIR/*.ndx" >> Makefile
    echo "	-./tcl \"source tclsh/tclsrc/packages.tcl; set TCL_PATHLIST $TCL_SOURCEDIR; build_package_indexes\"" >> Makefile
    echo "	-find $TCL_SOURCEDIR -type f -exec chmod go-w,a+r {} \;" >>Makefile
    echo "	-find $TCL_SOURCEDIR -type d -exec chmod go-w,a+rx {} \;" >>Makefile
fi

if [ "$TCL_LIBDIR" != "$PKG_TCL_LIBDIR" ]
then
    echo "	-cp tcl.a $TCL_LIBDIR" >> Makefile
    echo "	-chmod go-w,a+r $TCL_LIBDIR/tcl.a" >> Makefile
fi
echo "	-./tcl -f tclsh/tclsrc/gen_dfault.tcl $TCL_DEFAULTFILE $TCL_SOURCEDIR $TCL_SOURCEDIR/$TCL_INITFILE" >> Makefile

/bin/rm -f $GETYN $OUTMACROS
echo
echo "configuration of TCL complete --"
echo
echo "Do a 'make' and hopefully it will build up."
echo
echo "'make bldhelp', once TCL is working, will extract the help files"
echo "from the manual pages."
echo
echo "'make install' after a successful make ought to install various"
echo "things in the selected directories, if you selected directories"
echo "other than the ones already within the tcl source hierarchy."
echo
