#!/bin/sh

# Configure script for Hobbit server
# $Id: configure.server,v 1.66 2006/08/06 06:45:59 henrik Rel $

echo ""
echo "Configuration script for Hobbit"
echo ""

while test "$1" != ""
do
	OPT="$1"; shift

	case "$OPT" in
	  "--help")
	  	cat <<EOF
  To configure Hobbit, the following options are available:
     --rrdinclude DIRECTORY   : Specify location of RRDtool include files
     --rrdlib DIRECTORY       : Specify location of RRDtool libraries
     --pcreinclude DIRECTORY  : Specify location of PCRE include files
     --pcrelib DIRECTORY      : Specify location of PCRE libraries
     --sslinclude DIRECTORY   : Specify location of OpenSSL include files
     --ssllib DIRECTORY       : Specify location of OpenSSL libraries
     --ldapinclude DIRECTORY  : Specify location of OpenLDAP include files
     --ldaplib DIRECTORY      : Specify location of OpenLDAP libraries
     --fping FILENAME         : Specify location of the Fping program

  The script will search a number of standard directories for
  all of these files, so you normally do not have to specify any options.
EOF
		exit 0
		;;
	  "--rrdinclude")
	  	USERRRDINC="$1"; shift
		;;
	  "--rrdlib")
	  	USERRRDLIB="$1"; shift
		;;
	  "--pcreinclude")
	  	USERPCREINC="$1"; shift
		;;
	  "--pcrelib")
	  	USERPCRELIB="$1"; shift
		;;
	  "--sslinclude")
	  	USEROSSLINC="$1"; shift
		;;
	  "--ssllib")
	  	USEROSSLLIB="$1"; shift
		;;
	  "--ldapinclude")
	  	USERLDAPINC="$1"; shift
		;;
	  "--ldaplib")
	  	USERLDAPLIB="$1"; shift
		;;
	  "--fping")
	  	USERFPING="$1"; shift
		;;
	esac
done

if test -r Makefile
then
	echo "Already configured - remove Makefile to re-configure"
	exit 1
fi

echo "This script asks a few questions and builds a Makefile to compile Hobbit"
echo ""

if test "$MAKE" = ""; then
	MAKE=make
fi
echo "Checking your make-utility"
MAKEVER=`$MAKE -version 2>&1 | head -n 1 | awk '{print $1 " " $2}'`
if test "$MAKEVER" != "GNU Make"
then
	echo "GNU make is required to build Hobbit."
	echo "If it is available as \"gmake\", run configure as: 'MAKE=gmake $0'"
	exit 1
fi

echo "Checking pre-requisites for building Hobbit"
echo ""
. build/fping.sh
echo ""; echo ""
. build/rrd.sh
echo ""; echo ""
. build/pcre.sh
echo ""; echo ""

. build/ssl.sh
if test -z "$OSSLINC" -o -z "$OSSLLIB"
then
	ENABLESSL="n"
	SSLDEF=""
else
	echo ""
	echo "Hobbit can use the OpenSSL library to test SSL-enabled services"
	echo "like POP3S, IMAPS, NNTPS and TELNETS. If you have the OpenSSL"
	echo "library installed, I recommend that you enable this."
	echo ""
	echo "Do you want to be able to test SSL-enabled services (y) ?"
	if test "$ENABLESSL" = ""
	then
		read ENABLESSL
	fi
	if test "$ENABLESSL" = "" -o "$ENABLESSL" = "y"
	then
		SSLDEF="-DBBGEN_SSL"
	else
		SSLDEF=""
	fi
	echo ""
fi
echo ""; echo "";

. build/ldap.sh
if test -z "$LDAPINC" -o -z "$LDAPLIB"
then
	ENABLELDAP="n"
	LDAPDEF=""
else
	echo ""
	echo "Hobbit can use your $LDAPVENDOR LDAP client library to test LDAP servers."
	echo ""
	echo "Do you want to be able to test LDAP servers (y) ?"
	if test "$ENABLELDAP" = ""
	then
		read ENABLELDAP
	fi
	if test "$ENABLELDAP" = "" -o "$ENABLELDAP" = "y"
	then
		if test "$LDAPVENDOR" = "OpenLDAP"; then
			echo "Enable experimental support for LDAP/SSL (OpenLDAP 2.x only) (y) ?"
			if test "$ENABLELDAPSSL" = ""
			then
				read ENABLELDAPSSL
			fi
		else
			ENABLELDAPSSL="n"
		fi

		if test "$ENABLELDAPSSL" = "" -o "$ENABLELDAPSSL" = "y"
		then
			LDAPDEF="-DBBGEN_LDAP -DBBGEN_LDAP_USESTARTTLS"
		else
			LDAPDEF="-DBBGEN_LDAP"
		fi
	else
		LDAPDEF=""
	fi
	echo ""
