#!/bin/sh

# Configure script for Xymon server
# $Id: configure.server 8052 2019-04-22 18:20:02Z jccleaver $

echo ""
echo "Configuration script for Xymon"
echo ""

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

	case "$OPT" in
	  "--help")
	  	cat <<EOF
  To configure Xymon, 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
     --caresinclude DIRECTORY : Specify location of C-ARES include files
     --careslib DIRECTORY     : Specify location of C-ARES 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
		;;
	  "--caresinclude")
	  	USERCARESINC="$1"; shift
		;;
	  "--careslib")
	  	USERCARESLIB="$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 Xymon"
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 Xymon."
	echo "If it is available as \"gmake\", run configure as: 'MAKE=gmake $0'"
	exit 1
fi

echo "Checking pre-requisites for building Xymon"
echo ""
. build/fping.sh
echo ""; echo ""
. build/pcre.sh
echo ""; echo ""
. build/c-ares.sh
echo ""; echo ""

. build/rrd.sh
if test "$RRDOK" = "NO"
then
	ENABLERRD="n"
else
	ENABLERRD="y"
fi
echo ""; echo ""

. build/ssl.sh
if test "$SSLOK" = "NO"
then
	ENABLESSL="n"
	SSLDEF=""
else
	echo ""
	echo "Xymon can use the OpenSSL library to test SSL-enabled services"
	echo "like https-encrypted websites, POP3S, IMAPS, NNTPS and TELNETS."
	echo "If you have the OpenSSL 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
		ENABLESSL="y"
		SSLDEF="-DHAVE_OPENSSL"
		if test "$OSSL2OK" = "Y"
		then
			SSLDEF="$SSLDEF -DHAVE_SSLV2_SUPPORT=1"
		fi
		if test "$OSSL3OK" = "Y"
		then
			SSLDEF="$SSLDEF -DHAVE_SSLV3_SUPPORT=1"
		fi
	else
		ENABLESSL="n"
		SSLDEF=""
	fi
	echo ""
fi
echo ""; echo "";

. build/ldap.sh
if test "$LDAPOK" = "NO"
then
	ENABLELDAP="n"
	LDAPDEF=""
else
	echo ""
	echo "Xymon 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
		ENABLELDAP="y"
		LDAPDEF="-DHAVE_LDAP"
	else
		ENABLELDAP="n"
		LDAPDEF=""
	fi
	echo ""
fi
echo ""; echo "";

. build/clock-gettime-librt.sh
echo ""; echo ""

if test "$SNMP" = "1"
then
	. build/snmp.sh
	echo ""; echo ""
else
	DOSNMP=no
fi

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 Xymon server"
echo ""; echo ""

echo "What userid will be running Xymon [xymon] ?"
if test -z "$XYMONUSER"
then
	read XYMONUSER
fi
if test -z "$XYMONUSER"
then
	XYMONUSER="xymon"
fi

if test -z "$XYMONTOPDIR"
then
	if test "`uname -s`" = "Darwin"
	then
		# Use "dscl" for locating user information. From Isaac Vetter
		# http://www.xymon.com/archive/2008/02/msg00173.html
		USERDATA="`dscl . -list /Users  | grep $XYMONUSER`"
		if test "$USERDATA" != ""
		then
			echo "Found Directory entry for user: $USERDATA"
		else
			echo "FAILURE: The user $XYMONUSER does not exist locally. Create user and try again."
			exit 1
		fi
		echo ""; echo ""
		HOMEDIR="`dscl . -read /Users/$XYMONUSER | grep HomeDirectory | awk '{print $2}'`"
	else
		USERDATA=`getent passwd $XYMONUSER 2>/dev/null || ypmatch "${XYMONUSER}" passwd || grep "^${XYMONUSER}:" /etc/passwd`
		if test $? -eq 0
		then
			echo "Found passwd entry for user $USERDATA"
		else
			echo "FAILURE: The user $XYMONUSER does not exist. Create user and try again."
			exit 1
		fi
		echo ""; echo ""
		HOMEDIR="`echo $USERDATA|cut -d: -f6`"
	fi
