dnl Process this file with autoconf to produce a configure script.

dnl autoconf configuration script for the JNI part of the 
dnl au.com.pharos.gdbm Java package
dnl $Id: configure.in 37415 2023-03-06 02:41:51Z anupama $

AC_REVISION($Revision: 37415 $)

# Copyright (C) 1997 by Pharos IP Pty Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

AC_INIT(jni/gdbmjava.c)

dnl
dnl look for libgdbm if not in standard location
dnl
AC_ARG_WITH(gdbm, [  --with-gdbm=dir         Use local gdbm library and headers], gdbmdir=$withval)

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

# TODO: allow the user to specify whether they want a debugging
# library or not?

# check for compilers and other tools
AC_PROG_CC
AC_PROG_INSTALL
AC_PROG_LN_S

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"
    gsdlarch='64'
  fi
else
  AC_MSG_RESULT(no)
  if test -z "$ARCH_DEP_FLAGS" ; then
    ARCH_DEP_FLAGS=
    gsdlarch='32'
  fi
fi

export ARCH_DEP_FLAGS
AC_SUBST(ARCH_DEP_FLAGS)

AC_CHECK_SIZEOF(void *)
AC_CHECK_SIZEOF(int)
AC_CHECK_SIZEOF(long)

######## check for the JDK and tools within the JDK
######## look in the JAVA_HOME environment variable at configure-time
#AC_MSG_CHECKING([for the JDK])
#if test "z$JAVA_HOME" != z &&
#   test -d $JAVA_HOME &&
#  (test -d $JAVA_HOME/bin)
## The following used to be the test, but failed on newer versions of MacOS
## Cut back to something simpler, and rely on the later tests for JNIINC to confirm
## files like 'jni.h' can be found
##   test -d $JAVA_HOME && 
##   (test -d $JAVA_HOME/include || test -d $JAVA_HOME/../Headers)
#then
#	AC_SUBST(JAVA_HOME)
#	AC_MSG_RESULT([found in $JAVA_HOME])
#else
#	AC_MSG_ERROR([not found in \$JAVA_HOME])
#fi

## TODO: Check for specific header files in the JDK, or
## functions/definitions within those headers?

## TODO: guess which include directory to use under the JDK

#AC_PATH_PROG(javagdbm_cv_path_javah, javah, missing_javah)
#if test "$javagdbm_cv_path_javah" = missing_javah; then
#	AC_MSG_ERROR([javah not found.])
#fi

#AC_PATH_PROG(javagdbm_cv_path_java, java, missing)
#if test "$javagdbm_cv_path_java" = missing; then
#	AC_MSG_ERROR([java not found.])
#fi

# Replacing the above with a more streamlined test for 'javac'
# This should have the advantage of meaning its not mandated
# that JAVA_HOME must be set for it to locate a javac
AC_PROG_JAVAC
  
# check for gdbm library
#AC_CHECK_LIB(gdbm, gdbm_open, [javagdbm_cv_lib_gdbm="-lgdbm"],
#	[javagdbm_cv_lib_gdbm=no])
#if test "$javagdbm_cv_lib_gdbm" = "no"; then
#	AC_MSG_ERROR([no native gdbm library found.])
#fi
#LIBS="$javagdbm_cv_lib_gdbm"

# gdbm stuff copied from gsdl [kjdon]

dnl
dnl Check that the GDBM library is available
dnl
dnl check libgdbm manually, in case it's not in the standard location.

AC_MSG_CHECKING(that the GDBM library is available)
if test ! -z "$gdbmdir" ; then
  # look in given directory for header file
  if test -r $gdbmdir/include/gdbm.h ; then
    GDBM_INCLUDE="-I$gdbmdir/include" ;
  elif test -r $gdbmdir/gdbm.h ; then
    GDBM_INCLUDE="-I$gdbmdir" ;
  fi

  # look in given directory for library
  if test -r $gdbmdir/libgdbm.a ; then
    GDBM_LIBPATH="-L$gdbmdir" ;
  elif test -r $gdbmdir/lib/libgdbm.a ; then
    GDBM_LIBPATH="-L$gdbmdir/lib" ;
  elif test -r $gdbmdir/.libs/libgdbm.a ; then
    GDBM_LIBPATH="-L$gdbmdir/.libs" ;
  fi

  if test ! -z "$GDBM_LIBPATH" -a ! -z "$GDBM_INCLUDE" ; then
    AC_MSG_RESULT($gdbmdir) ;
  else
    AC_MSG_RESULT(can't find gdbm.h and libgdbm.a in $gdbmdir);
    AC_MSG_CHECKING(   looking for GDBM library in default locations)
  fi
fi

# look in default path and in /usr/local
if test -z "$GDBM_INCLUDE" ; then
  # try in system directory 
  AC_TRY_COMPILE(
  [#include <gdbm.h>
],
  [],
  success=yes,
  success=no
  )

  if test $success = "no"; then
# last chance - look in /usr/local (eg FreeBSD)
    GDBM_LIBPATH=-L/usr/local/lib
    GDBM_INCLUDE=-I/usr/local/include
    ac_save_CXXFLAGS="$CXXFLAGS"
    ac_save_LIBS="$LIBS"
      CXXFLAGS="$CXXFLAGS -I/usr/local/include"
      LIBS="$LIBS -L/usr/local/lib -lgdbm"
      AC_TRY_LINK(
  	[#include <gdbm.h>
#include <stdio.h>
	],
  	[printf("%s",gdbm_version);],
  	success=/usr/local,
  	success=no
      )
      CXXFLAGS=$ac_save_CXXFLAGS
      LIBS=$ac_save_LIBS
  fi

  AC_MSG_RESULT($success)
  if test $success = "no"; then
  AC_MSG_ERROR(GDBM Library not available - cannot install)
  fi
fi
dnl I don't think anything actually checks this define, but...
AC_DEFINE(HAVE_LIBGDBM , 1)

AC_SUBST(GDBM_LIBPATH)
AC_SUBST(GDBM_INCLUDE)

########## set compiler options
# TODO: is there a more appropriate way to guess which switches to use
# with which systems/compilers?
CFLAGS="-Wall -O2 -fpic"
PKG="au.com.pharos.gdbm"
AC_SUBST(CFLAGS)
AC_SUBST(PKG)
AC_SUBST(LIBS)

# 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="-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)

# write the output
AC_OUTPUT([Makefile jni/Makefile java/Makefile])