fi
echo ""; echo "";

MAKE="$MAKE -s" ./build/lfs.sh
if test $? -eq 0; then
	LFS="-D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64"
else
	LFS=""
fi
echo ""; echo ""

# Pre-requisites completed.

echo "Setting up for a Hobbit server"
echo ""; echo ""

echo "What userid will be running Hobbit [hobbit] ?"
if test -z "$BBUSER"
then
	read BBUSER
fi
if test -z "$BBUSER"
then
	BBUSER="hobbit"
fi

if test -z "$BBTOPDIR"
then
	if test "`uname -s`" = "Darwin"
	then
		# Use "nireport" to pick data on Mac OSX. From Peter Welter
		USERDATA="`nireport / /users name home | grep $BBUSER`"
		if test "$USERDATA" != ""
		then
			echo "Found NetInfo entry for user: $USERDATA"
		else
			echo "FAILURE: The user $BBUSER does not exist. Create user and try again."
			exit 1
		fi
		echo ""; echo ""
		HOMEDIR="`echo $USERDATA | awk '{print $2}'`"
	else
		USERDATA=`getent passwd $BBUSER 2>/dev/null || ypmatch "${BBUSER}" passwd || grep "^${BBUSER}:" /etc/passwd`
		if test $? -eq 0
		then
			echo "Found passwd entry for user $USERDATA"
		else
			echo "FAILURE: The user $BBUSER does not exist. Create user and try again."
			exit 1
		fi
		echo ""; echo ""
		HOMEDIR="`echo $USERDATA|cut -d: -f6`"
	fi
else
	HOMEDIR="$BBTOPDIR"
fi

echo "Where do you want the Hobbit installation [${HOMEDIR}] ?"
if test -z "$BBTOPDIR"
then
	read BBTOPDIR
fi
if test -z "$BBTOPDIR"
then
	BBTOPDIR=${HOMEDIR}
fi
if test -d "$BBTOPDIR"
then
	echo "OK, will configure to use $BBTOPDIR as the Hobbit toplevel directory"
else
	echo "WARNING: $BBTOPDIR does not exist."
fi
echo ""; echo ""

echo "What URL will you use for the Hobbit webpages [/hobbit] ? "
if test -z "$BBHOSTURL"
then
	read BBHOSTURL
fi

if test -z "$BBHOSTURL"
then
	BBHOSTURL="/hobbit"
elif test "$BBHOSTURL" = "/"
then
	# For Hobbit as the root URL, we must clear this setting.
	# Otherwise, gifs, menus etc stop working because we generate
	# URL's begining with "//"
	BBHOSTURL=""
fi
echo ""; echo ""

echo "Where to put the Hobbit CGI scripts [$BBTOPDIR/cgi-bin] ? "
echo "(Note: This is the filesystem directory - we will get to the URL shortly)"
if test -z "$CGIDIR"
then
	read CGIDIR
fi
if test -z "$CGIDIR"
then
	CGIDIR=$BBTOPDIR/cgi-bin
fi
echo ""; echo ""

echo "What is the URL for the Hobbit CGI directory [/hobbit-cgi] ? "
echo "(Note: This is the URL - NOT the filesystem directory)"
if test -z "$BBCGIURL"
then
	read BBCGIURL
fi
if test -z "$BBCGIURL"
then
	BBCGIURL="/hobbit-cgi"
fi
echo ""; echo ""

echo "********************** SECURITY NOTICE ****************************"
echo "If your Hobbit server is accessible by outsiders, then you should"
echo "restrict access to the CGI scripts that handle enable/disable of"
echo "hosts, and acknowledging of alerts. The easiest way to do this is"
echo "to put these in a separate CGI directory and require a password to"
echo "access them."
echo "Even if your Hobbit server is on a secured, internal network, you"
echo "may want to have some operations (like disabling a host) be password-"
echo "protected - that lets you see who disabled or acknowledged an alert."
echo ""
echo "Where to put the Hobbit Administration CGI scripts [$BBTOPDIR/cgi-secure] ? "
echo "(Note: This is the filesystem directory - we will get to the URL shortly)"
if test -z "$SECURECGIDIR"
then
	read SECURECGIDIR
fi
if test -z "$SECURECGIDIR"
then
	SECURECGIDIR=$BBTOPDIR/cgi-secure
fi
echo ""; echo ""

