#!/bin/sh

EXITCODE=0

if [ $0 != "./configure" ] ; then
	echo This script needs to be executed in the directory that
	echo contains this script.
	exit 1
fi

# Options: '--authonly'
if [ "$1" = '--authonly' ] ; then
	AUTHONLY="yes"
elif [ -n "$1" ] ; then
	echo 'Usage: ./configure [options]'
	echo 'Where [options] currently only supports "--authonly" for'
	echo 'making an authoritative-only build of MaraDNS'
	exit 1
fi

# Make sure we are running this script from the maradns base directory
if [ `pwd | awk -F/ '{print $NF}' | cut -f1 -d-` != "maradns" ] ; then
	echo This script needs to be run from the MaraDNS base
	echo directory \(the top-level directory created when the
	echo MaraDNS tar file was extracted\)
	exit 1
fi

# Set the directory for the build files
BUILDDIR=build
export BUILDDIR

# Show them the disclaimer
cat 00README.FIRST
echo -----
echo

# Try to determine what kind of system we are running
UNAME=`uname -s`

# Make sure the Makefile has the current version number of MaraDNS
# VERSION is a one line file in the form "VERSION=0.1.23" (without the
# quotes
if [ -f VERSION ] ; then
	cp VERSION Makefile
elif [ -f $BUILDDIR/VERSION ] ; then
	cp $BUILDDIR/VERSION Makefile
else
	pwd | awk -F- '{print "VERSION="$NF}' > Makefile
fi

# Set up the informaiton on where and when this version of MaraDNS was
# compiled
SA="system at"
if [ -z "$AUTHONLY" ] ; then
	echo COMPILED=\"$UNAME system at `date`\" >> Makefile
	echo COMPILED_DEBUG=\"$UNAME system at `date`\ \(Debug\)\" >> Makefile
else
	echo COMPILED=\"$UNAME system at `date` \(authonly\)\" >> Makefile
      echo COMPILED_DEBUG=\"$UNAME $SA `date`\ \(Debug authonly\)\" >> Makefile
fi

# Give them a message dependent on what kind of systme they have
if echo $UNAME | grep -i linux > /dev/null ; then
	cat $BUILDDIR/Makefile.linux >> Makefile
	echo It looks like you are using Linux\; just type in \'make\'
	EXITCODE=0
elif echo $UNAME | grep -i freebsd > /dev/null ; then
	cat $BUILDDIR/Makefile.freebsd >> Makefile
	echo It looks like you are using FreeBSD\; this should compile
	echo fine by typing in \'make\'.  There is an official port here:
	echo
	echo	http://www.freebsd.org/cgi/cvsweb.cgi/ports/dns/maradns/
	echo
	echo Which may be on your system here:
	echo
	echo	/usr/ports/dns/maradns
	echo
	EXITCODE=0
elif echo $UNAME | grep -i openbsd > /dev/null ; then
	cat $BUILDDIR/Makefile.freebsd >> Makefile
	echo It looks like you are using OpenBSD\; this should compile 
	echo fine by typing in \'make\'.
	EXITCODE=0
elif echo $UNAME | grep -i mingw32 > /dev/null ; then
	cat $BUILDDIR/Makefile.mingw32 >> Makefile
	echo It looks like you are using MinGW32 \; this is only a partial
	echo port of MaraDNS and does not have all of the security features
	echo of other ports of MaraDNS.  Only MaraDNS and Askmara will
	echo compile.  You need mingw32 and pthreads to compile this.
	echo You have been warned.
	# We don't support --authonly for the mingw32 port
	# Other Makefile changes
	cp server/Makefile.mingw32 server/Makefile
	cp tools/Makefile.mingw32 tools/Makefile
	cp qual/Makefile.threadsafe qual/Makefile
	cp dns/Makefile.recursive dns/Makefile
	# Mingw32 doesn't support symlinks
	cp server/MaraBigHash_en.h server/MaraBigHash_locale.h
	cp server/MaraDNS_en.h server/MaraDNS_locale.h
	cp COPYING 00README.FIRST
	exit 0
