#!/bin/sh

#--------------------------------------------------------------------*
#
# Developed by;
#      Neal Horman - http://www.wanlink.com
#
#      CVS:  $Id: configure,v 1.20 2005/11/27 20:51:10 neal Exp $
#
# DESCRIPTION:
#	application:	Spamilter
#	module:		configure
#
#--------------------------------------------------------------------*

smdir="/usr/src/contrib/sendmail"
tmp="/tmp/t.$$"
log="config.tmp"

os="`uname -s`"
osver="`uname -r`"
case "$os" in
	SunOS)
		echobin="/usr/ucb/echo"
		;;
	*)
		echobin="echo"
		;;
esac

rm -f $tmp
$echobin -n "./configure " > $log

usage()
{
cat <<EOF
Usage:
	--help
	-f			- forceMakefile creation
	--sendmail-dir		- directory prefix of previously compiled sendmail source
	--debug-timestamp	- add date/time and pid stamps to debug output
	--with-libspf		- compile in support for libspf
	--with-popauth		- compile in support for pop after smtp
	--with-pam		- compile in support for pam for ipfwmtad (for use on FreeBSD)
	--have-resn		- platform has support for res_n???? functions (--with-libbind defines this)
	--with-libbind		- directory prefix of previously compiled bind source
	--with_libdb		- directory prefix of previously compiled db source - use this if your system doesn't include libdb,
EOF
}

Not()
{
	$echobin "s/<NOT_$1 \([^>]*\)>/\1 /g" >> $tmp
}

Include()
{
	$echobin "s/<INC_$1 \([^>]*\)>/\1 /g" >> $tmp
}

With()
{
	$echobin "s/<WITH_$1 \([^>]*\)>/\1 /g" >> $tmp
}

ExpandTagLine()
{
if [ "$3" != "" ]; then cat<<EOF >> $1
/<$2>/s/<X>/$3/
/<$2>/s/<x>/$3/
s/<$2>/<ALL>/
EOF
fi
}

CheckDir()
{
	if [ ! -d "$2" ]; then
		$echobin "$1 \"$2\" does not exist, no action taken."
		exit 0;
	fi
}

SedifyDir()
{
	$echobin $1|sed -e's/[\./ ]/\\&/g'
}

while [ "$1" != "" ]; do
	case "$1" in
		-f)
			$echobin -n "$1 " >> $log
			if [ -f Makefile ]; then
				make distclean || rm -f Makefile
			fi
			;;
		--sendmail-dir)
			if [ "$2" = "" ]; then
				echo "Missing --sendmail-dir directory paramater";
				exit 1;
			fi
			$echobin -n "$1 $2 " >> $log
			CheckDir $1 $2
			smdir=$2
			shift
			;;
		--debug-timestamp)
			$echobin -n "$1 " >> $log
			cflags="-DDEBUG_TIMESTAMP $cflags"
			;;
		--with-popauth)
			$echobin -n "$1 " >> $log
			cflags="-DSUPPORT_POPAUTH $cflags"
			With "POPAUTH"
			;;
		--with-libspf)
			if [ "$2" = "" ]; then
				echo "Missing --with-libspf directory paramater";
				exit 1;
			fi
			$echobin -n "$1 $2 " >> $log
			cflags="-DSUPPORT_LIBSPF $cflags"
			With "LIBSPF"
			x="`SedifyDir $2`"
			ExpandTagLine $tmp "LIBSPFINC" $x
			ExpandTagLine $tmp "LIBSPFDIR" $x
			shift
			;;
		--with-pam)
			$echobin -n "$1 " >> $log
			cflags="-DSUPPORT_PAM $cflags"
			With "PAM"
			;;
		--have-resn)
			$echobin -n "$1 " >> $log
			cflags="-DHAVE_RES_N $cflags"
			;;
		--with-libbind)
			if [ "$2" = "" ]; then
				echo "Missing --with-libbind directory paramater";
				exit 1;
			fi
			$echobin -n "$1 $2 " >> $log
			cflags="-DHAVE_RES_N $cflags"
			incdirs="-I`SedifyDir $2/include` $incdirs"
			libdirs="-L`SedifyDir $2/lib` $libdirs"
			libs="-lbind $libs"
			CheckDir $1 "$2/include"
			CheckDir $1 "$2/lib"
			shift
			;;
		--with-libdb)
			if [ "$2" = "" ]; then
				echo "Missing --with-libdb directory paramater";
				exit 1;
			fi
			$echobin -n "$1 $2 " >> $log
			incdirs="-I`SedifyDir $2/include` $incdirs"
			libdirs="-L`SedifyDir $2/lib` $libdirs"
			libs="-ldb $libs"
			CheckDir $1 "$2/include"
			CheckDir $1 "$2/lib"
			shift
			;;
		--help)
			usage;
			exit 0;
			;;
		*)
			$echobin "Unsupported configure paramenter \"$1\""
			usage;
			exit 0;
			;;
	esac
	shift
