#!/bin/sh
#
# configure script for the ColdX driver
#
# this does not currently test too much, will expand upon it over time
# as we learn what we must check for.  Chunks of this have been stripped
# from such exemplary sources as the perl distribution, emacs configure
# and that which is produced with autoconf.  I didn't use autoconf
# because it is much more fun to just write your own.
#
# -Brandon Gillespie

trap "rm -f test.c a.out core $me.tmp; exit" 0 2 3

# because I'm a sucker for formatting.
t="	"
i="- "

###########################################################################
# parse options

BASE=${CONFIG_DIR}
if [ -z "$BASE" -o -d "$BASE" ]; then
    BASE=`pwd`
fi
ETC=${BASE}/etc
BIN=${BASE}/bin
DOC=${BASE}/doc
LOG=${BASE}/log
TEST=${BASE}/test
SRC=${BASE}/src
WORLD=${BASE}/world

me=`basename $0`
conf="${BASE}/etc/${me}.prev"
fail=""
quick=""
prev=""
no_prev=""
MAKEFILE="yes"
SETVER=""
VERSION=`cat ${ETC}/version`
NAME="ColdX-${VERSION}"

while [ ! -z "$1" ]; do
        if [ `echo $1 | cut -c1` != "-" ]; then
                break
        fi

        case $1 in
        "-p")
                no_prev="yes"
                ;;
        "-q")
                quick="yes"
                ;;
        "-m")
                MAKEFILE=""
                ;;
        "-v")
                SETVER="yes"
                ;;
        "-h")
		echo "-- ${NAME} --

${t}'$me [options] [relative makefile directory]'

Options:
${t}-p${t}-- ignore any existing preferences file
${t}-q${t}-- be quick (do not say much, skip the non important questions)
${t}-a${t}-- do not generate a makefile
${t}-h${t}-- this usage/help description

For instance, \"$me -m -p\" will create a new preferences file, but
will not generate a makefile.
"
                exit
        esac

	shift
done

###########################################################################
# Defaults

LIBS='-lm'
YACC='yacc'
CC=''
# default OPT flags are defined where it figures CC
OPT=''
CFLAGS=''
INCLUDEDIRS=''

###########################################################################
echo "-- ${NAME} --

${t}Configure script for the ColdX driver.

${t}Use '$me -h' for help
"

(echo "hi there\c" ; echo " ") > $me.tmp
if `grep c $me.tmp >/dev/null 2>&1`; then
	n='-n'
	supress=$n
	c=''
else
	n=''
	supress="\\\c"
	c='\c'
fi

if [ -z "$quick" ]; then
	echo "${i}Supressing newlines with $supress..."
	echo ''
	echo $n "${i}The asterisk should be here => $c"
	echo '*'
	echo ''
fi

rm -f $me.tmp

###########################################################################
# Change the version number

if [ ! -z "$SETVER" ]; then
        echo "Old version was: ${VERSION}"
        echo
        echo $n "Enter new version: $c"
        read NEWVER
        while [ -z "$NEWVER" ]; do
                echo "Invalid version, try again."
                echo $n "Enter new version: $c"
                read NEWVER
        done

        OLDVER=$VERSION
        VERSION=$NEWVER
        echo ${VERSION} > ${ETC}/version
        echo
        echo "${i}New version set."
        echo
        echo "${i}Adjusting directory heirarchy..."
        NEWDIR=`dirname ${BASE}`/ColdX-${VERSION}
        echo "${i}${BASE}"
	echo "${i} => ${NEWDIR}"
        mv ${BASE} ${NEWDIR}
        echo

	# exit because all the paths will be wrong now
	configure -p
	exit
fi

###########################################################################
# check previous config ?

# does the prev config file exist
if [ -f $conf ]; then
	prev="yes"
fi

if [ "$prev" = "yes" -a -z "$no_prev" ]; then
	if [ -z "$quick" ]; then
		echo "${i}Reading previous configuration..."
	fi
else

###########################################################################
# figure out the OS

SYS=`(/bin/uname || /usr/bin/uname) 2>/dev/null`
SYSTYPE="SYSV"

# this isn't the best way to do this, we should really be checking for
# most of the following piece by piece, I need to find out exactly what
# /bin/uname returns in:
#
#    SGI Irix
#    NextStep

case $SYS in
SunOS)
	# check for Solaris the hard way
	if [ "`/bin/uname -rs | cut -c0-9`" != "SunOS 4.1" ]; then
		SYS="Solaris"
		LIBS="-lsocket -lnsl -lelf ${LIBS}"
	else
		echo "${t}You should read the file 'docs/README.src' (find the secion on
