#!/bin/sh

# configure - not a GNU autoconf-generated one, but looks like it
# $Id: configure,v 1.18 2003/05/15 12:04:37 remstats Exp $
# from remstats @@VERSION@@

# Copyright 1999, 2000, 2001, 2002 (c) Thomas Erskine <@@AUTHOR@@>
# See the COPYRIGHT file with the distribution.

# - - -   Configuration   - - -

# Guess values for system-dependent variables and create Makefiles.
# Mutilated from a configure created using autoconf version 2.13 

prefix=/var/remstats
installdir='$PREFIX'
bindir='$PREFIX/bin'
datadir='$PREFIX/data'
datapagedir='$PREFIX/data/DATAPAGES'
errorlogdir='/var/log';
libdir='$PREFIX/lib'
mandir='$PREFIX/man'
mansect='1'
etcdir='$PREFIX/etc'
configdir='$PREFIX/etc/config'
htmldir='$PREFIX/html'
htmlurl=/remstats
cgidir='$PREFIX/cgi-bin'
cgiurl='/cgi-bin'
rrdlibdir=/usr/local/rrdtool/lib/perl
rrdcgi=/usr/local/rrdtool/bin/rrdcgi
webmaster='webmaster@'`hostname`
alertemail="$webmaster"
owner=remstats
group=remstats
webgroup=remstats
dirmode=0755
sendmail=/usr/lib/sendmail
domain=`hostname`
withperl=''
localnet=127.0.0.

# Initialize some other variables.
subdirs='multiping traceroute'
MFLAGS= MAKEFLAGS=
SHELL=${CONFIG_SHELL-/bin/sh}

# - - -   Version History   - - -

# version = $Revision: 1.18 $

# - - -   Setup   - - -

# Copied the style from an autoconf configure
echo "This is *not* a GNU autoconf-generated configure.  No abbreviations please."

prev=''
for option
do

  # If the previous option needs an argument, assign it.
  if test -n "$prev"; then
    eval "$prev=\$option"
    prev=
    continue
  fi

  case "$option" in
  -*=*) optarg=`echo "$option" | sed 's/[-_a-zA-Z0-9]*=//'` ;;
  *) optarg= ;;
  esac

  # Accept the important Cygnus configure options, so we can diagnose typos.

  case "$option" in

  -prefix | --prefix )
    prev=prefix ;;
  -prefix=* | --prefix=* )
    prefix="$optarg" ;;

  -etcdir | --etcdir )
    prev=etcdir ;;
  -etcdir=* | --etcdir=* )
    etcdir="$optarg" ;;

  -rrdlibdir | --rrdlibdir )
    prev=rrdlibdir ;;
  -rrdlibdir=* | --rrdlibdir=* )
    rrdlibdir="$optarg" ;;

  -rrdcgi | --rrdcgi )
    prev=rrdcgi ;;
  -rrdcgi=* | --rrdcgi=* )
    rrdcgi="$optarg" ;;

  -configdir | --configdir )
    prev=configdir ;;
  -configdir=* | --configdir=* )
    configdir="$optarg" ;;

  -htmldir | --htmldir )
    prev=htmldir ;;
  -htmldir=* | --htmldir=* )
    htmldir="$optarg" ;;

  -htmlurl | --htmlurl )
    prev=htmlurl ;;
  -htmlurl=* | --htmlurl=* )
    htmlurl="$optarg" ;;

  -cgidir | --cgidir )
    prev=cgidir ;;
  -cgidir=* | --cgidir=* )
    cgidir="$optarg" ;;

  -cgiurl | --cgiurl )
    prev=cgiurl ;;
  -cgiurl=* | --cgiurl=* )
    cgiurl="$optarg" ;;

  -libdir | --libdir )
    prev=libdir ;;
  -libdir=* | --libdir=* )
    libdir="$optarg" ;;

  -mandir | --mandir )
    prev=mandir ;;
  -mandir=* | --mandir=* )
    mandir="$optarg" ;;

  -mansect | --mansect )
    prev=mansect ;;
  -mansect=* | --mansect=* )
    mansect="$optarg" ;;

  -bindir | --bindir )
    prev=bindir ;;
  -bindir=* | --bindir=* )
    bindir="$optarg" ;;

  -datadir | --datadir )
    prev=datadir ;;
  -datadir=* | --datadir=* )
    datadir="$optarg" ;;

  -datapagedir | --datapagedir )
    prev=datapagedir ;;
  -datapagedir=* | --datapagedir=* )
    datapagedir="$optarg" ;;

  -errorlogdir | --errorlogdir )
    prev=errorlogdir ;;
  -errorlogdir=* | --errorlogdir=* )
    errorlogdir="$optarg" ;;

  -owner | --owner )
    prev=owner ;;
  -owner=* | --owner=* )
    owner="$optarg" ;;

  -group | --group )
    prev=group ;;
  -group=* | --group=* )
    group="$optarg" ;;

  -webgroup | --webgroup )
    prev=webgroup ;;
  -webgroup=* | --webgroup=* )
    webgroup="$optarg" ;;

  -dirmode | --dirmode )
    prev=dirmode ;;
  -dirmode=* | --dirmode=* )
    dirmode="$optarg" ;;

  -domain | --domain )
    prev=domain ;;
  -domain=* | --domain=* )
    domain="$optarg" ;;

  -webmaster | --webmaster )
    prev=webmaster ;;
  -webmaster=* | --webmaster=* )
    webmaster="$optarg" ;;

  -alertemail | --alertemail )
    prev=alertemail ;;
  -alertemail=* | --alertemail=* )
    alertemail="$optarg" ;;

  -with-perl | --with-perl )
    prev=with-perl ;;
  -with-perl=* | --with-perl=* )
    withperl="$optarg" ;;

  -local-net | --local-net )
    prev=local-net ;;
  -local-net=* | --local-net=* )
    localnet="$optarg" ;;

  -help | --help )
    # This message is too long to be a string in the A/UX 3.1 sh.
    cat << EOF
