dnl Process this file with autoconf to produce a configure script.
dnl Use autoconf-2.59 or later... it might not work anymore with
dnl the older 2.1x versions - jrm21 12/2004
AC_INIT()
AC_CONFIG_HEADER(config.h)

PACKAGE=gsdl
VERSION=2.82-svn
AC_DEFINE_UNQUOTED(PACKAGE, "$PACKAGE")
AC_DEFINE_UNQUOTED(VERSION, "$VERSION")
AC_SUBST(PACKAGE)
AC_SUBST(VERSION)

dnl
dnl Set use of fast cgi
dnl
USE_FASTCGI=0
if test USE_FASTCGI = 1; then
AC_DEFINE(USE_FASTCGI)
AC_SUBST(USE_FASTCGI)
fi

dnl
dnl Set use of langaction
dnl
dnl USE_LANGACTION=0
if test USE_LANGACTION = 1; then
AC_DEFINE(USE_LANGACTION)
AC_SUBST(USE_LANGACTION)
fi

dnl
dnl Set use of corba
dnl
AC_ARG_ENABLE(corba, [  --enable-corba          Enable CORBA support], USE_CORBA=1, USE_CORBA=0)
AC_DEFINE_UNQUOTED(USE_CORBA, $USE_CORBA)
AC_SUBST(USE_CORBA)

AC_ARG_WITH(micodir, [  --with-micodir          Use installed MICO directory], MICO_DIR=$withval, MICO_DIR="default")
AC_DEFINE_UNQUOTED(MICO_DIR, "$MICO_DIR")
AC_SUBST(MICO_DIR)

dnl
dnl Set use of z39.50
dnl
AC_ARG_ENABLE(z3950, [  --enable-z3950          Enable Z39.50 client support], USE_Z3950=1, USE_Z3950=0)
if test USE_Z3950 = 1; then
AC_DEFINE(USE_Z3950, $USE_Z3950)
AC_SUBST(USE_Z3950)
fi

dnl
dnl Set use of yaz - now compiled by default even when z3950 support not required
dnl
AC_ARG_ENABLE(yaz, [  --disable-yaz           Disable YAZ compilation], USE_YAZ=0, USE_YAZ=1)
dnl if test USE_YAZ = 1; then
AC_DEFINE(USE_YAZ, $USE_YAZ)
AC_SUBST(USE_YAZ)
dnl fi

dnl 
dnl disable compilation of accent folding stuff for mgpp (and one day mg)
dnl
AC_ARG_ENABLE(accentfold, [  --disable-accentfold    Disable Accent Folding for MGPP], ENABLE_ACCENTFOLD=0, ENABLE_ACCENTFOLD=1)
AC_DEFINE(ENABLE_ACCENTFOLD, $ENABLE_ACCENTFOLD)
AC_SUBST(ENABLE_ACCENTFOLD)

dnl
dnl Set use of SQLite (enabled by default)
dnl
AC_ARG_ENABLE(sqlite, [  --disable-sqlite        Disable SQLite support], USE_SQLITE=0, USE_SQLITE=1)
AC_DEFINE(USE_SQLITE, $USE_SQLITE)
AC_SUBST(USE_SQLITE)

dnl
dnl Set use of Apache httpd (disabled by default)
dnl
if test -d runtime-src; then
   AC_ARG_ENABLE(apache-httpd, [  --enable-apache-httpd  Enable Apache httpd support], USE_APACHE_HTTPD=1, USE_APACHE_HTTPD=0)
   AC_DEFINE(USE_APACHE_HTTPD, $USE_APACHE_HTTPD)
   AC_SUBST(USE_APACHE_HTTPD)
fi