${t}SunOS 1.4.x).  There are some changes required to the source,
${t}for this operating system."
		SYSTYPE="BSD"
	fi
	;;
HP-UX)
	LIBS="-ldbm ${LIBS}"
	CFLAGS="-Aa ${CFLAGS}"
	;;
AIX)
	LIBS="-lbsd ${LIBS}"
	;;
Linux)
	LIBS="-ldbm ${LIBS}"
	;;
ULTRIX)
	SYSTYPE="BSD"
	;;
IRIX)
	LIBS="-lsun ${LIBS}"
	CFLAGS="-mips2 ${CFLAGS}"
	;;
*)
	fail="yes"
        if [ -r /unix ]; then
                SYS2=`(strings /unix || strings /vmunix) | grep -i unixware`
                if [ -n $SYS2 ]; then
                        fail=""
			SYS="Unixware"
			LIBS="-lnsl -lsocket -lresolv ${LIBS}"
                fi
        fi
	;;
esac

CFLAGS="-D${SYS} -D${SYSTYPE} ${CFLAGS}"

if [ ! -z "$fail" ]; then
	echo "${t}Note: your operating system (${SYS}) is not one of the
${t}few we check for.  This is not neccessarily bad, but it may mean
${t}that a few compiler flags are set incorrectly."
else
	echo "${i}Looks like you are running ${SYS}"
fi

############################################################################
# compiler
echo ""
echo $n "${i}Checking for cc or gcc...$c"

cat > test.c <<EOF
int main() { exit(0); }
EOF
err=`eval "(gcc test.c >/dev/null) 2>&1"`
rm -f test.c a.out

if [ X"$err" != X ]; then
	echo $n "using cc"
	OPT='-O'
	CC=cc
else
	echo $n "using gcc"
	OPT='-O2'
	CC=gcc
fi

###########################################################################
# vfork()

echo ''
echo $n "${i}Checking for vfork()...$c"

cat > test.c <<EOF
/* extern pid_t vfork(void); */
main () { vfork(); }
EOF

err=`eval "($CC test.c >/dev/null) 2>&1"`
if [ ! -z "$err" ]; then
        USE_VFORK=""
        echo "not found."
else
        USE_VFORK="yes"
        echo "found."
fi

rm -f test.c a.out

###########################################################################
echo ''
echo $n "Which optimization/debug state to use? (default: ${OPT}) $c"
read ANS

if [ X$ANS != X ]; then
	OPT=$ANS
fi

###########################################################################
echo $n "\nList any system specific include directories you would like searched
(use the -I/path syntax): $c"

read ANSWER
INCLUDEDIRS=$ANSWER

###########################################################################
echo ''
echo $n "Which Compiler Compiler to use? (default: ${YACC}) $c"
read ANS

if [ X$ANS != X ]; then
	YACC=$ANS
fi

###########################################################################

CHECK_LOAD=""
USE_GETRUSAGE=""

if [ -z "$quick" ]; then

echo $n "
${t}The remaining questions relate to non-system specific configuration
${t}values in the server (such as using FLOATS or not).  Skipping this
${t}section will simply use the defaults, which currently are:

${t}${t}CHECK_LOAD=no
${t}${t}USE_GETRUSAGE=no

Do you wish to skip this section? (default: no): $c"

read ANS

if [ -z "$ANS" -o \( "$ANS" = "no" \) -o \( "$ANS" = "n" \) ]; then 
	echo $n "
${t}System load is reported by executing the command \"uptime\"
${t}and parsing the returned string.  This can cause blocked pipes and
${t}freeze the driver (if the filesystem is slow in responding).  If
${t}load checking is off, the routine \"load()\" will return an
${t}empty list.

Do you want to check system load? (default: no): $c"

	read ANS
	if [ ! -z "$ANS" -a \( "$ANS" = "yes" \) -o \( "$ANS" = "y" \) ]; then
	    CHECK_LOAD="yes"
	fi

	echo $n "
${t}The function \"getrusage()\" is used by the routine \"status()\".
${t}Since the values returned by this function vary from architecture
${t}to architecture, be warned that it may cause problems on yours.
${t}If you do not use it, \"status()\" will return an emtpy list.

Do you wish to use \"getrusage() in \"status()\"? (default: no): $c"

	read ANS
	if [ ! -z "$ANS" -a \( "$ANS" = "y" \) -o \( "$ANS" = "yes" \) ]; then
            USE_GETRUSAGE="yes"
	fi
fi
fi

###########################################################################

echo
echo $n "${i}Building defaults file...$c"