elif echo $UNAME | grep -i cygwin > /dev/null ; then
	cat $BUILDDIR/Makefile.noflock >> Makefile
	echo It looks like you are using Cygwin\; this should compile fine
	echo by typing in \'make\'.
	EXITCODE=0
elif echo $UNAME | grep -i AIX > /dev/null ; then
	cat $BUILDDIR/Makefile.AIX >> Makefile
	echo It looks like you are using AIX\; this should compile fine
	echo by typing in \'make\'.
	EXITCODE=0
elif echo $UNAME | grep -i darwin > /dev/null ; then
	cat $BUILDDIR/Makefile.darwin >> Makefile
	cp $BUILDDIR/install.darwin $BUILDDIR/install.locations
	echo It looks like you are using Darwin \(usually\; Mac OS X\)\;
	#echo This should compile fine by typing in \'make\'.
	echo You might be able to compile MaraDNS by typing \"make\"
	echo \(if you typed in \"make\" before, just type it again\)
	echo
	echo THIS PORT HAS NOT BEEN FULLY TESTED\; USE AT YOUR OWN RISK
	EXITCODE=1
# This is a a template for adding support to a new OS for MaraDNS.
# Some points: If you port MaraDNS, make sure the underlying OS has
# /dev/urandom support or have the default mararc have a
# random_seed_file mararc variable pointing to a file with good
# randomness.  Make sure this port compiles and installs.  Take
# responsibility for people who have problems with this port.
elif echo $UNAME | grep -i SomeNewPortOfMaradns > /dev/null ; then
	cat $BUILDDIR/Makefile.SomeNewPort >> Makefile
	echo It looks like you are using NAME OF OS HERE\; this port has
        echo been done by YOUR NAME AND CONTACT INFO HERE\; this should
 	echo compile fine by typing in \'make\'.  If you encounter any
	echo problems with this port, please contact the person who has
	echo done the port before trying to contact the MaraDNS	
	echo developer.
	# The next line is technically optional, but should be here
	EXITCODE=0
else
	cat $BUILDDIR/Makefile.noflock >> Makefile
	echo WARNING WARNING WARNING
	echo
	echo This is an unknown platform.  MaraDNS may or may not compile
	echo on this platform.  If you are able to sucessfully compile
	echo and install MaraDNS on this platform, please let me know
	echo by contacting me.  My contact info is here:
	echo
	echo	http://www.maradns.org/contact.html
	echo
	echo I encourage you to make an official port of MaraDNS for this
	echo platform so other users of MaraDNS do not see this obnoxious
	echo warning.
	echo
	echo You might be able to compile MaraDNS by typing \"make\"
	echo \(if you typed in \"make\" before, just type it again\)
	echo
	echo THIS PORT HAS NOT BEEN TESTED\; USE AT YOUR OWN RISK
	EXITCODE=1
fi

# Set up recursive and authoritative name serving
if [ -z "$AUTHONLY" ] ; then
	cp server/Makefile.recursive server/Makefile
	# FreeBSD uses -pthread instead of -lpthread to compile a
	# Pthread program
	if echo $UNAME | grep -i freebsd > /dev/null ; then
		cat server/Makefile.recursive | \
			sed 's/lpthread/pthread/' > server/Makefile
	fi
	cp qual/Makefile.threadsafe qual/Makefile
	cp tcp/Makefile.recursive tcp/Makefile
	cp dns/Makefile.recursive dns/Makefile
	cp tools/Makefile.recursive tools/Makefile
else
	cp server/Makefile.authonly server/Makefile
	cp qual/Makefile.nothreads qual/Makefile
	cp tcp/Makefile.authonly tcp/Makefile
	cp dns/Makefile.authonly dns/Makefile
	cp tools/Makefile.authonly tools/Makefile
fi

echo
exit $EXITCODE

