dnl Process this file with autoconf to produce a configure script.
AC_INIT(Makefile.in)
AC_CONFIG_HEADER(config.h)

dnl make sure we are using a recent autoconf version
AC_PREREQ(2.12)

dnl Default is to install in /www/bin.
AC_PREFIX_DEFAULT(/www)

dnl canonicalize the host and os so we can do some tricky things before
dnl parsing options
AC_CANONICAL_HOST

PLATFORM="${host_cpu}-${host_os}"
AC_DEFINE_UNQUOTED(MHTML_SYSTEM_TYPE, [ "${host_cpu}-${host_os}" ])

dnl Configure defaults.
with_gdbm=yes
if test "$with_gnu_malloc" = ""; then
   case "${host_os}" in
     winnt*) with_gnu_malloc=no ;;
     linux*) with_gnu_malloc=no ;;
   freebsd*) with_gnu_malloc=no ;;
   solaris*) with_gnu_malloc=no ;;
          *) with_gnu_malloc=yes ;;
   esac
fi
if test "$with_readline" = ""; then with_readline=yes; fi

[OS_NAME=`echo "$host_os" | sed -e 's/[.0-9]*[_U]*[0-9]*$//'`]
if test "${OS_NAME}" = "sunos"; then
   AC_DEFINE(OPENLINK_UDBC_CLIENT, 1)
fi

if test "${OS_NAME}" = "solaris"; then
   AC_DEFINE(OPENLINK_UDBC_CLIENT, 1)
fi

dnl Argument declarations.
AC_ARG_WITH(gnu-malloc,
	[  --with-gnu-malloc       use the GNU version of malloc],
	with_gnu_malloc=$withval)
