dnl Process this file with autoconf to produce a configure script.
AC_INIT(cgi/cgi.c)
AC_CONFIG_HEADER(cs_config.h)

dnl Checks for programs.
AC_PROG_CC
AC_PROG_CPP
AC_PROG_LN_S
AC_CHECK_PROGS(AR, ar aal, ar)
AC_PROG_RANLIB

AC_PROG_MAKE_SET
AC_PROG_INSTALL

dnl Checks for Neotonic Paths
AC_MSG_CHECKING(for Neotonic Paths)
if test -d /neo/opt/include; then
  AC_MSG_RESULT(found)
  CPPFLAGS="$CPPFLAGS -I/neo/opt/include"
  LDFLAGS="$LDFLAGS -L/neo/opt/lib"
else
  AC_MSG_RESULT(not found)
fi

dnl Checks for header files.
AC_HEADER_DIRENT
AC_HEADER_STDC
AC_HEADER_SYS_WAIT
AC_CHECK_HEADERS(fcntl.h stdarg.h varargs.h limits.h strings.h sys/ioctl.h sys/time.h unistd.h features.h)

dnl Checks for typedefs, structures, and compiler characteristics.
AC_C_CONST
AC_TYPE_MODE_T
AC_TYPE_OFF_T
AC_TYPE_PID_T
AC_TYPE_SIZE_T
AC_HEADER_TIME
AC_STRUCT_TM
AC_STRUCT_TIMEZONE

dnl Checks for library functions.
AC_TYPE_SIGNAL
AC_FUNC_STRFTIME
AC_FUNC_VPRINTF
AC_FUNC_WAIT3
AC_CHECK_FUNCS(gettimeofday mktime putenv strerror strspn strtod strtol strtoul)
AC_CHECK_FUNCS(random rand drand48)

dnl Checks for libraries.
EXTRA_UTL_OBJS=
EXTRA_UTL_SRC=
cs_cv_wdb=no
AC_ARG_ENABLE(apache, [  --disable-wdb Disables building of wdb],
  [if test $enableval = no; then
     AC_MSG_RESULT(Disabling wdb code)
   else
     AC_SEARCH_LIBS(db_open, db db2, [cs_cv_wdb=yes])
     if test $cs_cv_wdb = yes; then
       AC_DEFINE(HAVE_DB2)
       EXTRA_UTL_SRC="$EXTRA_UTL_SRC wdb.c"
     fi
   fi])

dnl Check for locks
AC_CHECK_FUNC(lockf, [
  AC_DEFINE(HAVE_LOCKF)
  EXTRA_UTL_SRC="$EXTRA_UTL_SRC ulocks.c rcfs.c"

  cs_cv_pthread=no
  AC_CHECK_HEADER(pthread.h, [cs_cv_pthread=yes])
  if test $cs_cv_pthread = yes; then
    AC_DEFINE(HAVE_PTHREADS)
    EXTRA_UTL_SRC="$EXTRA_UTL_SRC skiplist.c dict.c"
  fi
])

AC_MINGW32()
if test "x$MINGW32" = "xyes"; then
  CPPFLAGS="$CPPFLAGS -D__WINDOWS_GCC__"
  USE_MINGW32="USE_MINGW32 = 1"
else
  EXTRA_UTL_SRC="$EXTRA_UTL_SRC filter.c neo_net.c neo_server.c"
fi

dnl Check for snprintf and vsnprintf
cs_cv_snprintf=no
SNPRINTFOBJS=""
AC_CHECK_FUNC(snprintf, [AC_DEFINE(HAVE_SNPRINTF)], [cs_cv_snprintf=yes])
AC_CHECK_FUNC(vsnprintf, [AC_DEFINE(HAVE_VSNPRINTF)], [cs_cv_snprintf=yes])
if test $cs_cv_snprintf = yes; then
  EXTRA_UTL_OBJS="$EXTRA_UTL_OBJS snprintf.o"
fi