echo "Makefile1=\$1" > $conf
echo "Makefile2=\$2" >> $conf
echo "config=\$3" >> $conf
echo "chead=\$\$.chead" >> $conf
echo "shead=\$\$.shead" >> $conf
echo "tmp=\$\$.tmp" >> $conf
echo "trap \"rm -f \$tmp \$chead \$shead; exit\" 0 2 3" >> $conf

# build the two file headers (C and sh headers)
echo "echo \"/* This file generated by \\\"$me\\\" on \`date\`. */\" > \$chead" >> $conf
echo "echo \"/* Make changes to the config.h.in file and run the configure script.  */\" >> \$chead" >> $conf
echo "echo '' >> \$chead" >> $conf
echo "echo \"# This file generated by \\\"$me\\\" on \`date\`.\" > \$shead" >> $conf
echo "echo \"# Make changes to the Makefile.in file and run the configure script.\" >> \$shead" >> $conf
echo "echo '' >> \$shead" >> $conf

sline="s:^#@@LIBS@:LIBS=${LIBS}:g"
sline="${sline};s:^#@@CC@:CC=${CC}:g"
sline="${sline};s:^#@@VERSION@:VERSION=${VERSION}:g"
sline="${sline};s:^#@@YACC@:YACC=${YACC}:g"
sline="${sline};s:^#@@CFLAGS@:CFLAGS=${CFLAGS} ${OPT}:g"
sline="${sline};s:^#@@INCLUDEDIRS@:INCLUDEDIRS=${INCLUDEDIRS}:g"
sline="${sline};s:^#@@BINDIR@:BINDIR=${BIN}:g"
sline="${sline};s:^#@@BASEDIR@:BASEDIR=${BASE}:g"
sline="${sline};s:^#@@TESTDIR@:TESTDIR=${TEST}:g"
sline="${sline};s:^#@@ETCDIR@:ETCDIR=${ETC}:g"
sline="${sline};s:^#@@SRCDIR@:SRCDIR=${SRC}:g"

echo "chmod u+w \$Makefile1 \$Makefile2" >> $conf
echo "sed '${sline}' \${Makefile1}.in > \$tmp" >> $conf
echo "cat \$shead \$tmp > \$Makefile1" >> $conf
echo "sed '${sline}' \${Makefile2}.in > \$tmp" >> $conf
echo "cat \$shead \$tmp > \$Makefile2" >> $conf
echo "chmod a-w \$Makefile1 \$Makefile2" >> $conf

# this gets ugly, please remove women and children from the room at this time

MAJORV=`echo ${VERSION} | cut -d'.' -f1`
MINORV=`echo ${VERSION} | cut -d'.' -f2 | cut -d'-' -f1`
PATCHV=`echo ${VERSION} | cut -d'-' -f2`

sline="s:^/\* @@VERSION_MAJOR@@ \*/.*:#define VERSION_MAJOR ${MAJORV}:g"
sline="${sline};s:^/\* @@VERSION_MINOR@@ \*/.*:#define VERSION_MINOR ${MINORV}:g"
sline="${sline};s:^/\* @@VERSION_PATCH@@ \*/.*:#define VERSION_PATCH ${PATCHV}:g"

if [ ! -z "$USE_VFORK" ]; then
	sline="${sline};s:^/\* @@USE_VFORK@@ \*/.*:#define USE_VFORK:g"
fi

if [ ! -z "$CHECK_LOAD" ]; then
	sline="${sline};s:^/\* @@CHECK_LOAD@@ \*/.*:#define CHECK_LOAD:g"
fi

if [ ! -z "$USE_GETRUSAGE" ]; then
	sline="${sline};s:^/\* @@USE_GETRUSAGE@@ \*/.*:#define USE_GETRUSAGE:g"
fi

echo "chmod u+w \$config" >> $conf
echo "sed '${sline}' \${config}.in > \$tmp" >> $conf
echo "cat \$chead \$tmp > \$config" >> $conf
echo "chmod a-w \$config" >> $conf

echo "rm -f \$chead \$shead \$tmp" >> $conf

echo "Done."

#
# This 'fi' is from the first if (checking previous config)
#

fi

###########################################################################

if [ ! -z "$MAKEFILE" ]; then
	Makefile1=${BASE}/Makefile
	Makefile2=${SRC}/Makefile
	config=${SRC}/config.h

	echo $n "${i}Building Makefiles and config.h...$c"

	sh $conf $Makefile1 $Makefile2 $config

	echo "Done."

	echo $n "${i}Building dependancies...$c"
	cd ${SRC}
	make depend >/dev/null 2>&1
	echo "Done."
	cd ${BASE}

	# make depend creates a Makefile.bak which we can hose
	rm -f ${Makefile1}.bak ${Makefile2}.bak

fi