dnl
dnl Set compilation of JNI parts of MG and MGPP indexers (disabled by default, which is GS2's default behaviour)
dnl
AC_ARG_ENABLE(jni, [  --enable-jni    Enable JNI compilation], ENABLE_JNI=$enableval, ENABLE_JNI=no)
if test $ENABLE_JNI = "yes" -o $ENABLE_JNI = "1" ; then
  ENABLE_JNI=1
  AC_DEFINE(ENABLE_JNI,[])
else
  ENABLE_JNI=0
fi
AC_SUBST(ENABLE_JNI)

dnl
dnl Set compilation of MG (enabled by default)
dnl
AC_ARG_ENABLE(mg, [  --disable-mg    Disable MG compilation], ENABLE_MG=$enableval, ENABLE_MG=yes)
if test $ENABLE_MG = "yes" -o $ENABLE_MG = "1" ; then
  ENABLE_MG=1
  AC_DEFINE(ENABLE_MG,[])
 else
  ENABLE_MG=0
 fi
AC_SUBST(ENABLE_MG)


dnl
dnl Set compilation of MGPP (enabled by default)
dnl
AC_ARG_ENABLE(mgpp, [  --disable-mgpp        Disable MGPP compilation], ENABLE_MGPP=$enableval, ENABLE_MGPP=yes)
if test $ENABLE_MGPP = "yes" -o $ENABLE_MGPP = "1" ; then
  ENABLE_MGPP=1
  AC_DEFINE(ENABLE_MGPP,[])
else
  ENABLE_MGPP=0
fi
AC_SUBST(ENABLE_MGPP)

dnl
dnl Set compilation of lucene (enabled by default)
dnl
AC_ARG_ENABLE(lucene, [  --disable-lucene        Disable Lucene compilation], ENABLE_LUCENE=$enableval, ENABLE_LUCENE=yes)
if test $ENABLE_LUCENE = "yes" -o $ENABLE_LUCENE = "1" ; then
  ENABLE_LUCENE=1
  AC_DEFINE(ENABLE_LUCENE, [])
else
  ENABLE_LUCENE=0
fi
AC_SUBST(ENABLE_LUCENE)


dnl
dnl Set compilation and/or sourcing of gnome-lib extension (disabled by default)
dnl
AC_ARG_ENABLE(gnome-lib-ext, [  --enable-gnome-lib-ext  Enable the gnome-lib extension], ENABLE_GNOMELIB_EXT=1, ENABLE_GNOMELIB_EXT=0)
if test -d ext/gnome-lib ; then
   AC_DEFINE(ENABLE_GNOMELIB_EXT, $ENABLE_GNOMELIB_EXT)
elif test -d ext/gnome-lib-minimal ; then
   AC_DEFINE(ENABLE_GNOMELIB_EXT, $ENABLE_GNOMELIB_EXT)
else
   # Running `svn` returns 127 if no svn is installed. It returns 1 if svn is installed, 
   # since it suggests running svn --help. It returns 0 if running `svn --version`

   if test $ENABLE_GNOMELIB_EXT = 1; then
      svn --version
      result=$?
      if test $result = 0; then
      	 cd ext
	 svn co https://svn.greenstone.org/gs2-extensions/gnome-lib/trunk/src gnome-lib
	 cd ..
	 AC_DEFINE(ENABLE_GNOMELIB_EXT, 1)
      else
         echo "**** WARNING: No gnome-lib or gnome-lib-minimal in gs2build/ext folder. Unable to check out gnome-lib. Disabling gnome-lib-ext."
	 AC_DEFINE(ENABLE_GNOMELIB_EXT, 0)
      fi
   fi
fi
AC_SUBST(ENABLE_GNOMELIB_EXT)


dnl Checks for programs.
LDFLAGS=$LDFLAGS
AC_SUBST(LDFLAGS)
dnl only set CFLAGS if not already set in environment
if test -z "$CFLAGS"; then
CFLAGS=-O2
fi
AC_SUBST(CFLAGS)


AC_PROG_CC
AC_PROG_CXX
AC_PROG_AWK
AC_PROG_YACC
dnl get $target_cpu, $target_vendor, and $target_os
dnl (as well as host_*)
AC_CANONICAL_SYSTEM
dnl do we actually want this? It adds a prefix based on host, system etc
dnl AC_ARG_PROGRAM
AC_PROG_INSTALL
AC_PROG_LN_S
AC_PROG_MAKE_SET
AC_PROG_RANLIB

AC_MSG_CHECKING(to see if architecture is 64-bit)
arch_64bit=no
case "$host_cpu" in
x86_64) arch_64bit=yes ;;
esac