Usage: configure [options]
Options: [defaults in brackets after descriptions]
Configuration:
  --help                  print this message
Directory and file names:
  --prefix=DIR            where to put the whole installation tree [$prefix]
  --bindir=DIR            executables in DIR [PREFIX/bin]
  --etcdir=DIR            where to store configuration information [PREFIX/etc]
  --configdir=DIR         where is the default configuration directory [PREFIX/etc/config]
  --libdir=DIR            where to store perl libraries [PREFIX/lib]
  --mandir=DIR            where to store man-pages [PREFIX/man]
  --mansect=N             whhich section for man-pages [$mansect]
  --datadir=DIR           where to store RRDs and status files and other
                          variable data [PREFIX/data]
  --datapagedir=DIR       where to store datapages [PREFIX/data/DATAPAGES]
  --htmldir=DIR           where to put the web-pages [PREFIX/html]
  --cgidir=DIR            where to put the CGI scripts [PREFIX/cgi-bin]
  --errorlogdir=DIR       where servers log errors to [/var/log]
  --rrdlibdir=DIR         where are the rrdtool perl libraries [$rrdlibdir]
  --rrdcgi=PROGRAM        where is the rrdcgi interpreter [$rrdcgi]
  --sendmail=PROGRAM      a program to inject email [$sendmail]
Other stuff:
  --with-perl=PROGRAM     override automatic finding of perl
  --htmlurl=URL           where is cgidir as a URL [$htmlurl]
  --cgiurl=URL            where is cgidir as a URL [$cgiurl]
  --owner=USER            which user will own this installation [$owner]
  --group=GROUP           which group will own this installation [$group]
  --webgroup=WEBGROUP     which group does the web-server run as [$webgroup]
  --dirmode=MODE          what mode to use for directories [$dirmode]
  --webmaster=EMAIL       who to send email to about this web-site [$webmaster]
  --alertemail=EMAIL      who to send email alerts to [$alertemail]
  --domain=DOMAIN         to decide what email is local [$domain]
  --local-net=LOCALNET    to create default access config-file [$localnet]