dnl Check for missing re-entrant functions
cs_cv_missing=no
cs_cv_need_reentrant=no
dnl copied from libcurl
AC_CHECK_FUNCS(localtime_r, [
  AC_MSG_CHECKING(whether localtime_r is declared)
  AC_EGREP_CPP(localtime_r,[
#include <time.h>],[
    AC_DEFINE(HAVE_LOCALTIME_R)
    AC_MSG_RESULT(yes)],[
    AC_MSG_RESULT(no)
    AC_MSG_CHECKING(whether localtime_r with -D_REENTRANT is declared)
    AC_EGREP_CPP(localtime_r,[
#define _REENTRANT
#include <time.h>],[
      cs_cv_need_reentrant=yes
      AC_MSG_RESULT(yes)],[
      cs_cv_missing=yes
      AC_MSG_RESULT(no)])])], [cs_cv_missing=yes])

AC_CHECK_FUNCS(gmtime_r, [
  AC_MSG_CHECKING(whether gmtime_r is declared)
  AC_EGREP_CPP(gmtime_r,[
#include <time.h>],[
    AC_DEFINE(HAVE_GMTIME_R)
    AC_MSG_RESULT(yes)],[
    AC_MSG_RESULT(no)
    AC_MSG_CHECKING(whether gmtime_r with -D_REENTRANT is declared)
    AC_EGREP_CPP(gmtime_r,[
#define _REENTRANT
#include <time.h>],[
      cs_cv_need_reentrant=yes
      AC_MSG_RESULT(yes)],[
      cs_cv_missing=yes
      AC_MSG_RESULT(no)])])], [cs_cv_missing=yes])

AC_CHECK_FUNCS(strtok_r, [
  AC_MSG_CHECKING(whether strtok_r is declared)
  AC_EGREP_CPP(strtok_r,[
#include <string.h>],[
    AC_DEFINE(HAVE_STRTOK_R)
    AC_MSG_RESULT(yes)],[
    AC_MSG_RESULT(no)
    AC_MSG_CHECKING(whether strtok_r with -D_REENTRANT is declared)
    AC_EGREP_CPP(strtok_r,[
#define _REENTRANT
#include <string.h>],[
      cs_cv_need_reentrant=yes
      AC_MSG_RESULT(yes)],[
      cs_cv_missing=yes
      AC_MSG_RESULT(no)])])], [cs_cv_missing=yes])

AC_CHECK_FUNC(mkstemp, [AC_DEFINE(HAVE_MKSTEMP)], [cs_cv_missing=yes])
if test $cs_cv_missing = yes; then
  EXTRA_UTL_OBJS="$EXTRA_UTL_OBJS missing.o"
fi
if test $cs_cv_need_reentrant = yes; then
  CPPFLAGS="$CPPFLAGS -D_REENTRANT"
fi

cs_cv_regex=yes
AC_CHECK_FUNC(regexec, [AC_DEFINE(HAVE_REGEX)], [cs_cv_regex=no])
if test $cs_cv_regex = no; then
  CPPFLAGS="$CPPFLAGS -I\$(NEOTONIC_ROOT)/util/regex"
  EXTRA_UTL_SRC="$EXTRA_UTL_SRC regex/regex.c"
fi

cs_cv_compression=yes
AC_CHECK_LIB(z, deflate, [cs_cv_compression=yes], [cs_cv_compression=no])
AC_ARG_ENABLE(compression, [  --disable-compression		Disables HTML Compression support],
  [if test $enableval = no; then
     cs_cv_compression=no;
     AC_MSG_RESULT(Disabling HTML Compression support)
   fi])

if test $cs_cv_compression = yes; then
  AC_DEFINE(HTML_COMPRESSION)
  LIBS="$LIBS -lz"
fi

AC_ARG_ENABLE(remote-debugger, [  --enable-remote-debugger	Enables remote X CGI debugging],
  [if test $enableval = yes; then
     AC_DEFINE(ENABLE_REMOTE_DEBUG)
     AC_MSG_RESULT(Enabling CGI X Remote debugger)
   fi])

dnl Check for Apache apxs
cs_cv_apache=yes
AC_ARG_ENABLE(apache, [  --disable-apache		Disables building of apache 1.3.x module],
  [if test $enableval = no; then
     cs_cv_apache=no;
     AC_MSG_RESULT(Disabling Apache 1.3.x Module)
   fi])
AC_ARG_WITH(apache, [  --with-apache=path		Set location of Apache installation], [cs_cv_apache_path="$withval"], [cs_cv_apache_path=])

