dnl Process this file with autoconf to produce a configure script.
AC_INIT(text/MGQuery.cpp)
AC_CONFIG_HEADER(config.h)

dnl get $target_cpu, $target_vendor, and $target_os
dnl (as well as host_*)
AC_CANONICAL_SYSTEM
AC_ARG_PROGRAM

dnl
PACKAGE=mgpp
VERSION=1.3

AC_DEFINE_UNQUOTED(PACKAGE, "$PACKAGE")
AC_DEFINE_UNQUOTED(VERSION, "$VERSION")
AC_SUBST(PACKAGE)
AC_SUBST(VERSION)

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)

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 AC_MSG_CHECKING(to see if architecture is 64-bit)
dnl arch_64bit=no
dnl case "$host_cpu" in
dnl x86_64) arch_64bit=yes ;;
dnl esac
if test -z "$ARCH_DEP_FLAGS" ; then
    ARCH_DEP_FLAGS=
fi
AC_SUBST(ARCH_DEP_FLAGS)

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 Checks for programs.
AC_PROG_CXX
AC_PROG_AWK
AC_PROG_YACC
AC_PROG_CC
AC_PROG_INSTALL
AC_PROG_LN_S
AC_PROG_MAKE_SET
AC_PROG_RANLIB
if test $ENABLE_JAVA = "1" ; then
  AC_PROG_JAVA
  AC_PROG_JAVAC
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
dnl look for unac package if not in standard location
dnl
dnl is it ok to use `pwd` like this??
AC_ARG_WITH(unac, [  --with-unac=dir         Use local unac library and headers], UNAC_DIR=$withval, UNAC_DIR="`pwd`/../packages/unac")
AC_DEFINE_UNQUOTED(UNAC_DIR, "$UNAC_DIR")
AC_SUBST(UNAC_DIR)

dnl
dnl look for libiconv package if not in standard location
dnl
#AC_ARG_WITH(unac, [  --with-libiconv-prefix=dir         Use local libiconv library], LIBICONV_DIR=$withval)
#AC_DEFINE_UNQUOTED(LIBICONV_DIR, "$LIBICONV_DIR")
#AC_SUBST(LIBICONV_DIR)


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 Replace `main' with a function in -lg:
AC_CHECK_LIB(g, main)
dnl Replace `main' with a function in -lm:
AC_CHECK_LIB(m, main)
dnl Replace `main' with a function in -lstdc++:
dnl AC_CHECK_LIB(stdc++, main)
dnl Replace `main' with a function in -lcrypt:
AC_CHECK_LIB(crypt, main)
#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)
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) )

# we need to set an extended JNI path for Mac OS/Darwin, as jni.h is on
# a non-standard Path
# is there a better way to do this??
AC_MSG_CHECKING(for OS to set JNI options)
# set defaults
JNIINC=""
JNIPREFIX="lib"
JNISUFFIX="so"
JNICFLAGS=""
JNILDFLAGS="-shared"

if test "`(uname -s) 2> /dev/null`" = 'Darwin'; then
  AC_MSG_RESULT(Darwin)
  JNIINC="-I\$(JAVA_HOME)/include/darwin -I\$(JAVA_HOME)/include "    
  JNISUFFIX="jnilib"
  knownMinXcodeVersionForNewJavaFramework=2395
  xcodeVersion=`(xcode-select --version) 2> /dev/null`
  if test "$?" -eq 127 || test "${xcodeVersion//[!0-9]/}" -lt $knownMinXcodeVersionForNewJavaFramework; then
    JNILDFLAGS="-dynamiclib -fno-common -single_module -framework JavaVM"
  else
    JNILDFLAGS="-dynamiclib -fno-common -single_module -framework JavaNativeFoundation"
  fi
fi
if test "`(uname -s) 2> /dev/null`" = 'SunOS'; then
  AC_MSG_RESULT(Solaris)
  JNIINC="-I\$(JAVA_HOME)/include/solaris "
fi
if test "`(uname -s) 2> /dev/null`" = 'Linux'; then
  AC_MSG_RESULT(Linux)
  JNIINC="-I\$(JAVA_HOME)/include/linux -I\$(JAVA_HOME)/include "
fi

if test "x$crossOS" != "x" ; then
  if test "$crossOS" = "windows" ; then
    JNIINC="-I../../java-windows-include/win32 -I../../java-windows-include "
    JNICFLAGS="-shared -Wall -D_JNI_IMPLEMENTATION_ -Wl,--kill-at"
    JNIPREFIX=""
    JNISUFFIX="dll"
  fi
fi

AC_SUBST(JNIINC)
AC_SUBST(JNIPREFIX)
AC_SUBST(JNISUFFIX)
AC_SUBST(JNICFLAGS)
AC_SUBST(JNILDFLAGS)

# *** 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

fp_WITH_DMALLOC
fp_WITH_REGEX
fp_WITH_GNU_READLINE

# text for endianness
AC_C_BIGENDIAN

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


# ---------------------------------------------------------------------------
# see if the user wants to override use of long long
AC_ARG_ENABLE(override-longlong,
[  --enable-override-longlong
                          overide use of long long with GCC],
  AC_DEFINE(DISABLE_LONG_LONG))


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

AC_OUTPUT([Makefile text/Makefile lib/Makefile 
   jni/Makefile java/org/greenstone/mgpp/Makefile], 
[test -z "$CONFIG_HEADERS" || echo timestamp > stamp-h])