EOF
    exit 0;
	;;
  esac
done

if [ -n "$prev" ] ; then
  echo "configure: error: missing argument to --`echo $prev | sed 's/_/-/g'`" 1>&2
  exit 1;
fi

# - - -   Mainline   - - -

# for checking messages
nsize=`echo -n 'a'|wc -c`
csize=`echo 'a\c'|wc -c`
if [ "$nsize" -eq 1 ] ; then
	echo0() { echo -n "$*"; }
elif [ "$csize" -eq 1 ] ; then
	echo0() { echo "$*\c"; }
else
	echo "I can't find a proper echo. -n=$nsize, \\c=$csize"
	exit 1
fi

# Find perl
echo0 'Looking for perl...'
perl=''
for prog in $withperl /usr/local/bin/perl /usr/bin/perl `which perl` ; do
	if [ -f $prog ] ; then
		perl=$prog
		break
	elif [ -L $prog ] ; then
		perl=$prog
		break
# Command not found
	elif [ "$prog" = 'Command' ] ; then
		break
	fi
done

# No automatically found perl
if [ -z "$perl" ] ; then
	echo "can't find perl"
	exit 1

else
	echo $perl
fi

# Can we run that?
echo0 'Can we run perl...'
if $perl -v >/dev/null 2>&1 ; then
	echo 'yes'
else
	echo 'no'
	exit 1
fi

# What version is it?
echo0 'Is it a recent enough perl...'
perl_version=`$perl -v|awk '/This is perl, version/ {print $5; next} /This is perl, v/ {v=substr($4,2); print v}'`
goodenough=`expr "$perl_version" '>=' 5.005_00`
if [ -z "$perl_version" ] ; then
	echo "I don't understand which version of perl this is"
	exit 1
elif [ "$goodenough" = 1 ] ; then
	echo "yes ($perl_version)"
else
	echo "no.  Must be at least 5.005 ($perl_version)"
	exit 1
fi

# How about a good version of date? (with %Y)
echo0 'Need a date that does %Y...'
date=''
for thisdate in /usr/local/bin/date /usr/bin/date /bin/date `which date` ; do
	ok=`$thisdate +%Y 2>/dev/null |awk '/^[0-9][0-9][0-9][0-9]$/ {print "yes"; exit} {print "no"; exit}'`
	if [ "$ok" = yes ] ; then
		date=$thisdate
		echo $date
		break
# Command not found
	elif [ "$prog" = 'Command' ] ; then
		break
	fi
done
if [ -z "$date" ] ; then
	echo 'none'
	exit 1
else
	echo "using $date for date"
fi

# Where is sendmail?
echo0 'Looking for sendmail...'
sendmail=''
for prog in $sendmail /usr/lib/sendmail /usr/sbin/sendmail /var/qmail/bin/sendmail ; do
	if [ -f $prog ] ; then
		sendmail=$prog
		break
	elif [ -L $prog ] ; then
		sendmail=$prog
		break
	fi
done
if [ -z "$sendmail" ] ; then
	echo "can't find sendmail; using /usr/lib/sendmail"
	sendmail='/usr/lib/sendmail'
fi
echo $sendmail

# Where are we @
echo0 'Where are we...'
hostname=`hostname`
echo $hostname

# Now make sure we have the required perl modules
echo "Looking for required perl modules..."
missing=0
for m in `cat PERL_MODULES_REQUIRED` ; do
	if $perl -e "use lib '$libdir', '$rrdlibdir'; use $m;" 2>/dev/null ; then
		echo "   $m ok"
	else
		echo "   $m MISSING"
		missing=1
	fi
done

if [ "$missing" = 1 ] ; then
	echo "Missing required perl mdules; can't continue"
	exit 2
fi

# Now make sure we have the optional perl modules
echo "Looking for optional perl modules..."
missing=0
test -f FILES_MISSING_OPTIONAL_MODULES && rm -f FILES_MISSING_OPTIONAL_MODULES
for m in `cat PERL_MODULES_OPTIONAL` ; do
	if $perl -e "use $m;" 2>/dev/null ; then
		echo "   $m ok"
	else
		grep -ls "$m" *.pl *.pl.in *.cgi.in *stuff.in \
			>>FILES_MISSING_OPTIONAL_MODULES
		echo "   $m MISSING"
		missing=1
	fi