AC_ARG_WITH(des,
	[  --with-des              use EAY's DES library for encryption],
	with_des=$withval)
AC_ARG_WITH(msql,
	[  --with-msql             allow the use of the Minerva SQL database],
	with_msql=$withval)
AC_ARG_WITH(perl,
	[  --with-perl             allow the use of Perl in your pages],
	with_perl=$withval)

dnl Checks for programs.
AC_PROG_CC
AC_PROG_INSTALL
AC_PATH_PROG(FIND, find, [/usr/bin/find])
AC_PATH_PROG(BIN_PWD, pwd, [ : ])
AC_CHECK_PROGS(PERL, [perl5.004 perl5.003 perl5.002 perl5 perl])
AC_PROG_RANLIB

dnl Checks for header files.
AC_STDC_HEADERS
AC_HEADER_DIRENT
AC_CHECK_HEADERS(unistd.h stdarg.h string.h)
AC_CHECK_HEADERS(time.h sys/time.h file.h sys/file.h)
AC_CHECK_HEADERS(sys/resource.h fcntl.h sys/fcntl.h)
AC_CHECK_HEADERS(sys/ttold.h bstring.h ioctl.h sys/ioctl.h)
AC_CHECK_HEADERS(termio.h termios.h)
AC_CHECK_HEADERS(sys/select.h)
AC_HEADER_TIME

dnl A magic check here, which tries hard to determine the location of Gdbm.
if test "$with_gdbm" = ""; then with_gdbm=yes; fi
if test "$with_gdbm" = "yes"; then
GDBMLIB=$with_gdbmlib
if test "$GDBMLIB" = ""; then
  echo $ac_n "Searching for libgdbm.a... "
  for dir in $prefix/lib /usr/local/lib /opt/gnu/lib /usr/lib /lib /usr/local/gnu/lib /usr/gnu/lib; do
    if test -f $dir/libgdbm.a; then
      GDBMLIB=$dir/libgdbm.a
      SUB_SUB_GDBMLIB=$dir/libgdbm.a
      break;
    fi
  done
else
  echo $ac_n "Using libgdm.a from "
fi

if test "$GDBMLIB" = ""; then
    for dir in ../gdbm*; do
       if test -f $dir/libgdbm.a; then
	 GDBMLIB=../$dir/libgdbm.a
	 SUB_SUB_GDBMLIB=../../$dir/libgdbm.a
	 break
       fi
    done
fi
[
if test "$GDBMLIB"; then
   echo $GDBMLIB
   GDBMFIRST=`echo $GDBMLIB | sed -e 's@/[^/]*$@@'`
   GDBMFIRST="$GDBMFIRST ${GDBMFIRST}/include"
else
   echo "Not Found!  Cannot complete build."
   exit 1
fi
]

echo $ac_n "Searching for gdbm.h... "
for dir in $GDBMFIRST $prefix/include /usr/local/include /opt/gnu/include /usr/local /usr/include /usr/local/gnu/include /usr/gnu/include; do
   if test -f $dir/gdbm.h; then
     GDBM_IN=-I$dir
     SUB_SUB_GDBM_IN=-I$dir
     break
   fi
done

if test "$GDBM_IN" = ""; then
    for dir in ../gdbm*; do
       if test -f $dir/gdbm.h; then
	 GDBM_IN=-I../$dir
	 SUB_SUB_GDBM_IN=-I../../$dir
	 break
       fi
    done
fi

if test "$GDBM_IN"; then
   echo $GDBM_IN/gdbm.h
else
   echo "Not Found!  Cannot complete build."
   exit 1
fi
fi

AC_SUBST(GDBMLIB)
AC_SUBST(GDBM_IN)
AC_SUBST(SUB_SUB_GDBMLIB)
AC_SUBST(SUB_SUB_GDBM_IN)
AC_SUBST(SUB_SUB_IODBCLIB)
AC_SUBST(SUB_SUB_IODBC_IN)

dnl A magic check here, which tries hard to determine the location of libmsql.
if test "$with_msql" = "yes"; then
MSQLLIB=$with_msqllib

[
if test "$MSQLLIB"; then
   temp=`echo $MSQLLIB | sed -e 's@/[^/]*/[^/]*$@@'`/msql
   if test -r $temp/msql.h; then
      MSQL_IN=-I${temp}
      SUB_SUB_MSQL_IN=-I${temp}
   fi
fi

if test "$MSQLLIB" = ""; then
    for dir in ../msql/targets/*/lib ../msql-3*/targets/*/lib ../msql-2*/targets/*/lib ../msql-1*/targets/*/lib; do
       if test -f $dir/libmsql.a; then
	 MSQLLIB=../$dir/libmsql.a
	 SUB_SUB_MSQLLIB=../../$dir/libmsql.a
	 MSQL_IN=-I`echo $MSQLLIB | sed -e 's@/[^/]*/[^/]*$@@'`/msql
	 SUB_SUB_MSQL_IN=-I`echo $SUB_SUB_MSQLLIB | sed -e 's@/[^/]*/[^/]*$@@'`/msql
	 break
       fi
    done
fi

if test "$MSQLLIB" = ""; then
  echo $ac_n "Searching for libmsql.a... "
  for dir in $prefix/lib /usr/local/lib /opt/gnu/lib /usr/lib /lib /usr/local/gnu/lib /usr/gnu/lib /usr/local/Minerva/lib; do
    if test -f $dir/libmsql.a; then
      MSQLLIB=$dir/libmsql.a
      break;
    fi
  done
else
   echo $ac_n "Using libmsql.a from "
fi

if test "$MSQLLIB"; then
   echo $MSQLLIB
   MSQLFIRST=`echo $MSQLLIB | sed -e 's@/[^/]*/[^/]*$@@'`
   MSQLFIRST="$MSQLFIRST ${MSQLFIRST}/src ${MSQLFIRST}/msql"
else
   echo "Not Found!  Cannot complete build."
   exit 1
fi
]

echo $ac_n "Searching for msql.h... "
if test "$MSQL_IN" = ""; then
   for dir in $MSQLFIRST $prefix/include /usr/local/include /opt/gnu/include /usr/local /usr/include /usr/local/gnu/include /usr/gnu/include /usr/local/Minerva/include; do
      if test -f $dir/msql.h; then
	 MSQL_IN="-I${dir}"
	 SUB_SUB_MSQL_IN="-I${dir}"
	 break
      fi
   done
fi

if test "$MSQL_IN" = ""; then
    for dir in ../msql*; do
       if test -f $dir/msql.h; then
	 MSQL_IN="-I../$dir"
	 SUB_SUB_MSQL_IN="-I../../$dir"
	 break
       fi
    done
fi

if test "$MSQL_IN"; then
   echo $MSQL_IN/msql.h
   MSQLFUNCS_O=msqlfuncs.o
   MSQL_IN="$MSQL_IN -DMSQL_DATABASE"
else
   echo "Not Found!  Cannot complete build."
   exit 1
fi

fi
AC_SUBST(MSQLLIB)
AC_SUBST(MSQL_IN)
AC_SUBST(SUB_SUB_MSQLLIB)
AC_SUBST(SUB_SUB_MSQL_IN)
AC_SUBST(MSQLFUNCS_O)


dnl Yet another magic check, which tries hard to find the location of libdes.a.
if test "$with_des" = "yes"; then
DESLIB=$with_deslib

if test "$DESLIB" = ""; then
   echo $ac_n "Searching for libdes.a... "
   for dir in ../libdes*; do
      if test -f $dir/libdes.a; then
	 DESLIB=../$dir/libdes.a
	 SUB_SUB_DESLIB=../../$dir/libdes.a
	 break
      fi
   done
fi

if test "$DESLIB" = ""; then
   for dir in $prefix/lib /usr/local/lib /opt/gnu/lib /usr/lib /lib /usr/local/gnu/lib /usr/gnu/lib; do
      if test -f $dir/libdes.a; then
	 DESLIB=$dir/libdes.a
	 SUB_SUB_DESLIB=$dir/libdes.a
	 break;
      fi
   done
else
   echo $ac_n "Using libdes.a from "
fi


[
if test "$DESLIB"; then
   echo $DESLIB
   DESFIRST=`echo $DESLIB | sed -e 's@/[^/]*$@@'`
   DESFIRST="$DESFIRST ${DESFIRST}/include"
else
   echo "Not Found!  Cannot complete build."
   exit 1
fi
]
echo $ac_n "Searching for des.h... "
for dir in $DESFIRST $prefix/include /usr/local/include /opt/gnu/include /usr/local /usr/include /usr/local/gnu/include /usr/gnu/include; do
   if test -f $dir/des.h; then
     DES_IN=-I$dir
     break
   fi
done

if test "$DES_IN" = ""; then
    for dir in ../libdes*; do
       if test -f $dir/des.h; then
	 DES_IN=-I../$dir
	 SUB_SUB_DES_IN=-I../../$dir
	 break
       fi
    done
fi

if test "DES_IN"; then
   echo $DES_IN/des.h
   CRYPTFUNCS_O=cryptfuncs.o
   DES_IN="$DES_IN -DMHTML_CRYPTOGRAPHY"
else
   echo "Not Found!  Cannot complete build."
   exit 1
fi
fi

AC_SUBST(DESLIB)
AC_SUBST(DES_IN)
AC_SUBST(SUB_SUB_DESLIB)
AC_SUBST(SUB_SUB_DES_IN)
AC_SUBST(CRYPTFUNCS_O)

dnl Amother magic check here, which looks hard for Bash.
BASH_SHELL=bash
for dir in /bin /usr/bin /usr/contrib/bin /usr/local/bin /usr/gnu/bin /opt/gnu/bin /usr/local/gnu/bin /usr/local/gnubin; do
   if test -f $dir/bash; then
      BASH_SHELL=$dir/bash
      break;
   fi
done
AC_SUBST(BASH_SHELL)

dnl Check out the OS.  We may need to link with strange libraries.

OS_LIBS=
if test "$OS_NAME" = "solaris"; then
   OS_NAME=Solaris
   AC_DEFINE(Solaris,1)
   echo "OS is Solaris.  Good luck..."
fi

AC_CHECK_FUNCS(dlopen dlsym dlclose)
if test "$ac_cv_func_dlopen" = yes; then
   MODFUNCS_O=modfuncs.o
   MODULES_DIR=modules
fi

AC_CHECK_LIB(dl, dlopen, HAVE_LIBDL=true)
if test "$HAVE_LIBDL" = true; then
  OS_LIBS="$OS_LIBS -ldl"
  MODFUNCS_O=modfuncs.o
  MODULES_DIR=modules
fi

if test "$MODULES_DIR"; then
  SHARED_LD=ld
  case "$host_os" in
    sunos4*)	DASH_SHARED=-Bdynamic ;;
    linux*)	DASH_SHARED=-shared; DLLDFLAGS=-rdynamic ;;
    osf*)	DASH_SHARED=-shared; SHARED_LD=$CC ;;
    irix*)	DASH_SHARED=-shared; SHARED_LD=$CC ;;
    bsdi*)	DASH_SHARED=-r ;;
  freebsd*)	DASH_SHARED=B-shareable; SHARED_LD='$(LD) 2>/dev/null' ;;
    solaris*)   DASH_SHARED=-G ;;
  esac