if test "$CGIDIR" != "$SECURECGIDIR"
then
	echo "What is the URL for the Hobbit Administration CGI directory [/hobbit-seccgi] ? "
	echo "(Note: This is the URL - NOT the filesystem directory)"
	if test -z "$SECUREBBCGIURL"
	then
		read SECUREBBCGIURL
	fi
	if test -z "$SECUREBBCGIURL"
	then
		SECUREBBCGIURL="/hobbit-seccgi"
	fi
else
	SECUREBBCGIURL="$BBCGIURL"
fi
echo ""; echo ""

echo "** Note that you may need to modify your webserver configuration."
echo "** After installing, see $BBTOPDIR/server/etc/hobbit-apache.conf for an example configuration."
echo ""; echo ""

echo "To generate Hobbit availability reports, your webserver"
echo "must have write-access to a directory below the Hobbit"
echo "top-level directory. I can set this up if you tell me"
echo "what group-ID your webserver runs with. This is typically"
echo "'nobody' or 'apache' or 'www-data'"
echo "If you dont know, just hit ENTER and we will handle it later."
echo ""
echo "What group-ID does your webserver use ?"
if test -z "$HTTPDGID"
then
	read HTTPDGID
fi
echo ""; echo ""

echo "Where to put the Hobbit logfiles [/var/log/hobbit] ? "
if test -z "$BBLOGDIR"
then
	read BBLOGDIR
fi
if test -z "$BBLOGDIR"
then
	BBLOGDIR=/var/log/hobbit
fi
echo ""; echo ""

echo "What is the name of this host [`uname -n`] ? "
if test -z "$BBHOSTNAME"
then
	read BBHOSTNAME
fi
if test -z "$BBHOSTNAME"
then
	BBHOSTNAME=`uname -n`
fi
echo ""; echo ""

echo "What is the IP-address of this host [127.0.0.1] ? "
if test -z "$BBHOSTIP"
then
	read BBHOSTIP
fi
if test -z "$BBHOSTIP" -o "$BBHOSTIP" = "127.0.0.1"
then
	echo "** NOTE: Using 127.0.0.1 (loopback), but it is probably not what you want **"
	BBHOSTIP=127.0.0.1
fi
echo ""; echo ""

BBHOSTOS=`uname -s | tr [A-Z] [a-z]`
if test "$BBHOME" = ""; then
	BBHOME=$BBTOPDIR/server
fi
if test "$BBVAR" = ""; then
	BBVAR=$BBTOPDIR/data
fi

echo "Where should I install the Hobbit man-pages (/usr/local/man) ?"
if test -z "$MANROOT"
then
	read MANROOT
fi
if test -z "$MANROOT"
then
	MANROOT=/usr/local/man
fi
echo ""; echo ""

echo "# Toplevel Makefile for Hobbit"    > Makefile

echo "BUILDTOPDIR=\`pwd\`"               >>Makefile
echo ""                                  >>Makefile
echo "# configure settings for Hobbit"   >>Makefile
echo "#"                                 >>Makefile
echo "# Toplevel dir"                    >>Makefile
echo "BBTOPDIR = $BBTOPDIR"              >>Makefile
echo "# Server home dir for etc/, bin/"  >>Makefile
echo "BBHOME = $BBHOME"                  >>Makefile
echo "# Server data dir for hist/ etc."  >>Makefile
echo "BBVAR = $BBVAR"                    >>Makefile
echo "# CGI scripts go in CGIDIR"        >>Makefile
echo "CGIDIR = $CGIDIR"                  >>Makefile
echo "# Admin CGI scripts go in SECURECGIDIR" >>Makefile
echo "SECURECGIDIR = $SECURECGIDIR"      >>Makefile
echo "# Where to put logfiles"           >>Makefile
echo "BBLOGDIR = $BBLOGDIR"              >>Makefile
echo "# Where to install manpages"       >>Makefile
echo "MANROOT = $MANROOT"                >>Makefile
echo "# How to run fping or hobbitping"  >>Makefile
echo "FPING = $FPING"                    >>Makefile
echo ""                                  >>Makefile
echo "# Username running hobbit"         >>Makefile
echo "BBUSER = $BBUSER"                  >>Makefile
echo "# Hobbit server hostname"          >>Makefile
echo "BBHOSTNAME = $BBHOSTNAME"          >>Makefile
echo "# Hobbit server IP-address"        >>Makefile
echo "BBHOSTIP = $BBHOSTIP"              >>Makefile
echo "# Hobbit server OS"                >>Makefile
echo "BBHOSTOS = $BBHOSTOS"              >>Makefile
echo ""                                  >>Makefile
echo "# URL for Hobbit webpages"         >>Makefile
echo "BBHOSTURL = $BBHOSTURL"            >>Makefile
echo "# URL for Hobbit CGIs"             >>Makefile
echo "BBCGIURL = $BBCGIURL"              >>Makefile
echo "# URL for Hobbit Admin CGIs"       >>Makefile
echo "SECUREBBCGIURL = $SECUREBBCGIURL"  >>Makefile
echo "# Webserver group-ID"              >>Makefile
echo "HTTPDGID=$HTTPDGID"                >>Makefile
echo ""                                  >>Makefile