if test "$arch_64bit" = yes; then
  AC_MSG_RESULT(yes)
  if test -z "$ARCH_DEP_FLAGS" ; then
    ARCH_DEP_FLAGS=-fPIC
  fi
else
  AC_MSG_RESULT(no)
  if test -z "$ARCH_DEP_FLAGS" ; then
    ARCH_DEP_FLAGS=
  fi
fi

export ARCH_DEP_FLAGS
AC_SUBST(ARCH_DEP_FLAGS)

#moved CORBA MICO directory test to get version of MICO
if test $USE_CORBA = 1; then

if test $MICO_DIR = "default"; then
#do something
MICO_INC="-I packages/mico/mico/include"
else
#build the micotest program
MICO_INC= ""
if test $MICO_DIR != yes; then
MICO_INC="-I $MICO_DIR"
fi
fi

#get MICO_VER value
sh -c "$CC -c micotest.cpp -o micotest.o $MICO_INC 2>&1" > /dev/null
sh -c "$CC -o micotest micotest.o -lstdc++ 2>&1" > /dev/null
MICO_VER=`./micotest` || MICO_VER=2.3.5
sh -c "rm micotest"
sh -c "rm micotest.o"

#do test of MICO_VER
if test MICO_VER != 0; then
AC_DEFINE(MICO_VER)
AC_SUBST(MICO_VER)
fi
#end USE_CORBA test
fi

dnl Checks for typedefs, structures, and compiler characteristics.
AC_AIX
AC_ISC_POSIX
AC_MINIX
fp_C_PROTOTYPES
AC_C_CONST
AC_TYPE_OFF_T
AC_TYPE_SIZE_T
AC_HEADER_TIME
AC_STRUCT_TM

dnl --- This should be in the stdc checking -----------------
if test "$ac_cv_prog_cc_stdc" = '-Xc'; then
dnl The Pyramids running SMP_DC.OSx on MIPS r3000 chips with
dnl /usr/opt/cc3.11/bin/cc treat -Xc as full ANSI mode - no POSIX or
dnl XOPEN extensions (like popen).
dnl Unfortunately -D_{XOPEN,POSIX}_SOURCE can't provide all that mg
dnl needs. So drop back to -Xa
AC_TRY_COMPILE(
[#include <stdio.h>
], [ FILE *rubbish = popen("yes", "r"); ],,
[CC="`echo $CC | sed 's/-Xc/-Xa/'`"    ac_cv_prog_cc_stdc='-Xa'])
fi
dnl ----------------------


dnl Checks for libraries.
dnl check for -lg:
AC_HAVE_LIBRARY(g)
dnl check for -lm:
AC_HAVE_LIBRARY(m)
dnl check for -lcrypt:
AC_HAVE_LIBRARY(crypt)
#if test $ENABLE_ACCENTFOLD = 1; then
#AC_HAVE_LIBRARY(iconv)
#fi

dnl Checks for header files.
AC_HEADER_DIRENT
AC_HEADER_STDC
AC_CHECK_HEADERS(fcntl.h limits.h sys/time.h unistd.h crypt.h string.h memory.h sys/procfs.h sys/stat.h)
AC_EGREP_HEADER(fread, stdio.h, AC_DEFINE(HAVE_FREAD_DECL))
AC_EGREP_HEADER(fgetc, stdio.h, AC_DEFINE(HAVE_FGETC_DECL))
AC_EGREP_HEADER(pr_brksize, sys/procfs.h, AC_DEFINE(HAVE_PR_BRKSIZE))