fi
AC_SUBST(SHARED_LD)
AC_SUBST(DASH_SHARED)
AC_SUBST(MODFUNCS_O)
AC_SUBST(MODULES_DIR)
AC_SUBST(DLLDFLAGS)
AC_SUBST(OS_NAME)
AC_SUBST(EXTRAINC)

dnl Check for Perl and the Perl core directory.
if test "$with_perl" = "yes" && test -n "$PERL"; then
   cant_build_perl="";

   if test "$MODFUNCS_O" = ""; then
      echo "NO DYNAMIC MODULES SUPPORT, SO NO PERL WILL BE BUILT!"
      cant_build_perl=true;
   else
      if $PERL -e 'require(5.002)' > /dev/null 2>&1; then
	 echo "Using Perl binary $PERL"
      else
	 echo "PERL NOT FOUND, SO NO PERL SUPPORT WILL BE BUILT!"
	 cant_build_perl=true;
      fi
   fi
   if test "$cant_build_perl" = ""; then
      PERL_ARCH=`$PERL -MConfig -e 'print "$Config{archlib}\n"'`
      if test \! -r $PERL_ARCH/CORE/EXTERN.h || test \! -r $PERL_ARCH/CORE/libperl.a; then
	 echo "$PERL_ARCH does not look like a valid architecture-include directory!"
	 echo "SO, NO PERL SUPPORT WILL BE BUILT!"
	 cant_build_perl=true;
      elif test \! -r $PERL_ARCH/auto/DynaLoader/DynaLoader.a; then
	 echo "$PERL_ARCH does not seem to have DynaLoader built in it!"
	 echo "Without DynaLoader, NO PERL SUPPORT WILL BE BUILT!"
	 cant_build_perl=true;
      else
	 echo "Using Perl architecture includes in $PERL_ARCH"
      fi
   fi
   if test "$cant_build_perl" = ""; then
      PERL_INC="-I$PERL_ARCH/CORE"
      PERL_LIB="$PERL_ARCH/CORE/libperl.a -lm $PERL_ARCH/auto/DynaLoader/DynaLoader.a"
      MODPERL_SO=modperl.so
   fi
