#!/bin/sh

# genmake
# GENERATE OS-SPECIFIC MINI-MAKEFILES
# IT'S JUST EASIER KEEPING ALL THE INFORMATION IN ONE PLACE 
# Usage: genmake <OS-TYPE-LIST>

# USUALLY JUST SETS CFLAGS BUT CAN ALSO SET THE FOLLOWING
# LIBS SHELL CC MAKE

if test "$#" -gt "0"
then
	OSTYPES=$@
else
	OSTYPES="aix bsdi bsdi4 caldera darwin debian dgux dynix freebsd generic hpux hpux9 irix linux mandrake netbsd openbsd osf redhat sco sco3 sinix solaris sunos suse ultrix unixware unixware7"
fi

# IF gcc IS PRESENT, USE IT
if test -f "/usr/local/bin/gcc"
then
	CC="gcc"
else
	XX=`type gcc` 2>/dev/null
	if test "$XX" = ""
	then
		CC="cc"
	else
		CC="gcc"
	fi
fi

workdir=`echo $0 | sed 's/genmake//g'`

for BBOS in $OSTYPES
do
	BBENV=""
	LIBS=""
	CFLAGS=""

	case $BBOS in
		freebsd|bsdi*|darwin|netbsd|openbsd) echo "Making $BBOS makefile"
			CFLAGS="-DZOMBIE -D${BBOS} -DGETTIMEOFDAY -DREGEXEC -O"
		;;
		dgux) 	echo "Making $BBOS makefile"
			CFLAGS="-D${BBOS} -O -DBZERO -DTIMEH -DREGEXEC"
			LIBS="-lnsl -lsocket" 
		;;
		dynix) 	echo "Making $BBOS makefile"
			CFLAGS="-D${BBOS} -DREGEXEC"
			LIBS="-lgen -lnsl -lsocket" 
		;;
		hpux) 	echo "Making $BBOS makefile"
			CFLAGS="-D${BBOS} -DREGEXEC -DGETTIMEOFDAY"
		;;
		hpux9) 	echo "Making $BBOS makefile"
			CFLAGS="-D${BBOS} -DREGEXEC"
		;;
		irix|generic) 	echo "Making $BBOS makefile"
			CFLAGS="-D${BBOS} -O -DREGEXEC"
		;;
		osf) 	echo "Making $BBOS makefile"
			CFLAGS="-D${BBOS} -O -DREGEXEC -DZOMBIE"
		;;
		sco) 	echo "Making $BBOS makefile"
			CFLAGS="-D${BBOS} -O -DREGEXEC -DTIMEH"
			LIBS="-lsocket"
		;;
		sco3) 	echo "Making $BBOS makefile"
			CFLAGS="-D${BBOS} -O -DREGEX -DTIMEH"
			LIBS="-lsocket"
		;;
		sinix) 	echo "Making $BBOS makefile"
			CFLAGS="-D${BBOS} -O -DBZERO -DSIGSETJMP -DZOMBIE -DREGEXEC"

			LIBS="-lnsl -lsocket -s"
		;;
		solaris) echo "Making $BBOS makefile"
			CFLAGS="-D${BBOS} -O -DBZERO -DGETTIMEOFDAY -DREGEXEC"
			LIBS="-ll -lnsl -lsocket"
		;;
		sunos) echo "Making $BBOS makefile"
			CFLAGS="-D${BBOS} -O -DSEEK_END=2 -DREGEXEC"
		;;
		aix) 	echo "Making $BBOS makefile"
			CC="cc"
			CFLAGS="-D${BBOS} -DTIMEH -DREGEXEC -DGETTIMEOFDAY"
		;;
		linux|caldera|debian|mandrake|redhat|suse) echo "Making $BBOS makefile"
			CFLAGS="-DSIGSETJMP -O -D${BBOS} -DGETTIMEOFDAY -DREGEXEC -DTIMEH"
		;;
		ultrix) echo "Making $BBOS makefile"
			CFLAGS="-D${BBOS} -O DZOMBIE -DSYSLOG -DREGEXEC"
		;;
		unixware*) echo "Making $BBOS makefile"
			LIBS="-lgen  -lnsl -lsocket"
			CFLAGS="-O -DBZERO -D${BBOS} -DREGEXEC"
		;;
		*)	echo "*** Can't make Makefile for $BBOS"
			continue
		;;
	esac


	workfile="${workdir}/makefile.${BBOS}"

	# NOW MAKE THE MAKEFILE
	echo "# Makefile for $BBOS
# Generated by genmake on `date`

# OS specific compiler directives

# -DBZERO               - IF YOU DON'T HAVE bzero()
# -DZOMBIE              - IF zombie PROCESSES ARE GENERATED
# -DGETTIMEOFDAY        - TO GET DURATION STATISTICS
# -DREGEXEC             - IF YOUR COMPILER SUPPORTS regexec(),regcomp()
# -DREGEX               - IF YOUR COMPILER SUPPORTS regex(),regcmp()
# -DRE_EXEC             - IF YOUR COMPILER SUPPORTS re_exec(),re_comp()
# -DTIMEH               - IF YOUR OS HAS time.h INSTEAD OF sys/time.h
# -DSIGSETJMP           - IF YOUR OS SUPPORTS sigsetjmp() AND DOES NOT 
#				HANDLE SIGNALS PROPERLY WITH setjmp()
" > $workfile

	if test "$BBOS"
	then
		echo "BBOS = \"${BBOS}\"" >> $workfile
		BBENV="$BBENV BBOS=\${BBOS}"
	fi
	if test "$CC"
	then
		echo "CC = \"${CC}\"" >> $workfile
		BBENV="$BBENV CC=\${CC}"
	fi
	if test "$CFLAGS"
	then
		echo "CFLAGS = \"${CFLAGS}\"" >> $workfile
		BBENV="$BBENV CFLAGS=\${CFLAGS}"
	fi
	if test "$LIBS"
	then
		echo "LIBS = \"${LIBS}\"" >> $workfile
		BBENV="$BBENV LIBS=\${LIBS}"
	fi
	echo "
BBENV = ${BBENV}

all:
	\${BBENV} make -e bball
" >> $workfile

done