if test $cs_cv_apache = yes; then
  AC_MSG_CHECKING(for apache apxs)
  apxs_path=no
  apache_search_path="$cs_cv_apache_path /neo/opt /usr/local /usr"
  for path in $apache_search_path; do
    if test -x $path/httpd/bin/apxs; then
      apxs_path=$path/httpd/bin/apxs
      httpd_path=$path/httpd/bin/httpd
      break
    fi
    if test -x $path/httpd/sbin/apxs; then
      apxs_path=$path/httpd/sbin/apxs
      httpd_path=$path/httpd/sbin/httpd
      break
    fi
    if test -x $path/bin/apxs; then
      apxs_path=$path/bin/apxs
      httpd_path=$path/bin/httpd
      break
    fi
    if test -x $path/sbin/apxs; then
      apxs_path=$path/sbin/apxs
      httpd_path=$path/sbin/httpd
      break
    fi
  done
  if test "x$apxs_path" = "xno"; then
    AC_MSG_RESULT(not found)
  else
    AC_MSG_RESULT(found $apxs_path)
    AC_MSG_CHECKING(for apache 1.3.x)
    changequote(<<, >>)dnl
    apache_version="`$httpd_path -v | grep 'Server version' | sed -e 's/.*Apache\/\([0-9]*\.[0-9]*\.[0-9]*\).*/\1/'`"
    apache_major_version=`echo $apache_version | sed -e 's/\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\).*/\1/'`
    apache_minor_version=`echo $apache_version | sed -e 's/.*\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\).*/\2/'`
    changequote([, ])dnl
    if test "$apache_major_version" = "1" -a "$apache_minor_version" = "3"; then
      AC_MSG_RESULT(found $apache_version)
      APXS_PATH="$apxs_path"
      BUILD_WRAPPERS="$BUILD_WRAPPERS mod_ecs"
    else
      AC_MSG_RESULT(found $apache_version - disabling module build)
    fi
  fi
fi

dnl Check for Python library/includes
cs_cv_python=yes
AC_ARG_ENABLE(python, [  --disable-python		Disables building of python module],
  [if test $enableval = no; then
     cs_cv_python=no;
     AC_MSG_RESULT(Disabling python module)
   fi])
AC_ARG_WITH(python, [  --with-python=path		Set location of Python Interpreter], [cs_cv_python_path="$withval"], [cs_cv_python_path=no])

if test $cs_cv_python = yes; then
  AC_MSG_CHECKING(for python includes)
  python_inc=no
  python_lib=no
  python_search_path="/neo/opt /usr/local /usr /c"
  python_versions="2.4 2.3 2.2 2.1 2.0 1.5 24 23 22 21 20 15"
  if test $cs_cv_python_path != "no" -a -x $cs_cv_python_path; then
    python_bin=$cs_cv_python_path
    vers=`$python_bin -c "import sys; print sys.version[[:3]]"`
    py_inst_dir=`$python_bin -c "import sys; print sys.exec_prefix"`
    python_inc=$py_inst_dir/include/python$vers
    python_lib="-L$py_inst_dir/lib/python$vers/config -lpython$vers"
    python_site=$py_inst_dir/lib/python$vers/site-packages
  else
    for vers in $python_versions; do
      for path in $python_search_path; do
        if test -x $path/bin/python$vers; then
	  python_bin=$path/bin/python$vers
	  major_vers=`echo $vers | cut -b 1`
	  if test $major_vers -ge 2; then
	      python_base=`$python_bin -c "import sys, os; print os.path.dirname([[x for x in sys.path if x.find('site-packages') != -1]][[0]])"`
	  else
	      python_base=`$python_bin -c "import site, os; print os.path.dirname(site.sitedirs[[0]])"`
	  fi
	  if test -d $python_base; then
	      python_lib="-L$python_base/config -lpython$vers"
	      python_site=$python_base/site-packages
	  fi
	fi
	if test -f $path/include/python$vers/Python.h; then
	  python_inc=$path/include/python$vers
        fi
	if test "x$python_lib" = "xno"; then
	    if test -d $path/lib/python$vers; then
	      python_lib="-L$path/lib/python$vers/config -lpython$vers"
	      python_site=$path/lib/python$vers/site-packages
	    fi
	    if test -d $path/lib64/python$vers; then
	      python_lib="-L$path/lib64/python$vers/config -lpython$vers"
	      python_site=$path/lib64/python$vers/site-packages
	    fi
	fi
	dnl This is currently special cased mostly for Windows
	dnl installs, but we only use python_lib for windows anyways
	if test -f $path/python$vers/include/Python.h; then
	  python_inc=$path/python$vers/include
	  python_lib="-L$path/python$vers/libs -lpython$vers"
	  python_site=$path/python$vers/Lib/site-packages
	  break 2
	fi
	if test "x$python_inc" != "xno" -a "x$python_lib" != "xno"; then
	  break 2
	fi
      done
    done
  fi
  if test "x$python_inc" = "xno"; then
    AC_MSG_RESULT(not found)
    PYTHON=
    PYTHON_INC=
    PYTHON_LIB=
    PYTHON_SITE=
  else
    AC_MSG_RESULT(found $python_inc)
    PYTHON=$python_bin
    PYTHON_INC="-I$python_inc"
    PYTHON_LIB=$python_lib
    if test "x$PYTHON_SITE" = "x"; then
	PYTHON_SITE=$python_site
    fi
    BUILD_WRAPPERS="$BUILD_WRAPPERS python"
  fi
