######################################################################
#
#
# CONFIGURE Makefile AND BUILD cfengine
#
#
######################################################################

lookfor()

{
IFS="${IFS=   }"; ac_save_ifs="$IFS"; IFS="${IFS}:"

for name in $PATH; do
   if test -x $name/$1; then
      return 0
   fi
done
return 1
}

######################################################################

sunos()

{
case `uname -r` in

   3*)
        TYPE="SUN3";
        MAKE="/bin/make";
        LDFLAGS="-lpub -L../pub"
        CFLAGS="-DSUN3 -DREGEX_MALLOC -O"
        ;;
   4*)
        TYPE="SUN4";
        MAKE="/bin/make";
        LDFLAGS="-lpub -L../pub"
        CFLAGS="-DSUN4  -DREGEX_MALLOC -O"
        ;;
   5*)
        TYPE="SOLARIS";
        MAKE="/usr/ccs/bin/make";
        LDFLAGS="-lsocket -lnsl -lelf -lpub -L../pub" # -dn
        CFLAGS="-DSOLARIS -DHAVE_LCHOWN -DREGEX_MALLOC -O "
        ;;
esac
}

######################################################################

ultrix()

{
TYPE="ULTRIX"
MAKE="/bin/make"
LDFLAGS="-lpub -L../pub"
CFLAGS="-DULTRIX -O"
}

######################################################################

hpux()

{
TYPE="HPuUX"
MAKE="/bin/make"
LDFLAGS="-lc -lPW  -L../pub -lpub "
CFLAGS="-Ae -DHPuUX -DREGEX_MALLOC -O"
}

######################################################################

aix()

{
TYPE="AIX"
MAKE="/bin/make"
LDFLAGS="-lpub -L../pub"
CFLAGS="-DAIX -O"
}

######################################################################

osf()

{
TYPE="OSF"
MAKE="/bin/make"
LDFLAGS="-lpub -L../pub"
CFLAGS="-DOSF -DHAVE_LCHOWN -O"
}

######################################################################

irix()

{
TYPE="IRIX"
MAKE="/bin/make"
LDFLAGS="-lpub -L../pub"
CFLAGS="-DIRIX -DHAVE_ALLOCA_H -w -O"
}

######################################################################

linux()

{
TYPE="LINUX"
MAKE="/usr/bin/make"
LDFLAGS="-lpub -L../pub"

if [ ! -f /usr/lib/libfl.a ]; then
   echo "You don't have the lex/yacc library /usr/lib/libfl.a"
   echo "I can't compile without that"
   exit 1
fi

if [ `nm /usr/lib/libc.a | /usr/bin/grep -c netgr` -gt 0 ]; then
   echo "This linux has netgroups, good."
   CFLAGS="-DLINUX -O2 "
else
   echo "libc doesn't contain netgroup support. Compiling without."
   CFLAGS="-DLINUX -DNO_NETGR -O2 "
fi

if [ -d /var/lib/dpkg ]; then
   echo "This is a Debian system, great!"
   CFLAGS="$CFLAGS -DDEBIAN"
fi

echo ""
}

######################################################################

FreeBSD()

{
TYPE="FREEBSD"
MAKE="/usr/bin/make"
LDFLAGS="-lpub -L../pub"
CFLAGS="-DFREEBSD -O"
}

######################################################################
# Main
######################################################################

if lookfor uname; then
   echo ""
else
   echo "Can't even find the uname command."
   echo "Make sure your PATH variable is properly defined"
   exit 1
fi

 #
 #

case `uname -s` in

    SunOS*)       sunos  ;;
    ULTRIX*)      ultrix ;;
    HP-UX*)       hpux   ;;
    AIX*)         aix    ;;
    OSF*)         osf    ;;
    IRIX*)        irix   ;;
    Linux*)       linux  ;;
    FreeBSD*)     FreeBSD;;

    *)            echo "Unknown architecture. Can't use cfengine yet";
                  exit 1;;

esac

######################################################################

