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.x-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=$enableval, USE_CORBA=no)
if test $USE_CORBA = "yes" -o $USE_CORBA = "1" ; then
  USE_CORBA=1
  AC_DEFINE(USE_CORBA,[])
else
  USE_CORBA=0
fi
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=$enableval, USE_Z3950=no)
if test $USE_Z3950 = "yes" -o $USE_Z3950 = "1" ; then
  USE_Z3950=1
  AC_DEFINE(USE_Z3950,[])
else
  USE_Z3950=0
fi
AC_SUBST(USE_Z3950)

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=$enableval, USE_YAZ=yes)
if test $USE_YAZ = "yes" -o $USE_YAZ = "1" ; then
  USE_YAZ=1
  AC_DEFINE(USE_YAZ,[])
else
  USE_YAZ=0
fi
AC_SUBST(USE_YAZ)

dnl
dnl Disable all Java compilation
dnl
AC_ARG_ENABLE(java, [  --disable-java          Disable Java compilation], ENABLE_JAVA=$enableval, ENABLE_JAVA=yes)
if test $ENABLE_JAVA = "yes" -o $ENABLE_JAVA = "1" ; then
  ENABLE_JAVA=1
  if test "x$JAVA_HOME" != "x" ; then
    echo "Detected JAVA_HOME is set, however this will not be used during compilation"
    echo "To control the version of 'javac' and 'java' set environment variables JAVAC"
    echo "and JAVA respectively"
    export JAVA_HOME=
  fi
else
  ENABLE_JAVA=0
fi
AC_SUBST(ENABLE_JAVA)

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

dnl
dnl Set use of JDBM (enabled by default)
dnl
AC_ARG_ENABLE(jdbm, [  --disable-jdbm          Disable JDBM compilation], USE_JDBM=$enableval, USE_JDBM=yes)
if test $ENABLE_JAVA = "1" -a \( $USE_JDBM = "yes" -o $USE_JDBM = "1" \) ; then
  USE_JDBM=1
  AC_DEFINE(USE_JDBM,[])
else
  USE_JDBM=0
fi
AC_SUBST(USE_JDBM)

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

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=$enableval, ENABLE_ACCENTFOLD=yes)
if test $ENABLE_ACCENTFOLD = "yes" -o $ENABLE_ACCENTFOLD = "1" ; then
  ENABLE_ACCENTFOLD=1
  AC_DEFINE(ENABLE_ACCENTFOLD,[])
else
  ENABLE_ACCENTFOLD=0
fi
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=$enableval, USE_SQLITE=yes)
if test $USE_SQLITE = "yes" -o $USE_SQLITE = "1" ; then
  USE_SQLITE=1
  AC_DEFINE(USE_SQLITE,[])
else
  USE_SQLITE=0
fi
AC_SUBST(USE_SQLITE)

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 -z "$GSDLHOME"; then
  gshome=..
else 
  gshome=$GSDLHOME
fi
if test -d $gshome/ext/gnome-lib ; then
   AC_DEFINE(ENABLE_GNOMELIB_EXT, $ENABLE_GNOMELIB_EXT)
elif test -f $gshome/ext/gnome-lib-minimal/devel.bash ; then
   AC_DEFINE(ENABLE_GNOMELIB_EXT, $ENABLE_GNOMELIB_EXT)
else
   echo "**** WARNING: No gnome-lib or gnome-lib-minimal(/devel.bash) in gs2build/ext folder. Disabling gnome-lib-ext"
   AC_DEFINE(ENABLE_GNOMELIB_EXT, 0)
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
if test $ENABLE_JAVA = "1" ; then
  AC_PROG_JAVAC
  AC_PROG_JAVA
fi
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_CHECK_TOOL([STRIP],[strip])

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
if test "x$crossOS" != "x" ; then
  gsdlos=$crossOS
  gsdlarch=""
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

#----------------------------------------------------------------------------
# GS3 uses this to set up gnome-lib env, GS2 uses the configure in gs2build/toplevel GS2 installation
# 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 source its ./devel.bash to configure wvware
# If a gnome-lib is not yet compiled, we compile it first with CASCADE-MAKE.sh
# Need to make sure that the gnome-lib-minimal is not the cut-down gnome-lib-minimal (which contains
# only the linux/lib subfolder) that's included with linux and mac binaries, because we can't compile 
# wvware with the cut-down one, only run wvware with it. The test for the cut-down version is that 
# it does not contain devel.bash.

if test $ENABLE_GNOMELIB_EXT = 1; then

   if test -z "$GSDLHOME"; then
      cd ..
      gshome=`pwd`
      cd build-src
   else 
      gshome=$GSDLHOME  
   fi

   if test -f $gshome/ext/gnome-lib-minimal/devel.bash; then
      echo "*** gnome-lib (minimal) detected. Sourcing the gnomelib env for configuring wvware..."
      cd $gshome/ext/gnome-lib-minimal
      source ./devel.bash
      cd $gshome/build-src
   elif test -d $gshome/ext/gnome-lib; then
     if test -d $gshome/ext/gnome-lib/$gsdlos; then
        echo "*** Sourcing the gnomelib env for configuring wvware..."
	cd $gshome/ext/gnome-lib
      	source ./devel.bash
      	cd $gshome/build-src	
     fi 
   fi

fi

# ---------------------------------------------------------------------------
AC_CONFIG_SUBDIRS(packages)

# the list of folders in the src folder
srclist="src/hashfile/Makefile \
         src/phind/generate/Makefile \
         src/java/org/nzdl/gsdl/Makefile"

AC_OUTPUT(packages/Makefile Makefile $srclist $moduleDirs)