done

# Remove duplicates from the list
rm -rf MISSING
if [ -f FILES_MISSING_OPTIONAL_MODULES ] ; then
	sort FILES_MISSING_OPTIONAL_MODULES | uniq > TMP
	mv TMP FILES_MISSING_OPTIONAL_MODULES

	# Make a hash-equivalent for the shell, to be used by the Makefile
	mkdir MISSING
	for p in `cat FILES_MISSING_OPTIONAL_MODULES`; do
		touch MISSING/$p
	done
	rm FILES_MISSING_OPTIONAL_MODULES
fi

# Save this configuration, for automatically re-applying it
echo >config.cache #!/bin/sh
echo $0 $* | sed -e 's/\([\\\[\]()`\$\^\&\*\;\>]\)/\\\1/g' >>config.cache 
chmod 755 config.cache
echo "created config.cache; to re-config the same way run 'sh config.cache'"

# Write the new fixup.config
echo ''
if [ -f fixup.config ] ; then
	mv fixup.config fixup.config.old
	echo "Saved old fixup.config as fixup.config.old"
fi

rm -f fixup.config.new
echo >>fixup.config.new '# This is created automatically by configure'
echo >>fixup.config.new "PERL=$perl"
echo >>fixup.config.new "PERLOPTS='-w'"
echo >>fixup.config.new "PERLCGIOPTS='-Tw'"
echo >>fixup.config.new "INSTALLDIR=$installdir"
echo >>fixup.config.new "BINDIR=$bindir"
echo >>fixup.config.new "ETCDIR=$etcdir"
echo >>fixup.config.new "LIBDIR=$libdir"
echo >>fixup.config.new "MANDIR=$mandir"
echo >>fixup.config.new "MANSECT=$mansect"
echo >>fixup.config.new "CONFIGDIR=$configdir"
echo >>fixup.config.new "DATADIR=$datadir"
echo >>fixup.config.new "DATAPAGEDIR=$datapagedir"
echo >>fixup.config.new "ERRORLOGDIR=$errorlogdir"
echo >>fixup.config.new "HTMLDIR=$htmldir"
echo >>fixup.config.new "HTMLURL=$htmlurl"
echo >>fixup.config.new "CGIDIR=$cgidir"
echo >>fixup.config.new "CGIURL=$cgiurl"
echo >>fixup.config.new "RRDLIBDIR=$rrdlibdir"
echo >>fixup.config.new "RRDCGI=$rrdcgi"
echo >>fixup.config.new "WEBMASTER=$webmaster"
echo >>fixup.config.new "ALERTEMAIL=$alertemail"
echo >>fixup.config.new "REMSTATSPORT=1957"
echo >>fixup.config.new "OWNER=$owner"
echo >>fixup.config.new "GROUP=$group"
echo >>fixup.config.new "WEBGROUP=$webgroup"
echo >>fixup.config.new "DIRMODE=$dirmode"
echo >>fixup.config.new "SENDMAIL=$sendmail"
echo >>fixup.config.new "DATEPROG=$date"
echo >>fixup.config.new "OUTSITEEMAIL='@([-a-zA-Z0-9.]+)?$domain'"
echo >>fixup.config.new "LOCALNET=$localnet"
echo >>fixup.config.new "DEBUG='#'"
if [ -f fixup.config ] ; then
	mv -f fixup.config fixup.config.old
fi
sed -e "s#\$PREFIX#$prefix#g" fixup.config.new >fixup.config
rm -f fixup.config.new
echo 'configuration written to fixup.config'
./fixup Makefile.in >Makefile
cp fixup.config docs
./fixup docs/Makefile.in >docs/Makefile
echo 'wrote Makefiles'

# Now do any sub-directories
for dir in $subdirs ; do
	echo ""; echo "=== configuring $dir ==="
	(cd $dir; ./configure)
done
