#!/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 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 ..


#
# 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 ..


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,@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,
" < 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
