#!/bin/sh
#
#	Configuration script for mSQL.  This calls an autoconf generated
#	script for most of the hard work but does a few other checks
#	itself.
#						bambi


# Is this a Posix or bsd-ish shell?

if echo '\c' | grep -s c >/dev/null 2>&1
then
	ECHO_N="echo -n"
	ECHO_C=""
else
	ECHO_N="echo"
	ECHO_C='\c'
fi

echo
echo "Starting configuration of mSQL 2."
echo 

cp conf/site.mm.in .
chmod u+w site.mm.in

. conf/configure

if test "$LEX." = "flex."
then
	FLEX="1"
else
	FLEX="0"
fi


#
# Work out how to do a readdir().  Could be Posix dirent or direct.
# Could need dirent.h, sys/dir.h or even both!  (God help us).
#

echo
$ECHO_N "checking your directory stuff. $ECHO_C"
cd conf
	DIRENT=""
	# Try Posix first
	rm -f dirent
	DEFS="-DHAVE_DIRENT_H -DHAVE_DIRENT"
	${CC-cc} -I../ -o dirent $DEFS dirent.c >/dev/null 2>&1
	if test $? = 0
	then
		DIRENT=$DEFS
		echo " Using dirent.h and struct dirent"
	fi

	# Try old style
	if test "$DIRENT." = "."
	then
		rm -f dirent
		DEFS="-DHAVE_SYS_DIR_H -DHAVE_DIRECT"
		${CC-cc} -I../ -o dirent $DEFS dirent.c >/dev/null 2>&1
		if test $? = 0
		then
			DIRENT=$DEFS
			echo " Using sys/dir.h and struct direct"
		fi
	fi

	# O.K. Time to get silly
	if test "$DIRENT." = "."
	then
		rm -f dirent
		DEFS="-DHAVE_DIRENT_H -DHAVE_SYS_DIR_H -DHAVE_DIRECT"
		${CC-cc} -I../ -o dirent $DEFS dirent.c >/dev/null 2>&1
		if test $? = 0
		then
			DIRENT=$DEFS
			echo " Using sys/dir.h, dirent.h and struct direct"
		fi
	fi

	if test "$DIRENT." = "."
	then
		echo
		echo
		echo "Hang on!  Could't work out your directory routines"
		echo "Check out conf/dirent.c and see what you need defined"
		echo "to make it compile.  When you've worked that out,"
		echo "rerun this script and enter the details at the prompt"
		echo "offered below (e.g. -DHAVE_DIRENT -DHAVE_DIRENT_H) :-"
		echo 
		$ECHO_N "Defines for directory stuff? "
		read DIRENT
	fi



rm -f dirent
cd ..


#
# mmap() test.
#

$ECHO_N "checking your mmap(). $ECHO_C"

cd conf
${CC-cc} -I../ -o mmap mmap.c >/dev/null 2>&1
if test $? != 0
then
	MMAP=
	echo " You don't have mmap()"
	echo
	echo "mSQL 2 needs a working mmap(), it cannot work without it."
	echo "You cannot run mSQL 2 on this machine.  Sorry."
	echo
	exit 1
else
	./mmap
	if test $? != 0
	then
		echo " Your mmap() isn't good enough !!!!"
		echo
		echo "mSQL 2 needs a working mmap(), it cannot work without it."
		echo "You cannot run mSQL 2 on this machine.  Sorry."
		echo
		exit 1
	else
		echo " Your mmap() is fine."
		MMAP="-DHAVE_MMAP"
	fi
fi
rm -f mmap
cd ..


#
# Work out how many args msync() takes on this box
#

$ECHO_N "checking your msync(). $ECHO_C"
cd conf
	MSYNC=""
	# Try 2 args first
	rm -f dirent
	DEFS="-DMSYNC_2"
	${CC-cc} -I../ -o msync $DEFS msync.c >/dev/null 2>&1
	if test $? = 0
	then
		MSYNC=$DEFS
		echo " Using 2 args to msync."
	else
		MSYNC="-DMSYNC_3"
		echo " Using 3 args to msync."
	fi
	rm -f msync
cd ..


#
# syserr test.
#

$ECHO_N "checking for sys_errlist. $ECHO_C"

cd conf
${CC-cc} -I../ -o syserr syserr.c >/dev/null 2>&1
if test $? != 0
then
	SYS_ERR=
	echo " You don't have sys_errlist in stdio.h or sys/types.h"
else
	SYS_ERR="-DHAVE_SYS_ERRLIST"
	echo " You're fine."
fi
cd ..

