#!/bin/sh
#
# $Header: /home/vikas/src/nocol/RCS/Configure,v 1.8 2000/01/19 04:33:31 vikas Exp $
#
# Configure script for nocol
#
# Edit all the Makefile's and insert the proper values. The names
# of the Makefile's should be Makefile.def with the variable names
#    PERL = @PERL@
#    ROOTDIR = @ROOTDIR@
# and so on.
#
# Merges Makefile.head Makefile.mid and Makefile.tail > Makefile
#
#	-vikas@navya.com  February 1997

OPATH=$PATH
PATH=/bin:/usr/bin:/sbin:/usr/sbin:/etc:/usr/etc
PATH=${PATH}:/usr/ucb:/usr/bsd:/usr/local/bin:/usr/local/gnu/bin:${OPATH}

package="nocol"
PACKAGE=`echo $package | tr [a-z] [A-Z]`

PREVDEFS=config.cache
SEDFILE=config.sed
OS="`uname -s -r`"

# If   'install -c -m 755 src dest'  fails on the system...
MYINSTALL="`pwd`/utility/myinstall"

# required variables
ROOTDIR="/usr/local/$package"
SRCDIR="`pwd`"
MANDIR="${ROOTDIR}/man"
MANEXT="n"
LOGHOST="`hostname`"
OPSMAIL="${package}-ops"
CRITMAIL="${package}-crit"
MAIL="/usr/ucb/mail"		# also  /usr/bin/mail or /sbin/mail

CC="gcc"
CFLAGS="-O"         # can add -DDEBUG also
YACC="yacc"

# Try and detect the following automatically based on the OS
OS_CFLAGS=""
OS_LIBS=""
RPCLIB=""

PSFLAGS='-axw'           # flags for ps to see all processes (-ef on SysV)

PERL="/usr/local/bin/perl"
AR="ar r"
RANLIB="ranlib"
INSTALL="install"
PING="multiping"         # need a ping that can be run as:  ping -c 3 host

# CURSESLIB can be curses or ncurses
# NEEDOBJS is required in the library for missing functions
CURSESLIB='-lcurses -ltermcap'
NEEDOBJS=""

# Now list all the variables that we should store and substitute in
# the makefiles...
ALLVARS="package ROOTDIR SRCDIR MANDIR MANEXT LOGHOST OPSMAIL CRITMAIL MAIL"
ALLVARS="${ALLVARS} CC CFLAGS YACC LIBS OS OS_CFLAGS OS_LIBS RPCLIB PSFLAGS"
ALLVARS="${ALLVARS} PERL AR WHOAMI RANLIB INSTALL PING CURSESLIB NEEDOBJS"

## end variables
##
#
if [ -f $PREVDEFS ]; then
  echo "You seem to have already run $0 before, would you like"
  echo "to take your previous answers as defaults [n]: " | tr -d '\012' 
  read input
  if [ "$input" = "Y" ] || [ "$input" = "y" ]
   then
     . ./$PREVDEFS
  fi
fi

cat <<!

SET INSTALLATION DIRECTORY
 It is advisable to put all the $PACKAGE files under one directory
 tree with further sub-directories like bin/ etc/ lib/ man/
 This will allow easy upgrades and replacements of this software
 without leaving old unsed files lying around.

 However, the DATA and LOG directories will have files which are constantly
 updated by the monitors, and you might want to keep them separately under
 a VAR directory (or alternately create soft links to these directories).
!

echo "Enter top level directory [${ROOTDIR}]: " | tr -d '\012'
read input ; if [ "X$input" != "X" ]; then  ROOTDIR=${input}; fi

MANDIR=${ROOTDIR}/man
echo "Enter location of man pages [${MANDIR}]: " | tr -d '\012'
read input ; if [ "X$input" != "X" ]; then  MANDIR=${input} ; fi

echo "Enter extension for man pages [${MANEXT}]: " | tr -d '\012'
read input ; if [ "X$input" != "X" ]; then  MANEXT=${input} ; fi

echo "               SELECT A LOGHOST"
echo " The various monitors will log messages to a loghost. It might"
echo " be useful to create a CNAME entry for ${package}.your.domain..."
echo "Enter FULLY QUALIFIED name of your loghost [${LOGHOST}]: " | tr -d '\012'
read input ; if [ "X$input" != "X" ]; then  LOGHOST=${input} ; fi

OPSMAIL="`echo $OPSMAIL | sed 's/@.*$//'`"
CRITMAIL="`echo $CRITMAIL | sed 's/@.*$//'`"
OPSMAIL="${OPSMAIL}@${LOGHOST}"
CRITMAIL="${CRITMAIL}@${LOGHOST}"

cat <<!

 $PACKAGE sends regular operational email messages when a device goes
 critical, etc. It is advised to create a "${package}-ops" email alias in
 your mail system.
 It also needs to send critical messages (in case of wrong directory
 permissions, etc.) - preferably to a system administrator. An email 
 alias such as "${package}-critical" sounds right.