else
	HOMEDIR="$XYMONTOPDIR"
fi

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

echo "What URL will you use for the Xymon webpages [/xymon] ? "
if test -z "$XYMONHOSTURL"
then
	read XYMONHOSTURL
fi

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

echo "Where to put the Xymon CGI scripts [$XYMONTOPDIR/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=$XYMONTOPDIR/cgi-bin
fi
echo ""; echo ""

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

echo "********************** SECURITY NOTICE ****************************"
echo "If your Xymon 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 Xymon 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 Xymon Administration CGI scripts [$XYMONTOPDIR/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=$XYMONTOPDIR/cgi-secure
fi
echo ""; echo ""

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

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

echo "To generate Xymon availability reports, your webserver"
echo "must have write-access to a directory below the Xymon"
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 ""
echo "What group-ID does your webserver use [nobody] ?"
if test -z "$HTTPDGID"
then
	read HTTPDGID
fi
if test -z "$HTTPDGID"
then
	HTTPDGID="nobody"
fi
echo ""; echo ""

echo "Where to put the Xymon logfiles [/var/log/xymon] ? "
if test -z "$XYMONLOGDIR"
then
	read XYMONLOGDIR
fi
if test -z "$XYMONLOGDIR"
then
	XYMONLOGDIR=/var/log/xymon
fi
echo ""; echo ""

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

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

XYMONHOSTOS=`uname -s | tr '[A-Z]' '[a-z]'`
if test "$XYMONVAR" = ""; then
	XYMONVAR=$XYMONTOPDIR/data
fi

echo "Where should I install the Xymon 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 Xymon"    > Makefile

echo "BUILDTOPDIR=\`pwd\`"               >>Makefile
echo ""                                  >>Makefile
echo "# configure settings for Xymon"   >>Makefile
echo "#"                                 >>Makefile
echo "# Toplevel dir"                    >>Makefile
echo "XYMONTOPDIR = $XYMONTOPDIR"        >>Makefile
echo "# Server data dir for hist/ etc."  >>Makefile
echo "XYMONVAR = $XYMONVAR"              >>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 "XYMONLOGDIR = $XYMONLOGDIR"        >>Makefile
echo "# Where to install manpages"       >>Makefile
echo "MANROOT = $MANROOT"                >>Makefile
echo "# How to run fping or xymonping"   >>Makefile
echo "FPING = $FPING"                    >>Makefile
echo ""                                  >>Makefile
echo "# Username running Xymon"          >>Makefile
echo "XYMONUSER = $XYMONUSER"            >>Makefile
echo "# Xymon server hostname"           >>Makefile
echo "XYMONHOSTNAME = $XYMONHOSTNAME"    >>Makefile
echo "# Xymon server IP-address"         >>Makefile
echo "XYMONHOSTIP = $XYMONHOSTIP"        >>Makefile
echo "# Xymon server OS"                 >>Makefile
echo "XYMONHOSTOS = $XYMONHOSTOS"        >>Makefile
echo ""                                  >>Makefile
echo "# URL for Xymon webpages"          >>Makefile
echo "XYMONHOSTURL = $XYMONHOSTURL"      >>Makefile
echo "# URL for Xymon CGIs"              >>Makefile
echo "XYMONCGIURL = $XYMONCGIURL"        >>Makefile
echo "# URL for Xymon Admin CGIs"        >>Makefile
echo "SECUREXYMONCGIURL = $SECUREXYMONCGIURL"  >>Makefile
echo "# Webserver group-ID"              >>Makefile
echo "HTTPDGID=$HTTPDGID"                >>Makefile
echo ""                                  >>Makefile