#
# u_int test.
#

$ECHO_N "checking for u_int. $ECHO_C"

cd conf
${CC-cc} -I../ -o uint uint.c >/dev/null 2>&1
if test $? != 0
then
	U_INT=
	echo " You don't have u_int in sys/types.h"
else
	U_INT="-DHAVE_U_INT"
	echo " You're fine."
fi
cd ..

#
# uint32_t test.
#

$ECHO_N "checking for int32_t and friends. $ECHO_C"

cd conf
${CC-cc} -I../ -o uint32 uint32.c >/dev/null 2>&1
if test $? != 0
then
	BITTYPES=
	echo " You don't have the bit types."
else
	BITTYPES="-DHAVE_BIT_TYPES"
	echo " You're fine."
fi
cd ..


#
# ssize_t test.
#

$ECHO_N "checking for ssize_t. $ECHO_C"

cd conf
${CC-cc} -I../ -o ssize ssize.c >/dev/null 2>&1
if test $? != 0
then
	SSIZE_T=
	echo " You don't have ssize_t in sys/types.h or unistd.h"
else
	SSIZE_T="-DHAVE_SSIZE_T"
	echo " You're fine."
fi
cd ..


#
# RLIMIT_NOFILE test.
#

$ECHO_N "checking for a working getrlimit. $ECHO_C"

cd conf
${CC-cc} -I../ -o rlimit rlimit.c >/dev/null 2>&1
if test $? != 0
then
	WORKING_RLIMIT=
	echo " Nope! (can't find RLIMIT_NOFILE)"
else
	WORKING_RLIMIT="-DHAVE_RLIMIT_NOFILE"
	echo " You're fine."
fi
cd ..

#
# See struct tm has the dst field
#

$ECHO_N "checking for day light saving info. $ECHO_C"

cd conf
${CC-cc} -I../ -o dst dst.c >/dev/null 2>&1
if test $? != 0
then
	HAVE_ISDST=
	echo " Nope!"
else
	HAVE_ISDST="-DHAVE_ISDST"
	echo " Yup, we'll use it."
fi
cd ..

#
# See if this is an HP-UX box in which case we must not call ranlib on
# archive files (unles we use the -z flag to ar) as reported by Eric
# Schwartz <erics@col.hp.com>
#

$ECHO_N "checking for HP-UX ranlib. $ECHO_C"
OS=`uname -s`
if test "$OS." = "HP-UX."
then
	RAN_TO="= true"
	echo "Yup, it's HP-UX.  Won't use ranlib."
else
	RAN_TO="= ranlib"
	echo "Nope, it's OK to use ranlib."
fi


#
# See if this is a Linux box in which case we must add -rdynamic to the
# link options to allow dynamic loaded Lite modules to access symbols
# in the Lite binary itself.
#

$ECHO_N "checking for Linux. $ECHO_C"
OS=`uname -s`
if test "$OS." = "Linux."
then
	LINUX_DYNAMIC="-rdynamic"
	MSYNC="-DMSYNC_3"
	echo "Yup, it's Linux.  Adding -rdynamic to the link flags."
	echo "Also forcing msync for Linux."
else
	LINUX_DYNAMIC=""
	echo "Nope, use normal linking."
fi



TARGET=`cat conf/target`
mv site.mm site.mm.$$
sed "	s,@INST_TOP@,$INST_TOP,
	s,@FLEX@,$FLEX,
	s,@SIGLIST@,$SIGLIST,
	s,@DIRENT@,$DIRENT,
	s,@MMAP@,$MMAP,
	s,@MSYNC@,$MSYNC,
	s,@U_INT@,$U_INT,
	s,@SSIZE_T@,$SSIZE_T,
	s,@BITTYPES@,$BITTYPES,
	s,@ROOT_EXEC@,$ROOT_EXEC,
	s,@ROOT@,$ROOT,
	s,@PID_DIR@,$PID_DIR,
	s,@SYS_ERR@,$SYS_ERR,
	s,@TARGET@,$TARGET,
	s,@WORKING_RLIMIT@,$WORKING_RLIMIT,
	s,@LINUX_DYNAMIC@,$LINUX_DYNAMIC,
	s,@HAVE_ISDST@,$HAVE_ISDST,
	s,= ranlib,$RAN_TO,
" < site.mm.$$ > site.mm
rm site.mm.$$

echo
echo "Ready to build mSQL."
echo
echo
echo
echo "You may wish to check \"site.mm\" although the defaults should be"
echo "fine.  When you're ready, type  \"make all\" to build the software"
echo
echo
echo
echo