fi
AC_SUBST(PERL_ARCH)
AC_SUBST(PERL_INC)
AC_SUBST(PERL_LIB)
AC_SUBST(MODPERL_SO)

dnl Checks for library functions.
AC_FUNC_ALLOCA
AC_FUNC_SETPGRP
AC_FUNC_VFORK
AC_CHECK_FUNCS(getcwd memmove strerror strchr strcasecmp strdup flock qsort)
AC_CHECK_FUNCS(getpwnam setuid setgid)
AC_CHECK_FUNCS(srandom random initstate setstate rand srand)

AC_CHECK_FUNC(gethostbyname)
if test $ac_cv_func_gethostbyname = no; then
    AC_CHECK_LIB(nsl, gethostbyname)
fi
AC_CHECK_FUNC(connect)
if test $ac_cv_func_connect = no; then
    AC_CHECK_LIB(socket, connect)
fi

AC_CHECK_FUNC(crypt)
if test $ac_cv_func_crypt = no; then
    AC_CHECK_LIB(crypt, crypt)
fi


OS_LIBS="$LIBS $OS_LIBS"
AC_SUBST(OS_LIBS)

dnl Check for specific types.
AC_TYPE_SIZE_T
AC_TYPE_PID_T
AC_TYPE_SIGNAL
AC_C_CONST
AC_HAVE_TYPE_SIG_T
AC_HAVE_TYPE_TIME_T