!
if [ ! -f ${MAIL} ]; then  MAIL=/bin/mail ; fi
if [ ! -f ${MAIL} ]; then  MAIL=/usr/bin/mail ; fi
echo "Where is your MAIL program located? [${MAIL}] :" | tr -d '\012'
read input ; if [ "X$input" != "X" ]; then  MAIL="${input}" ; fi
if [ ! -f ${MAIL} ]; then echo "WARNING, ${MAIL} does not exist" ; fi

echo "Where should the operational email go? [${OPSMAIL}] :" | tr -d '\012'
read input ; if [ "X$input" != "X" ]; then  OPSMAIL="${input}" ; fi

echo "Where should urgent/critical email go? [${CRITMAIL}] :" | tr -d '\012'
read input ; if [ "X$input" != "X" ]; then  CRITMAIL="${input}" ; fi


##
##
echo ""
echo "Which compiler would you like to use? [${CC}]: " | tr -d '\012'
read input ; if [ "X$input" != "X" ]; then  CC="${input}" ; fi

echo "What compiler options do you want (-DDEBUG)? [${CFLAGS}]: " | tr -d '\012'
read input ; if [ "X$input" != "X" ]; then  CFLAGS="${input}" ; fi

(mkdir TemP; cd TemP; ${YACC}) 2>&1 | egrep -i 'not.*found' >/dev/null 2>&1
if [ $? = 0 ]; then echo "${YACC} NOT FOUND" ; YACC="bison -y" ; fi
rm -rf TemP
echo "If using a yacc alternate, enter it here (bison -y) [${YACC}]: " | tr -d '\012'
read input ; if [ "X$input" != "X" ]; then  YACC="${input}" ; fi

if [ ! -f ${PERL} ]; then  PERL=/usr/bin/perl ; fi
if [ ! -f ${PERL} ]; then  PERL=/bin/perl ; fi
echo "Where is PERL located on your system? [$PERL]: "| tr -d '\012' 
read input ; if [ "X$input" != "X" ]; then  PERL="${input}" ; fi
if [ ! -f ${PERL} ]; then echo "WARNING, ${PERL} does not exist" ; fi

##
## Detect the operating system dependent parameters now
##
echo ""
echo "Detecting operating system dependencies for ${OS}..."

export PSFLAGS
(ps $PSFLAGS) 2>&1 | egrep -i '^usage' >/dev/null 2>&1
if [ $? = 0 ]; then PSFLAGS='-ef' ; fi

LIBRESOLV="-lresolv"
ls /usr/lib/libresolv* >/dev/null 2>&1
if [ $? = 1 ]; then
  ls /lib/libresolv* >/dev/null 2>&1
  if [ $? = 1 ]; then
  	ls /usr/lib/libbind* >/dev/null 2>&1
  	if [ $? = 1 ]; then LIBRESOLV="" ; else LIBRESOLV="-lbind" ; fi
  fi
fi

# mainly for Solaris systems
WHOAMI="/usr/ucb/whoami"
if [ ! -f ${WHOAMI} ]; then 
    WHOAMI="/usr/bin/whoami"
    if [ ! -f ${WHOAMI} ]; then WHOAMI="whoami" ; fi
fi

