#!/bin/sh


# 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 Checking for gcc version 2 or better
rm -f gcc_ver
gcc --version > gcc_ver 2>&1
grep "^2\." gcc_ver > /dev/null 2>&1
if test $? = 0
then
	echo You have GCC version 2
	GCC2=1
else
	echo Could not find GCC version 2
	GCC2=0
fi

SYS=`uname -s`


case $SYS in
	"SunOS")
		if test -f /vmunix
		then
			# Should be "real" SunOS
			echo "Looks like a SunOS box"
			if test $GCC2 = 1
			then
				PIC="-fpic"
			else
				PIC="-pic"
			fi
			LDFLAG="-assert pure-text"
			OS="SunOS"
			
		else
			# Hmmm, probably Solaris
			echo "Looks like a Solaris box"
			if test $GCC2 = 1
			then
				PIC="-fpic"
			else
				PIC="-Kpic"
			fi
			LDFLAG="-r"
			OS="Solaris"
		fi
		;;

	"SVR4")
		# Not tested!
		echo "Looks like a SVR4 box"
		LDFLAG="-G -z text"
		;;

	"SVR3")
		# Not tested!
		echo "Looks like a SVR3 box"
		LDFLAG="-q"
		;;

	"Linux")
		echo "Looks like a Linux box"
		PIC="-fpic"
		LDFLAG="-shared"
		OS="Linux"
		;;

	"HP-UX")
		# Not tested!
		echo "Looks like an HP-UX box"
		LDFLAG="-b"
		;;

	"BSD/OS")
		echo "Looks like a BSDI box"
		DLCC="shlicc2"
		LDFLAG="-r"
		OS="BSD/OS"
		;;

	"FreeBSD")
		echo "Looks like a FreeBSD box"
		PIC="-fPIC"
		LDFLAG="-Bshareable"
		OS="FreeBSD"
		;;
esac

if test "$OS." = "."
then
	echo
	echo "WARNING : This is an untested operating system"
	echo "          Setup has guessed the compiler and linker options"
	echo "          required to build a shared object.  Contact"
	echo "          bugs@Hughes.com.au if you have problems"
	echo ""
	sleep 5
fi


if test -f gcc_ver
then
	CC=gcc
else
	CC=cc
fi
rm -f gcc_ver
if test "$DLCC." != "."
then
	CC=$DLCC
fi

echo "Looking for -ldl library"
echo "
#include <stdio.h>

main()
{
	int	foo;
}
" > test_$$.c
$CC -o test_$$ test_$$.c -ldl > /dev/null 2>&1
if test $? = 0
then
	DL_LIB=-ldl
	echo "Found -ldl"
else
	echo "Nope, can't find -ldl"
fi
rm -f test_$$*

echo "Looking for the Hughes Technologines installation directory"
if test -f /usr/local/Hughes/msql.conf
then
	INST_DIR="/usr/local/Hughes"
fi
if test -f /usr/Hughes/msql.conf
then
	INST_DIR="/usr/Hughes"
fi
if test -f /opt/Hughes/msql.conf
then
	INST_DIR="/opt/Hughes"
fi
if test -f /usr/local/msql/msql.conf
then
	INST_DIR="/usr/local/msql"
fi
if test -f /usr/msql/msql.conf
then
	INST_DIR="/usr/msql"
fi
if test -f /opt/msql/msql.conf
then
	INST_DIR="/opt/msql"
fi
if test "$INST_DIR." = "."
then
	echo "Can't find the installation directory (usually /usr/local/Hughes)"
	$ECHO_N "Where is mSQL 2 installed? $ECHO_C"
	read INST_DIR
fi
echo "Using $INST_DIR as the installation directory"

if test ! -d $INST_DIR/makegen
then
	echo
	echo "Looks like you have an old version of mSQL installed"
	echo "Dynamic Lite modules need mSQL 2.0 Beta 3 or better"
	echo "Please see http://www.Hughes.com.au for a new mSQL release"
	echo
	exit 1
fi



cp $INST_DIR/makegen/site.mm .
sed "	s,@PIC@,$PIC,
	s,@SHLIB@,$LDFLAG,
	s,@DL_LIB@,$DL_LIB,
" < conf/site.mm.in >> site.mm

#
# Create the module Makefiles - just set the INST_DIR and let makegen
# handle the rest
#
cd src
cp Makefile.in Makefile
echo >> Makefile
echo "INST_DIR= $INST_DIR" >> Makefile
echo >> Makefile
cd ..


#
# Add make rules to the top level Makefile for each module subdirectory
#
echo "all :
	cd src; make all; cd ..

install :
	@if test ! -d $INST_DIR/modules; \\
	then	\\
		mkdir $INST_DIR/modules; \\
	fi
	cd src; make install; cd ..

clean :
	cd src; make clean; cd ..
" > Makefile


#
# We're done.  Just give a bit of user feedback
#
echo
echo "compiling shared objects with '$CC $PIC'"
echo "link shared libraries with 'ld $LDFLAG'"
if test "$DL_LIB." = "."
then
	echo "Not linking app's against -ldl"
else
	echo "Linking app's against -ldl"
fi
echo
echo "site.mm created."
echo "Makefile created."
echo "Module Makefiles created."
echo

rm -f main a.out *.o