echo "# C-ARES settings"                 >>Makefile
if test "$CARESOK" = "YES"
then
    echo "SYSTEMCARES = yes"             >>Makefile

    if test "$CARESINC" != ""; then
	echo "CARESINCDIR = -I$CARESINC" >>Makefile
    fi
    if test "$CARESLIB" != ""; then
	echo "CARESLIBS = -L$LIB -lcares" >>Makefile
	echo "RPATHVAL += ${LIB}"        >>Makefile
    else
	echo "CARESLIBS = -lcares"       >>Makefile
    fi
else
    echo "SYSTEMCARES = no"              >>Makefile
fi
echo ""                                  >>Makefile

echo "# PCRE settings"                   >>Makefile
if test "$PCREOK" = "YES"
then
    if test "$PCREINC" != ""; then
	echo "PCREINCDIR = -I$PCREINC"   >>Makefile
    fi
    if test "$PCRELIB" != ""; then
	echo "PCRELIBS = -L$PCRELIB -lpcre" >>Makefile
	echo "RPATHVAL += ${PCRELIB}"       >>Makefile
    else
	echo "PCRELIBS = -lpcre"         >>Makefile
    fi
fi
echo ""                                  >>Makefile
if test "$ENABLERRD" = "y"
then
    echo "# RRDtool settings"            >>Makefile
    echo "RRDDEF = $RRDDEF"              >>Makefile
    if test "$RRDINC" != ""; then
	echo "RRDINCDIR = -I$RRDINC"     >>Makefile
    fi
    if test "$RRDLIB" != ""; then
	echo "RRDLIBS = -L$RRDLIB -lrrd $PNGLIB" >>Makefile
	echo "RPATHVAL += ${RRDLIB}"             >>Makefile
    else
	echo "RRDLIBS = -lrrd $PNGLIB"   >>Makefile
    fi
    echo "DORRD = yes"                   >>Makefile
fi
echo "#"                                 >>Makefile
echo "# OpenSSL settings"                >>Makefile
if test "$ENABLESSL" = "y"
then
    echo "SSLFLAGS = $SSLFLAGS"          >>Makefile
    if test "$OSSLINC" != ""; then
	echo "SSLINCDIR = -I$OSSLINC"    >>Makefile
    fi
    if test "$OSSLLIB" != ""; then
	echo "SSLLIBS = -L$OSSLLIB -lssl -lcrypto" >>Makefile
	echo "RPATHVAL += ${OSSLLIB}"    >>Makefile
    else
	echo "SSLLIBS = -lssl -lcrypto"  >>Makefile
    fi
    echo "DOSSL = yes"                   >>Makefile
fi
echo "#"                                 >>Makefile
echo "# OpenLDAP settings"               >>Makefile
echo "LDAPFLAGS = $LDAPDEF"              >>Makefile
if test "$ENABLELDAP" = "y"
then
    echo 
    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
    echo "DOLDAP = yes"                  >>Makefile
fi
echo "#"                                 >>Makefile
echo "# clock_gettime() settings"        >>Makefile
echo "LIBRTDEF = $LIBRTDEF"              >>Makefile
echo ""                                  >>Makefile
echo "# Net-SNMP settings"               >>Makefile
echo "DOSNMP = $DOSNMP"                  >>Makefile
echo ""                                  >>Makefile

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

echo "" >>Makefile
if test -r build/Makefile.`uname -s | tr '/' '_'`
then
	echo "include build/Makefile.`uname -s | tr '/' '_'`" >>Makefile
	echo ""
	echo "Using `uname -s | tr '/' '_'` Makefile settings"
	echo ""
else
	echo "include build/Makefile.generic" >>Makefile
	echo ""
	echo "Using GENERIC Makefile settings (uname -s is `uname -s`)"
	echo ""
	echo "If this fails, change the compile settings in Makefile"
	echo ""
	echo "I would appreciate it if you send the changes to"
	echo "xymon-owner@xymon.com 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 Xymon"
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