done

if [ -f Makefile ]; then
	$echobin "Makefile exists, no action taken."
	$echobin "Use configure -f to rebuild the Makefile";
	exit 0;
else
#	osbsd="`uname -s|sed -e's/^.*[Bb][Ss][Dd]/BSD/'`"
	oflags="-DOS_$os"
	case "$os" in
		FreeBSD)
			;;
		SunOS)
			if [ "$osver" = "5.8" -o "$osver" = "5.9" -o "$osver" = "5.10" ]; then
				cflags="-DHAVE_RES_N $cflags"
			else
				cflags="-DUSE_RR_CL $cflags"
			fi
			smobj="$os.`uname -r`.`uname -m|sed -e's/.$//'`"
			$echobin "s/<SMOBJDIR>/$smobj/g" >> $tmp
			Not "FreeBSD"
			Not "BSD"
			;;
		OpenBSD)
			smobj="$os.`uname -r`.`uname -m`"
			$echobin "s/<SMOBJDIR>/$smobj/g" >> $tmp
			Not "FreeBSD"
			;;
		Linux)
			smobj="$os.`uname -r`.`uname -m`"
			$echobin "s/<SMOBJDIR>/$smobj/g" >> $tmp
			Not "FreeBSD"
			Not "BSD"
			;;
		HP-UX)
			smobj="$os.`uname -r`.`uname -m`"
			$echobin "s/<SMOBJDIR>/$smobj/g" >> $tmp
			Not "FreeBSD"
			Not "BSD"
			;;
		*)
			$echobin "Unkown OS $os"
			exit 0;
			;;
	esac


$echobin "Creating Makefile for $os"
$echobin "--sendmail-dir=\"$smdir\""
$echobin ""

CheckDir "--sendmail-dir" $smdir

cp $log config.log
rm -f $log

smdir="`SedifyDir $smdir`"
ExpandTagLine $tmp "SMDIR" $smdir

ExpandTagLine $tmp "CFLAGS" "$cflags"
ExpandTagLine $tmp "OFLAGS" $oflags
ExpandTagLine $tmp "INCDIRS" $incdirs
ExpandTagLine $tmp "LIBDIRS" $libdirs
ExpandTagLine $tmp "LIBS" $libs

Include $os

# Filter out <INC_....> sections that are not be expanded
# Filter out <WITH_....> sections that are not be expanded
# Filter out all lines that are not tagged with <ALL> or the os name.
cat <<EOF >> $tmp
s/<NOT_\([^>]*\)>//g
s/<INC_\([^>]*\)>//g
s/<WITH_\([^>]*\)>//g
/^<ALL>/{
s/^<[^>]*>//
p
}
/^<$os>/{
s/^<[^>]*>//
p
}
EOF

cat <<EOF > Makefile
 #--------------------------------------------------------------------*
 #
 # WARNING! - any changes you make to this file will be lost the next
 # time you run configure.
 #
EOF
	sed -n -f $tmp Makefile.tmpl >> Makefile
rm -f $tmp
fi