# These OS_CFLAGS and OS_LIBS are hardcoded in this script
#
case "$OS" in
  AIX|AIX\ *) 
	      OS_CFLAGS="-DAIX"
	      OS_LIBS="-lbsd"         # -l/lib/pse.exp ?
	      PING="/etc/ping"        # can do ping -c cnt host
              INSTALL=$MYINSTALL
	      ;; 
  AUX|A/UX\ 3*) 
		OS_CFLAGS="-DAUX"
		OS_LIBS="-lposix"
		;; 
  BSDI|BSDI\ *|BSD386\ *|BSD/386\ *|FreeBSD\ 1*) 
		OS_CFLAGS="-DBSDI"
		OS_LIBS="${LIBRESOLV}"    # -lcompat
		PING=`which ping`     # can do ping -c cnt host
		;; 
  BSD/OS\ 2*|BSD/OS\ 3*|BSD/OS\ 4*) 
		OS_CFLAGS="-DBSDI"
		OS_LIBS="${LIBRESOLV}"    # -lcompat
		PING=`which ping`     # can do ping -c cnt host
		;; 
  FREEBSD|FreeBSD\ 2*|FreeBSD\ 3*|NETBSD|NetBSD\ 1*)
		OS_CFLAGS="-DFREEBSD"
		OS_LIBS="${LIBRESOLV} -lcrypt" # some hosts -lresolv not needed
		PING=`which ping`     # can do ping -c cnt host
		;;
  OPENBSD|OpenBSD\ 2*)
		OS_CFLAGS="-DFREEBSD"
		OS_LIBS="${LIBRESOLV}"
		PING="/sbin/ping"
		;;
  HPUX|HP-UX\ *)
		OS_CFLAGS="-DHPUX"
		OS_LIBS=""
		RANLIB="touch"
		CURSESLIB='-lcurses'   # doesnt have -ltermcap
		;;                     #  irregular ping, so use multiping
  IRIX40|IRIX\ 4*)
		OS_CFLAGS="-DIRIX40 -cckr"
		OS_LIBS="-lsun"
		RANLIB=touch
		INSTALL=$MYINSTALL
		PING="/usr/etc/ping"  # can do ping -c cnt host
		;;
  IRIX51|IRIX\ 5*)
		OS_CFLAGS="-DIRIX51 -cckr"
		OS_LIBS=""
		RANLIB=touch
		PING="/usr/etc/ping"  # can do ping -c cnt host
		;;
  LINUX12|Linux\ 1*) 
		OS_CFLAGS="-DLINUX1"
		OS_LIBS="-ldbm"
		CURSESLIB='-lncurses -ltermcap'
		PING="/bin/ping"
		;; 
  LINUX20|Linux\ 2*) 
		OS_CFLAGS="-DLINUX2";
		OS_LIBS="${LIBRESOLV}"
		if [ -f /usr/lib/libgdbm.so ]; then
		  OS_LIBS="${OS_LIBS} -lgdbm"
		fi
		CURSESLIB='-lncurses' # no termcap
		PING="/bin/ping"     # can do ping -c cnt host
		;; 
  OSF1|OSF1\ *|OSF\ 1*) 
		OS_CFLAGS="-DOSF1"
		OS_LIBS=""
		INSTALL=bsdinst
		PING=/usr/sbin/ping  # can do ping -c cnt host
		;;
  RHAPSODY|Rhapsody\ 5*|MOSX) 
		OS_CFLAGS="-DBSDI"
		OS_LIBS=""
		PING=`which ping`     # can do ping -c cnt host
		;; 
  SUNOS4|SunOS\ 4*)
		OS_CFLAGS="-DSUNOS4"
		OS_LIBS="${LIBRESOLV}"
		;;                  # use multiping
  SUNOS5|SunOS\ 5*)
		OS_CFLAGS="-DSUNOS5"
		OS_LIBS="-lsocket -lnsl ${LIBRESOLV}"
		AR="ld -r -o"
		RANLIB="touch"
		;;                  # use multiping
  SCO|SCO*)
		OS_CFLAGS="-DSVR4"
		OS_LIBS="-lnsl -lsocket -lcrypt"
		INSTALL=installbsd
		;; 
  SVR4|System\ V\ Release\ 4*)
		OS_CFLAGS="-DSVR4"
		OS_LIBS="-lnsl -lsocket"
		;; 
  SVR3|System\ V\ Release\ 3*)
		OS_CFLAGS="-DSVR3"
		OS_LIBS="-lnsl -lsocket -lcrypt"
		INSTALL=installbsd
		;; 
  ULTRIX\ *|Ultrix\ *|ultrix\ *) 
		OS_CFLAGS="-DULTRIX"
		OS_LIBS=""
		;;                  # use multiping
  UNIXWARE|UNIXWARE\ *) 
		OS_CFLAGS="-DSVR4"
		OS_LIBS="-lcrypt -lgen -lc -L/usr/ucblib -lucb -lsocket"
		;; 
  *) 
     cat <<EoF
     OS=$OS is not a supported or detected operating system. Try setting
     the value of OS manually to the type of your system or set the
     OS_CFLAGS and OS_LIBS value manually in the Makefile if compile fails.

EoF
     ;; 
esac

##
echo "        OS_CFLAGS = ${OS_CFLAGS}"
echo "        OS_LIBS   = ${OS_LIBS}"
echo ""

# Check for the correct INSTALL...
>ChInstall
(${INSTALL} -c -m 750 ChInstall /tmp/) >/dev/null 2>&1
if [ ! -f /tmp/ChInstall ]; then INSTALL=${MYINSTALL} ; fi
rm -f ChInstall /tmp/ChInstall

##
## Save all entered parameters
##
echo "Saving all values in $PREVDEFS..."
rm -f $PREVDEFS
>$PREVDEFS
echo '# These answers are interdependent; do not edit this file!' >>$PREVDEFS
if test ! -w $PREVDEFS
then
  echo "Seems like the $PREVDEFS file is not writable"
else
  (
   for v in $ALLVARS
   do
     # even this horror botches metachars in var
     # set | grep "^${v}="
     eval "echo $v=\\\"\"\$$v\"\\\""
   done
  ) >>$PREVDEFS
fi

##
## Edit the Makefiles for all the variables in $ALLVARS
##
echo "Editing various Makefiles..."
rm -f $SEDFILE
>$SEDFILE
for v in $ALLVARS
 do
   (eval "echo s%@$v@%\$$v%g") >> $SEDFILE
   # mla@gams.co.at suggests the following instead on Linux... FIX FIX
   #(eval "echo \"s%\\\\([^s][^#]\\\\)@$v@%\\\\1\$$v%g\"") >> $SEDFILE
 done

for m in Makefile.mid */Makefile.mid */*/Makefile.mid
 do
   M=`echo $m | sed 's/\.mid$//'`
   if [ -f $m ]; then
     if [ -f $M ]; then mv -f $M ${M}.bak ; fi
     cat Makefile.head $m Makefile.tail | sed -f $SEDFILE > $M
   fi
 done

rm -f $SEDFILE


##
cat <<Eof

Done editing Makefiles.

              Type   "make"  to start building  $PACKAGE

Eof