dnl Checks for library functions.
AC_FUNC_ALLOCA
AC_PROG_GCC_TRADITIONAL
AC_TYPE_SIGNAL
AC_FUNC_VPRINTF
AC_CHECK_FUNCS(ftime select strftime strtol getrusage times mallinfo setbuffer getpagesize strerror)
AC_REPLACE_FUNCS(ftruncate strstr strcasecmp)

dnl see if gettext is in libc. (try the textdomain() function).
AC_CHECK_FUNC(textdomain, AC_DEFINE(ENABLE_NLS ,1) )

# *** Custom checking (based on GNU tar configure.in) ***
# ---------------------------------------------------------------------------
AC_MSG_CHECKING(for HP-UX needing gmalloc)
if test "`(uname -s) 2> /dev/null`" = 'HP-UX'; then
  AC_MSG_RESULT(yes)
  AC_LIBOBJ([gmalloc])
  AC_DEFINE(HAVE_VALLOC)
else
  AC_MSG_RESULT(no)
  AC_CHECK_FUNCS(valloc)
fi

# we cannot generate static libraries under Darwin
AC_MSG_CHECKING(for Apple MacOS X/Darwin)
if test "`(uname -s) 2> /dev/null`" = 'Darwin'; then
  AC_MSG_RESULT(yes)
  STATIC=""
else
  AC_MSG_RESULT(no)
  STATIC="-static "
fi
AC_SUBST(STATIC)

fp_WITH_DMALLOC
fp_WITH_REGEX

# ---------------------------------------------------------------------------
if test "$ac_cv_func_alloca" = 'no'; then
dnl    support functions for alloca.c
  AC_LIBOBJ([xmalloc])
  AC_LIBOBJ([error])
fi


# ---------------------------------------------------------------------------

dnl
dnl Switch into C++
dnl
AC_LANG_CPLUSPLUS

dnl
dnl Now check that Perl is actually around, and can be found
dnl
AC_MSG_CHECKING(that Perl 5 is available)
success="no"
pl_path="$PATH"
IFS="${IFS=   }"; pl_save_ifs="$IFS"; IFS=":"
for pl_dir in $pl_path; do
  test -z "$pl_dir" && pl_dir=.
  if test -x $pl_dir/perl; then
    success="yes"
    break
  fi
done
IFS="$pl_save_ifs"