LIBCOMPAT=compat/libcompat.a
COMPAT_H=compat/compat.h

AC_SUBST(LIBCOMPAT)
AC_SUBST(COMPAT_H)

if test "$with_regex" = ""; then with_regex=yes; fi
if test "$ac_cv_header_regex_h" != yes -o "$with_regex" = yes; then
   LIBREGEX=regex/libregex.a
   REGEX_H=regex/regex.h
   REGEX_IN=-I../libutils/regex
fi

if test -f /bin/sh.exe; then
   EXE=".exe"
   if test "$DESLIB" = "../../libdes/libdes.a"; then
      NTLIBS=../$DESLIB;
   elif test "$DESLIB"; then
      NTLIBS=$DESLIB;
   fi
fi
AC_SUBST(PLATFORM)
AC_SUBST(EXE)
AC_SUBST(NTLIBS)
AC_SUBST(LIBREGEX)
AC_SUBST(REGEX_H)
AC_SUBST(REGEX_IN)

if test "$with_gnu_malloc" = ""; then with_gnu_malloc=yes; fi
if test "$with_gnu_malloc" = yes; then
   LIBMALLOC=malloc/libmalloc.a
fi
AC_SUBST(LIBMALLOC)

WEBBASEDIR=$with_webbasedir
AC_SUBST(WEBBASEDIR)

if test "$with_readline" = ""; then with_readline=yes; fi
if test "$with_readline" = "yes"; then
   AC_CHECK_LIB(readline, readline, LIBREADLINE=-lreadline)
   if test "$LIBREADLINE" = ""; then
      LIBREADLINE=readline/libreadline.a
      LIBHISTORY=readline/libhistory.a
   fi
   AC_CHECK_LIB(termcap, tgetflag, LIBTERMCAP=-ltermcap)
   if test "$LIBTERMCAP" = ""; then
      AC_CHECK_LIB(curses, tgetflag, LIBTERMCAP=-lcurses)
   fi
   if test "$LIBTERMCAP" = ""; then
      AC_CHECK_LIB(ncurses, tgetflag, LIBTERMCAP=-lncurses)
   fi
   if test "$LIBTERMCAP" = ""; then
      if test "$LIBREADLINE" = "readline/libreadline.a"; then
	 LIBTERMCAP=-ltermcap
      fi
   fi
fi
AC_SUBST(LIBREADLINE)
AC_SUBST(LIBHISTORY)
AC_SUBST(LIBTERMCAP)
AC_SUBST(IODBCLIB)
AC_SUBST(IODBC_IN)
AC_SUBST(IODBCFUNCS_O)

AC_OUTPUT(Makefile mhc/Makefile server/Makefile engine/Makefile \
	  engine/fcgi/libfcgi/Makefile \
	  mdb/Makefile mdb/readline/Makefile sessions/Makefile \
	  dbutils/Makefile libmhtml/Makefile libutils/Makefile \
	  libutils/compat/Makefile libserver/Makefile utilities/Makefile \
	  utilities/imagemap/Makefile modules/Makefile)