fi

dnl Check for Perl binary
cs_cv_perl=yes
AC_ARG_ENABLE(perl, [  --disable-perl		Disables building of perl module],
  [if test $enableval = no; then
     cs_cv_perl=no;
     AC_MSG_RESULT(Disabling perl module)
   fi])
AC_ARG_WITH(perl, [  --with-perl=path		Set location of Perl binary], [cs_cv_perl_path="$withval"], [cs_cv_perl_path=no])

if test $cs_cv_perl = yes; then
  AC_MSG_CHECKING(for perl >= 5.006)
  perl_path=no
  perl_search_path="/neo/opt /usr/local /usr"
  if test $cs_cv_perl_path != "no" -a -x $cs_cv_perl_path; then
    perl_path=$cs_cv_perl_path
  else
    for path in $perl_search_path; do
      if test -x $path/bin/perl; then
	require_error=`$path/bin/perl -e 'require 5.006' 2>&1`
	if test "x$require_error" = "x"; then
	  perl_path=$path/bin/perl
	  break
	fi
      fi
    done
  fi
  if test "x$perl_path" = "xno"; then
    AC_MSG_RESULT(not found)
    PERL=
  else
    AC_MSG_RESULT(found $perl_path)
    PERL="$perl_path"
    BUILD_WRAPPERS="$BUILD_WRAPPERS perl"
  fi
fi

dnl Check for Ruby binary
cs_cv_ruby=yes
AC_ARG_ENABLE(ruby, [  --disable-ruby		Disables building of ruby module],
  [if test $enableval = no; then
     cs_cv_ruby=no;
     AC_MSG_RESULT(Disabling ruby module)
   fi])
AC_ARG_WITH(ruby, [  --with-ruby=path		Set location of Ruby binary], [cs_cv_ruby_path="$withval"], [cs_cv_ruby_path=no])

if test $cs_cv_ruby = yes; then
  AC_MSG_CHECKING(for ruby)
  ruby_path=no
  ruby_search_path="/neo/opt /usr/local /usr"
  if test $cs_cv_ruby_path != "no" -a -x $cs_cv_ruby_path; then
    ruby_path=$cs_cv_ruby_path
  else
    for path in $ruby_search_path; do
      if test -x $path/bin/ruby; then
	ruby_path=$path/bin/ruby
	break
      fi
    done
  fi
  if test "x$ruby_path" = "xno"; then
    AC_MSG_RESULT(not found)
    RUBY=
  else
    AC_MSG_RESULT(found $ruby_path)
    RUBY="$ruby_path"
    BUILD_WRAPPERS="$BUILD_WRAPPERS ruby"
  fi
fi

dnl Check for Java library/includes
cs_cv_java=yes
AC_ARG_ENABLE(java, [  --disable-java		Disables building of java module],
  [if test $enableval = no; then
     cs_cv_java=no;
     AC_MSG_RESULT(Disabling java module)
   fi])
AC_ARG_WITH(java, [  --with-java=path		Set location of J2SDK], [cs_cv_java_path="$withval"], [cs_cv_java_path=no])

