#!/bin/sh
#
# configure script for the coldmud server.
#
# 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 $0.tmp; exit" 0 2 3

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

VERSION='0.12-1c'
LIBS=''
YACC='bison -y'
YFLAGS='-d'
CC=''
# default OPT flags are defined where it figures CC
OPT=''
CFLAGS=''

conf="etc/$0.defaults"
# because I'm a sucker for formatting.
t="	"
i="- "


###########################################################################
echo "-- ColdMUD ${VERSION} --

${t}Configure script for the ColdMUD driver.

${t}To rebuild the Makefile using previous options (in the instance that
${t}Makefile.in was changed), use the \"-p\" (previous) flag, \"make
${t}makefile\" will also execute \"$0 -p\".

${t}If this script has problems on your local system, please email
${t}brandon@paradise.declab.usu.edu with what the problem was.
"
# figure out what to do with echo -- from perl's Configure

(echo "hi there\c" ; echo " ") > $0.tmp
if grep c echotmp >/dev/null 2>&1 ; then
	echo "${i}Supressing newlines with -n..."
	n='-n'
	c=''
else
	echo "${i}Supressing newlines with \\\c..."
	n=''
	c='\c'
fi
echo ''
echo $n "${i}The asterisk should be here => $c"
echo '*'
echo ''

if [ X$1 != X -a X$1 = X"-p" ]; then
	echo "${i}Reading previous configuration..."
	if [ ! -f "$conf" ]; then
                echo ''
		echo "${t}Previous configuration file ($conf) not found!"
		exit 1
	fi
else

###########################################################################
SYS=`(/bin/uname || /usr/bin/uname) 2>/dev/null`

# 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."
	fi
	;;
HP-UX)
	LIBS="-ldbm ${LIBS}"
	CFLAGS="-Aa ${CFLAGS}"
	;;
# The rest are _TOTAL_ guesses, I need to find out what /bin/uname
# returns on these OS's.
AIX)
	LIBS="-lbsd ${LIBS}"
	;;
Linux)
	LIBS="-ldbm ${LIBS}"
	;;
IRIX)
	LIBS="-lsun ${LIBS}"
	CFLAGS="-mips2 ${CFLAGS}"
	;;
  *)
	# this is a hack because Unixware returns odd things with /bin/uname
	echo $n "Are you running Unixware? [yn] (default: n) $c"
	read ANS
	if [ X$ANS = X"y" ]; then
		SYS="Unixware"
		LIBS="-lnsl -lsocket -lresolv ${LIBS}"
	fi
	;;
esac

echo ''
echo "${i}Looks like you are running ${SYS}..."

############################################################################
# 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='-Wall -O2'
	CC=gcc
fi

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

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

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

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

###########################################################################
# # strdup()
# 
# echo ''
# echo $n "${i}Does strdup() exist...$c"
# 
# cat > test.c <<EOF
# #include <string.h>
# main () { char *str, *str2 = "test\n"; str = strdup(str2); }
# EOF
# 
# err=`eval "($CC test.c >/dev/null) 2>&1"`
# if [ ! -z "$err" ]; then
#         NO_STRDUP=1
#         echo "no."
# else
#         NO_STRDUP=0
#         echo "yes."
# fi
# 
# rm -f test.c a.out
# 
###########################################################################
# vfork()

echo ''
echo $n "${i}Does vfork() exist...$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=0
        echo "no."
else
        USE_VFORK=1
        echo "yes."
fi

rm -f test.c a.out

###########################################################################
# This is inefficient to do it seperately for each option

echo "tmp1=\$\$.temp1" > $conf
echo "tmp2=\$\$.temp2" >> $conf
echo "ROOT=\$1" >> $conf
echo "trap \"rm -f \$\$.temp1 \$\$.temp2 \$1.temp; exit\" 0 2 3" >> $conf
echo "FNAME=\${ROOT}/src/Makefile.in" >> $conf

echo "echo - Generating 'src/Makefile' from 'src/Makefile.in'..." >> $conf
# src/Makefile...
echo "sed \"s:^#@@LIBS@:LIBS=${LIBS}:g\" \${FNAME} > \$tmp1" >> $conf
echo "sed \"s:^#@@CC@:CC=${CC}:g\" \$tmp1 > \$tmp2" >> $conf
echo "sed \"s:^#@@VERSION@:VERSION=${VERSION}:g\" \$tmp2 > \$tmp1" >> $conf
echo "sed \"s:^#@@YACC@:YACC=${YACC}:g\" \$tmp1 > \$tmp2" >> $conf
echo "sed \"s:^#@@CFLAGS@:CFLAGS=${CFLAGS} ${OPT}:g\" \$tmp2 > \$tmp1" >> $conf
echo "sed \"s:^#@@YFLAGS@:YFLAGS=${YFLAGS}:g\" \$tmp1 > \$tmp2" >> $conf

# to keep things straight, adjust this if any of the above are removed
# or any additions are made:
echo "FNAME=\$tmp2" >> $conf

echo "echo \"# This file generated by $0 on `date`.\" > \$1.temp" >> $conf
echo "echo \"# Make changes to 'Makefile.in' and run the configure script\" >> \$1.temp" >> $conf
echo "echo \"#\" >> \$1.temp" >> $conf

echo "cat \$1.temp > \${ROOT}/src/Makefile" >> $conf
echo "cat \${FNAME} >> \${ROOT}/src/Makefile" >> $conf

echo "FNAME=\${ROOT}/src/config.h.in" >> $conf

echo "echo - Generating 'src/config.h' from 'src/config.h.in'..." >> $conf
# src/config.h
# if [ $NO_STRDUP = 1 ]; then
# 	echo "sed \"s:^/\* @@NO_STRDUP@ \*/.*:#define NO_STRDUP:g\" \$FNAME > \$tmp1" >> $conf
#         echo "FNAME=\$tmp1" >> $conf
# fi
if [ $USE_VFORK = 1 ]; then
	echo "sed \"s:^/\* @@USE_VFORK@ \*/.*:#define USE_VFORK:g\" \$FNAME > \$tmp2" >> $conf
        echo "FNAME=\$tmp2" >> $conf
fi

echo "echo \"/* config.h generated by configure on `date` */\" > \${ROOT}/src/config.h" >> $conf
echo "cat \${FNAME} >> \${ROOT}/src/config.h" >> $conf

# redundant, but (shrug)
echo "exit" >> $conf

## This 'fi' is from the first if (checking "-d")
fi

echo ''

sh $conf .

echo ''

exit