echo "# RRDtool settings"                >>Makefile
echo "RRDDEF = $RRDDEF"                  >>Makefile
echo "RRDINCDIR = -I$RRDINC"             >>Makefile
echo "RRDLIBS = -L$RRDLIB -lrrd $PNGLIB" >>Makefile
echo "RPATHVAL += ${RRDLIB}"             >>Makefile
echo ""                                  >>Makefile
echo "# PCRE settings"                   >>Makefile
echo "PCREINCDIR = -I$PCREINC"           >>Makefile
echo "PCRELIBS = -L$PCRELIB -lpcre"      >>Makefile
echo "RPATHVAL += ${PCRELIB}"            >>Makefile
echo "#"                                 >>Makefile
echo "# OpenSSL settings"                >>Makefile
echo "SSLFLAGS = $SSLDEF"                >>Makefile
if test "$SSLDEF" != ""
then
	echo "SSLINCDIR = -I$OSSLINC"    >>Makefile
	echo "SSLLIBS = -L$OSSLLIB -lssl -lcrypto" >>Makefile
	echo "RPATHVAL += ${OSSLLIB}"    >>Makefile
fi
echo "#"                                 >>Makefile
echo "# OpenLDAP settings"               >>Makefile
echo "LDAPFLAGS = $LDAPDEF"              >>Makefile
if test "$LDAPDEF" != ""
then
	if test "$LDAPINC" != ""
	then
		echo "LDAPINCDIR = -I$LDAPINC"                >>Makefile
	fi
	if test "$LDAPLIB" != ""
	then
		echo "LDAPLIBS = -L$LDAPLIB -lldap $LDAPLBER" >>Makefile
		echo "RPATHVAL += ${LDAPLIB}"                 >>Makefile
	else
		echo "LDAPLIBS = -lldap $LDAPLBER"            >>Makefile
	fi
fi

echo "# Large File Support settings"     >>Makefile
echo "LFSDEF = $LFS"                     >>Makefile

echo "" >>Makefile
if test -r build/Makefile.`uname -s`
then
	echo "include build/Makefile.`uname -s`" >>Makefile
	echo ""
	echo "Using `uname -s` Makefile settings"
	echo ""
else
	echo "include build/Makefile.generic" >>Makefile
	echo ""
	echo "Using GENERIC Makefile settings"
	echo ""
	echo "If this fails, change the compile settings in Makefile"
	echo ""
	echo "I would appreciate it if you send the changes to"
	echo "hobbit-owner@hswn.dk so I can include it in the next version."
	echo ""
fi
echo "" >>Makefile
if test "$INSTALLBINDIR" != ""; then
	echo "INSTALLBINDIR = $INSTALLBINDIR"   >>Makefile
fi
if test "$INSTALLETCDIR" != ""; then
	echo "INSTALLETCDIR = $INSTALLETCDIR"   >>Makefile
fi
if test "$INSTALLEXTDIR" != ""; then
	echo "INSTALLEXTDIR = $INSTALLEXTDIR"   >>Makefile
fi
if test "$INSTALLTMPDIR" != ""; then
	echo "INSTALLTMPDIR = $INSTALLTMPDIR"   >>Makefile
fi
if test "$INSTALLWEBDIR" != ""; then
	echo "INSTALLWEBDIR = $INSTALLWEBDIR"   >>Makefile
fi
if test "$INSTALLWWWDIR" != ""; then
	echo "INSTALLWWWDIR = $INSTALLWWWDIR"   >>Makefile
fi
echo "" >>Makefile
echo "# Add local CFLAGS etc. settings here" >>Makefile
echo "" >>Makefile
echo "include build/Makefile.rules" >> Makefile
echo "" >> Makefile

echo ""; echo ""
echo "Created Makefile with the necessary information to build Hobbit"
echo "Some defaults are used, so do look at the Makefile before continuing."
echo ""

echo "Configuration complete - now run $MAKE (GNU make) to build the tools"

exit 0