if test $cs_cv_java = yes; then
  AC_MSG_CHECKING(for j2sdk path)
  java_path=no
  if test $cs_cv_java_path != "no" -a -d $cs_cv_java_path; then
    java_path=$cs_cv_java_path
  else
    java_search_path="/neo/opt /usr/local /usr /usr/lib"
    for path in $java_search_path; do
      if test -d $path/java/j2sdk; then
	java_path=$path/java/j2sdk
	break
      fi
      if test -d $path/j2sdk; then
	java_path=$path/j2sdk
	break
      fi
      possible="$path/java/j2sdk*"
      for pos_path in $possible; do
	if test -d $pos_path; then
	  java_path=$pos_path
	  break 2
	fi
      done
      possible="$path/j2sdk*"
      for pos_path in $possible; do
	if test -d $pos_path; then
	  java_path=$pos_path
	  break 2
	fi
      done
    done
  fi
  if test "x$java_path" = "xno"; then
    AC_MSG_RESULT(not found)
    JAVA_PATH=
    JAVA_INCLUDE_PATH=
  else
    AC_MSG_RESULT(found $java_path)
    JAVA_PATH="$java_path"
    JAVAC="$java_path/bin/javac"
    _ACJNI_JAVAC=$JAVAC
    AC_JNI_INCLUDE_DIR
    for JNI_INCLUDE_DIR in $JNI_INCLUDE_DIRS
    do
	JAVA_INCLUDE_PATH="$JAVA_INCLUDE_PATH -I$JNI_INCLUDE_DIR"
    done
    BUILD_WRAPPERS="$BUILD_WRAPPERS java-jni"
  fi
fi

dnl Check for C# library/includes
cs_cv_csharp=yes
AC_ARG_ENABLE(csharp, [  --disable-csharp	Disables building of csharp module],
  [if test $enableval = no; then
     cs_cv_csharp=no;
     AC_MSG_RESULT(Disabling csharp module)
   fi])
AC_ARG_WITH(csharp, [  --with-csharp=path	Set location of csharp], [cs_cv_csharp_path="$withval"], [cs_cv_csharp_path=no])
if test $cs_cv_csharp = yes; then
  AC_MSG_CHECKING(for csharp path)
  csharp_path=no
  if test $cs_cv_csharp_path != "no" -a -d $cs_cv_csharp_path; then
    csharp_path=$cs_cv_csharp_path
  else
    csharp_search_path="/neo/opt /usr/local /usr"
    for path in $csharp_search_path; do
      if test -f $path/bin/mcs; then
	csharp_path=$path
	break
      fi
    done
  fi
  if test "x$csharp_path" = "xno"; then
    AC_MSG_RESULT(not found)
    CSHARP_PATH=
  else
    AC_MSG_RESULT(found $csharp_path/bin/mcs)
    CSHARP_PATH="$csharp_path"
    BUILD_WRAPPERS="$BUILD_WRAPPERS dso csharp"
  fi
fi

AC_ARG_ENABLE(gettext, [  --enable-gettext	Enables gettext message translation],
  [if test $enableval = yes; then
     dnl Check for gettext
     AC_CHECK_FUNC(gettext, [
       cs_cv_libintl=no
       AC_CHECK_HEADER(libintl.h, [cs_cv_libintl=yes])
       if test $cs_cv_libintl = yes; then
         AC_DEFINE(ENABLE_GETTEXT)
         AC_MSG_RESULT(Enabling gettext message translation)
       else
         AC_MSG_RESULT(not found)
       fi
     ])
   fi])


AC_SUBST(RANLIB)
AC_SUBST(AR)
AC_SUBST(USE_MINGW32)
AC_SUBST(APXS_PATH)
AC_SUBST(PERL)
AC_SUBST(RUBY)
AC_SUBST(BUILD_WRAPPERS)
AC_SUBST(JAVA_PATH)
AC_SUBST(JAVA_INCLUDE_PATH)
AC_SUBST(PYTHON)
AC_SUBST(PYTHON_INC)
AC_SUBST(PYTHON_LIB)
AC_SUBST(PYTHON_SITE)
AC_SUBST(EXTRA_UTL_SRC)
AC_SUBST(EXTRA_UTL_OBJS)
AC_SUBST(CSHARP_PATH)

AC_OUTPUT(rules.mk)
