#!/bin/sh

#--------------------------------------------------------------------*
#
# Developed by;
#      Neal Horman - http://www.wanlink.com
#
#      CVS:  $Id: configure,v 1.6 2004/04/10 06:03:12 neal Exp $
#
# DESCRIPTION:
#	application:	Spamilter
#	module:		configure
#
#--------------------------------------------------------------------*

smdir="/usr/src/contrib/sendmail"

usage()
{
cat <<EOF
Usage:
	--help
	-f			- forceMakefile creation
	--sendmail_dir		- directory of previously compiled sendmail source
	--debug_timestamp	- add date/time and pid stamps to debug output
EOF
}

while [ "$1" != "" ]; do
	case "$1" in
		-f)
			rm -f `find . -type f -name Makefile`
			;;
		--sendmail_dir)
			shift
			smdir="$1"
			;;
		--debug_timestamp)
			cflags="-DDEBUG_TIMESTAMP $cflags"
			;;
		--help)
			usage;
			exit 0;
			;;
	esac
	shift
done

if [ ! -d "$smdir" ]; then
	echo "--sendmail_dir=\"$smdir\" does not exist, no action taken."
	exit 0;
fi

if [ -f Makefile ]; then
	echo "Makefile exists, no action taken."
	echo "Use configure -f to rebuild the Makefile";
	exit 0;
else
	os="`uname -s|sed -e's/^.*[Bb][Ss][Dd]/BSD/'`"
	oflags="-DOS_$os $oflags"
	tmp="/tmp/t.$$"
	rm -f $tmp
	case "$os" in
		SunOS)
			smobj="SunOS.`uname -r`.`uname -m|sed -e's/.$//'`"
			echo "s/<SMOBJDIR>/$smobj/g" >> $tmp
			;;
		Linux)
			smobj="Linux.`uname -r`.`uname -m`"
			echo "s/<SMOBJDIR>/$smobj/g" >> $tmp
			;;
	esac


echo "Creating Make for for $os"
echo "--sendmail_dir=\"$smdir\""

smdir="`echo \"$smdir\"|sed -e's/[\./]/\\\\&/g'`"

if [ "$cflags" != "" ]; then cat<<EOF >> $tmp
/<CFLAGS>/s/<THEFLAGS>/$cflags/
s/<CFLAGS>/<ALL>/
EOF
fi

if [ "$oflags" != "" ]; then cat<<EOF >> $tmp
/<OFLAGS>/s/<THEFLAGS>/$oflags/
s/<OFLAGS>/<ALL>/
EOF
fi

cat <<EOF >> $tmp
s/<SMDIR>/$smdir/g
/^<ALL>/{
s/^<[^>]*>//
p
}
/^<$os>/{
s/^<[^>]*>//
p
}
EOF

oldir="`pwd`";
for i in . libspf; do
cd $i
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
done
rm -f $tmp
cd $oldir;
fi