if [ $# -eq 1 ]; then
   prefix=$1;
else
   prefix="/usr/local"
fi

execprefix='${prefix}'
bindir="$execprefix/bin"
masterdir="`pwd`"

########################################################################

echo System type is $TYPE
echo Installation root is $prefix
echo ""

LEX=lex
YACC=yacc

echo Looking for a parser...

if lookfor bison; then

  YACC="bison -y"
  echo "(got bison)"

elif lookfor yacc; then

  YACC=yacc
  echo "(got yacc)"
  LDFLAGS="$LDFLAGS -ly"

else

  echo "Failed to find a parser (yacc or bison)"
  echo Make sure you have them in your command path
  exit 1

fi

echo Looking for a lexer...

if lookfor flex; then
  LEX=flex
  echo "(got flex)"
  LDFLAGS="$LDFLAGS -L$prefix/lib -L/usr/local/lib -lfl"
elif lookfor lex; then
  LEX=lex
  echo "(got lex)"
  LDFLAGS="$LDFLAGS -ll"
else
  echo "Failed to find a lexer (flex or lex)"
  echo Make sure you have them in your command path
  exit 1
fi

required="ln gcc cc"

for prog in $required
  do
    if lookfor $prog; then

       if [ $prog = "cc" ]; then
         CC="cc"
         break
       fi

       if [ $prog = "gcc" ]; then
         CC="gcc"
         break
       fi
    fi
  done

# Find out if we are using GNU C, under whatever name.

cat <<EOF > conftest.c
#ifdef __GNUC__
  yes
#endif
EOF
${CC-cc} -E conftest.c > conftest.out 2>&1
if egrep yes conftest.out >/dev/null 2>&1; then
  GCC=1 # For later tests.
  echo Looks like this is gcc
  CFLAGS="$CFLAGS"
  if [ $TYPE = "HPuUX" ]; then
     CFLAGS="-DHPuUX -O"
  fi
fi
rm -f conftest*

echo Compiler is $CC

echo checking for alloca
echo "$decl
main() { exit(0); } t() { char *p = (char *) alloca(1); }" > conftest.c
if eval $compile; then
  :
else
  alloca_missing=1
fi
rm -f conftest*
 
if test -n "$alloca_missing"; then
  SAVELIBS="$LIBS"
  # Maybe alloca is in a different library.
  #if test -f /usr/ucblib/libucb.a; then
    # Avoid the broken BSD compatibility library as much as possible.
    #LIBS="$LIBS -L/usr/ucblib -lc -lucb" trylib=-lucb # SVR4
  #el
  if test -f /lib/libPW.a; then
    LIBS="$LIBS -lPW" trylib=-lPW # SVR2 and SVR3
  fi
  if test -n "$trylib"; then
    alloca_missing=
    echo checking for alloca in $trylib
echo "$decl
main() { exit(0); } t() { char *p = (char *) alloca(1); }" > conftest.c
if eval $compile; then
  :
else
  alloca_missing=1
fi
rm -f conftest*
 
  fi
  if test -n "$alloca_missing"; then
    LIBS="$SAVELIBS" ALLOCA=alloca.o
  fi
fi

echo Checking to see if we have sys/mount.h

if test -f /usr/include/sys/mount.h; then
   echo Yes we do
else
   CFLAGS="$CFLAGS -DNO_MOUNT_H"
fi

#
# Insert special variables
#

echo Building Makefiles

cat src/Makefile.header  > src/Makefile
cat pub/Makefile.header > pub/Makefile
echo "" >> src/Makefile
echo CC=$CC >> src/Makefile
echo YACC=$YACC >> src/Makefile
echo LEX=$LEX >> src/Makefile
echo CC=$CC >> pub/Makefile
echo LDFLAGS=$LDFLAGS >> src/Makefile
echo CFLAGS=$CFLAGS   >> src/Makefile
echo CFLAGS=$CFLAGS   >> pub/Makefile
echo prefix=$prefix   >> src/Makefile
echo exec-prefix=$execprefix >> src/Makefile
echo MASTERDIR=$masterdir >> src/Makefile
echo bindir=$bindir >> src/Makefile
echo VPATH=$masterdir >> src/Makefile
echo "" >> src/Makefile
cat src/Makefile.rules  >> src/Makefile
cat pub/Makefile.rules  >> pub/Makefile



#
# Move to source directory and build the executable
#

echo VPATH=MASTERDIR > Makefile
echo MASTERDIR=$masterdir >> Makefile
cat .Makefile >> Makefile

echo Building help scripts..

if [ -x $prefix/bin/perl ]; then
   PERL="#!$prefix/bin/perl"
fi
  if [ -x /usr/bin/perl ]; then
     PERL="#!/usr/bin/perl"
  elif [ -x $prefix/bin/perl ]; then
     PERL="#!$prefix/bin/perl"
  else
     PERL="#!/usr/local/bin/perl"
  fi

echo Taking Perl invocation to be $PERL

rm -f bin/cfdaily

cat >> bin/cfdaily << END
#!/bin/csh -f
###########################################
#
# Daily configuration check
#
###########################################
 
setenv CFINPUTS $prefix/lib/cfengine/inputs
$prefix/bin/cfengine

END

chmod 755 bin/cfdaily
echo Building wrapper script cfwrap

rm -f bin/cfwrap
echo $PERL > bin/cfwrap
cat bin/cfwrap.h >> bin/cfwrap
echo '$prefix = ' \"$prefix\"';' >> bin/cfwrap
cat bin/cfwrap.t >> bin/cfwrap
chmod 755 bin/cfwrap

echo $PERL > bin/cfmail
cat bin/cfmail.t >> bin/cfmail
chmod 755 bin/cfmail

cfbindir=$prefix/lib/cfengine/bin
echo Building softquota script noseyparker

rm -f bin/noseyparker
echo $PERL > bin/noseyparker
cat bin/noseyparker.h >> bin/noseyparker
echo '$cfbindir = ' \"$cfbindir\"';' >> bin/noseyparker
cat bin/noseyparker.t >> bin/noseyparker
chmod 755 bin/noseyparker

echo Building backup script cfbackup

rm -f bin/cfbackup
echo $PERL > bin/cfbackup
cat bin/cfbackup.t >> bin/cfbackup
chmod 755 bin/cfbackup

echo Building restore script cfrestore

rm -f bin/cfrestore
echo $PERL > bin/cfrestore
cat bin/cfrestore.t >> bin/cfrestore
chmod 755 bin/cfrestore

echo Building background wrapper cfbg
echo $PERL > bin/cfbg
cat bin/cfbg.t >> bin/cfbg
chmod 755 bin/cfbg

echo "Configuration done. Run make to build cfengine."