dnl
dnl Perl actually can be run; now we see if it is version 5 by
dnl running a sample script (which returns a "clean" version number
dnl and then tests the result using expr.  If perl errors in some
dnl way (eg. broken Perl executable, missing script), we set a failure
dnl value ("0") and then proceed as best we can
dnl
if test $success = "yes"; then
try=`perl configtest.pl` || try="0"
try=`expr $try \> 5.000`
if test $try = "1"; then
AC_MSG_RESULT("yes")
else
success="no"
fi
fi

if test $success = "no"; then
AC_MSG_RESULT("no")
AC_MSG_ERROR("Perl 5 not available - cannot install")
fi

dnl
dnl Check if the STL library has any known faults;
dnl In GCC 2.7.2.x series, some STL library headers are missing
dnl a file called "new" which is referred to by other headers
dnl (eg. vector/vector.h); if this applies, then all use of the
dnl STL will cause a compile fail, so we'd better let the user
dnl know now, before things really go wrong!
dnl
success=no
AC_MSG_CHECKING("whether STL library has known faults")

dnl - first shot, try simple good old vector on its own
AC_TRY_COMPILE(
[#include <vector>
], 
[],
success=yes,
)

dnl - try using the .h equivalent
if test $success = "no"; then
AC_TRY_COMPILE(
[#include <vector.h>
], 
[],
success="yes",
)
fi

dnl - now try for the objectspace headers
if test $success = "no"; then
AC_TRY_COMPILE(
[#include <ospace\\std\\vector>
], 
[],
success="yes",
)
fi

if test $success = yes; then
AC_MSG_RESULT("no")
else
AC_MSG_RESULT("yes")
AC_MSG_ERROR("STL Broken - Obtain newer version of GNU C Compiler")
fi

gsdlos=`uname | tr ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz`
# check for running bash under cygwin
 if test "`echo $gsdlos | sed 's/cygwin//'`" != "$gsdlos" ;
 then
  gsdlos=windows
 fi
AC_SUBST(gsdlos)

# check for endianness
AC_C_BIGENDIAN
# ---------------------------------------------------------------------------
if test "$ac_cv_func_alloca" = 'no'; then
dnl    support functions for alloca.c
  AC_LIBOBJ([xmalloc.o])
  AC_LIBOBJ([error])
fi

# ---------------------------------------------------------------------------

dnl
dnl Locate all children of the modules folder
dnl
moduleDirs=""
MODULEDIRS=""
if test -d modules ; then
  for x in `find modules -mindepth 1 -maxdepth 2`; do
    if test -d "$x"; then
      d="$x/Makefile.in"
      if test -e "$d"; then
  dnl      echo $d
        moduleDirs="$moduleDirs $x/Makefile"
        MODULEDIRS="$MODULEDIRS $x"
      fi
    fi
  done
  echo "Making $moduleDirs in modules directory"
  AC_SUBST(MODULEDIRS)
fi

#----------------------------------------------------------------------------
# GS2 uses this configure to set up gnome-lib env, GS3 uses the one in gs2build/build-src
# For gnome-lib-minimal need to source its ./devel.bash before wvware can even be configured
# For a gnome-lib that's already compiled, don't need to its source ./devel.bash to configure wvware
# If a gnome-lib is not yet compiled, we compile it first with CASCADE-MAKE.sh

if test -d ext/gnome-lib-minimal; then
   echo "*** gnome-lib (minimal) detected. Sourcing the gnomelib env for configuring wvware..."
   cd ext/gnome-lib-minimal
   source ./devel.bash
   cd ../..
elif test -d ext/gnome-lib; then
  if test -d ext/gnome-lib/$gsdlos; then
    echo "*** gnome-lib already compiled up in ext/gnome-lib/$gsdlos"
    echo "*** Sourcing the gnomelib env for configuring wvware for GS2 (although it's not necessary)..."
    cd ext/gnome-lib
    source ./devel.bash
    cd ../..
  else
    echo "*** Compiling up gnome-lib..."
    cd ext/gnome-lib
    ./CASCADE-MAKE.sh
    gnomestatus=$?
    if test $gnomestatus != 0; then
       echo "****************************************"
       echo "@@@ Failed to compile up gnomelib successfully"
       echo "****************************************"
       exit -1
    fi
    cd ../..
  fi 
fi

# ---------------------------------------------------------------------------
dnl for Darwin, as our packages don't (yet) correctly guess our system type.
dnl not needed any more?
dnl ac_configure_args="$ac_configure_args --build=$build --target=$target"

# indexers needs a bindir option to get the binaries into the right place
gsdlprefix=`pwd`
ac_configure_args="$ac_configure_args --bindir=$gsdlprefix/bin/$gsdlos --libdir=$gsdlprefix/lib/$gsdlos"
AC_CONFIG_SUBDIRS(common-src)
AC_CONFIG_SUBDIRS(build-src)

# the list of folders in the src folder
srclist="common-src/Makefile \
         build-src/Makefile"

if test -d runtime-src; then
   AC_CONFIG_SUBDIRS(runtime-src)
   srclist="$srclist \
         runtime-src/Makefile"
fi

AC_OUTPUT(Makefile $srclist $moduleDirs)


