dnl configure.in for libtabe
dnl
dnl Process this file with autoconf to produce a configure script.
dnl-----------------------------------------------------------------------
AC_INIT(src/tabe.h)
AC_CONFIG_AUX_DIR(script)
AC_CONFIG_HEADER(config.h:script/config.h.in)
AC_CANONICAL_HOST

dnl-----------------------------------------------------------------------
dnl libtabe configure special command line options
dnl-----------------------------------------------------------------------
AC_ARG_ENABLE(debug,
    [  --enable-debug          enable debug],
    [  test x$enableval = xyes && AC_DEFINE([DEBUG], 1, [Define if the debug option is enabled.]) ])

AC_ARG_ENABLE(merge_db,
    [  --enable-merge-db       merge the original db files with the new version.],
    [  test x$enableval = xyes && enable_merge_db=mergedb ],
    [  enable_merge_db='' ])

AC_ARG_WITH(db_inc,
    [  --with-db-inc=PATH      set the include dir of Berkeley DB.],
    [  test x$withval != x && with_db_inc="-I$withval"])

AC_ARG_WITH(db_lib,
    [  --with-db-lib=PATH      set the library dir of Berkeley DB.],
    [  test x$withval != x && test_db_lib="-L$withval"])

AC_ARG_WITH(db_bin,
    [  --with-db-bin=PATH      set the binary dir of Berkeley DB.])

AC_ARG_WITH(db_name,
    [  --with-db-name=NAME       set the library name of Berkeley DB.],
    [  test x$withval != x && with_db_name="$withval" ],
    [  with_db_name='db' ])

if test "$program_prefix" != NONE; then
    program_prefix="$program_prefix/"
else
    program_prefix=
fi
AC_SUBST(program_prefix)
AC_SUBST(enable_merge_db)

dnl-----------------------------------------------------------------------
dnl Update version information.
dnl-----------------------------------------------------------------------
verf='src/version.h'
RELEASE_VER=`awk '{if (/RELEASE_VER/) print $3}' $verf | sed 's/"\(.*\)"/\1/'`
CURRENT_VER=`awk '{if (/CURRENT_VER/) print $3}' $verf`
REVISION_VER=`awk '{if (/REVISION_VER/) print $3}' $verf`
AGE_VER=`awk '{if (/AGE_VER/) print $3}' $verf`
AC_SUBST(RELEASE_VER)
AC_SUBST(CURRENT_VER)
AC_SUBST(REVISION_VER)
AC_SUBST(AGE_VER)

dnl-----------------------------------------------------------------------
dnl Check compiler: You can specify in 'CC=<..> ./configure'
dnl-----------------------------------------------------------------------
OLD_CFLAGS=$CFLAGS
AC_PROG_CC
if test "x$GCC" != xyes; then
    compiler_type=

    AC_MSG_CHECKING([ANSI C compiler])
    CFLAGS="$OLD_CFLAGS -Aa"
    AC_TRY_RUN([ int main(){return 0;} ], [ compiler_type=ANSIC ],
	       [ compiler_type=UNKNOWN ], [ cross_compiler=1 ])
    if test $compiler_type = ANSIC; then
	AC_MSG_RESULT([yes])
    else
	AC_MSG_RESULT([no])
    fi
fi
CFLAGS=$OLD_CFLAGS

dnl-----------------------------------------------------------------------
dnl Supply default CFLAGS, if not specified by `CFLAGS=flags ./configure'
dnl-----------------------------------------------------------------------
if test -z "$CFLAGS"; then
    if test "x$GCC" = xyes; then
	optimize_opt=-O2
	CFLAGS='-Wall -fsigned-char'
    else
	optimize_opt=-O
	if test "x$compiler_type" = xANSIC; then
	    CFLAGS=-Aa
	else
	    CFLAGS=
	fi
    fi

    if test "x$enable_debug" = xyes; then
        CFLAGS="$CFLAGS -g"
    else
	CFLAGS="$CFLAGS $optimize_opt"
    fi
fi

dnl-----------------------------------------------------------------------
dnl libtool commands.
dnl-----------------------------------------------------------------------
AM_PROG_LIBTOOL
AC_SUBST(LIBTOOL_DEPS)

dnl-----------------------------------------------------------------------
dnl Checks for programs.
dnl-----------------------------------------------------------------------
AC_PROG_CPP
AC_PROG_LN_S
AC_PROG_MAKE_SET
AC_PROG_INSTALL

${INSTALL} -d testdir > /dev/null 2>&1
if test ! -d testdir; then
    INSTALL="$srcdir/script/install-sh -c"
    AC_SUBST(INSTALL)
fi
rm -rf testdir
AC_MSG_RESULT([checking for install -d: $INSTALL])


dnl-----------------------------------------------------------------------
dnl Checks for host type
dnl-----------------------------------------------------------------------
case "$host_os" in
    linux*)  
        AC_DEFINE([LINUX], 1, [Define for Linux system.])
        ;;
    freebsd*)
        AC_DEFINE(FREEBSD, 1, [Define for FreeBSD system.])
        ;;
    openbsd*)
	AC_DEFINE(OPENBSD, 1, [Define for OpenBSD system.])
        ;;
    netbsd*)
	AC_DEFINE(NETBSD, 1, [Define for NETBSD system.])
	;;
    solaris*)
	AC_DEFINE(SOLARIS, 1, [Define for Solaris system.])
	;;
    hp*)
        AC_DEFINE(HPUX, 1, [Define for HP-UX system.])
	;;
    darwin*)
	AC_DEFINE(DARWIN, 1, [Define for Darwin system.])
	;;
esac


dnl-----------------------------------------------------------------------
dnl Checks for Berkeley DB libraries.
dnl-----------------------------------------------------------------------
OLD_CPPFLAGS=$CPPFLAGS
OLD_LDFLAGS=$LDFLAGS
OLD_LIBS=$LIBS
CPPFLAGS="$OLD_CPPFLAGS $with_db_inc"
LDFLAGS="$OLD_LDFLAGS $test_db_lib"

for dbname in $with_db_name db db2 db3 db4
do
  AC_CHECK_LIB($dbname, db_open, [found_bdb=yes; test_db_lib="$test_db_lib -l$dbname"; LIBS="-l$dbname"; break])
  AC_CHECK_LIB($dbname, db_create, [found_bdb=yes; test_db_lib="$test_db_lib -l$dbname"; LIBS="-l$dbname"; break])
done

if test x$found_bdb != xyes; then
      AC_MSG_RESULT([You need Berkeley DB to compile libtabe.])
      AC_MSG_RESULT([Please use --with-db-lib and/or --with-db-name to configure.])
      exit 1
fi

AC_CHECK_HEADERS(db.h, ,
    [ AC_MSG_RESULT([db.h is not available.])
      AC_MSG_RESULT([Please use --with-db-inc to configure.])
      exit 1 ])

AC_MSG_CHECKING([BerkeleyDB API])
AC_TRY_RUN([
#include <errno.h>
#include <stdlib.h>
#include <db.h>
DB *dbp;
DBC *dbcp;
DB_BTREE_STAT *sp;
#define DB_VERSION (DB_VERSION_MAJOR*100000+DB_VERSION_MINOR*1000+DB_VERSION_PATCH)

int main(void)
{
#if DB_VERSION >= 300000
errno = db_create(&dbp, NULL, 0);
#endif
if(errno) return errno;


#if DB_VERSION >= 401025
errno = dbp->open(dbp, NULL, "foo.db", NULL, DB_BTREE, DB_CREATE, 0644);
#elif DB_VERSION >= 300000
errno = dbp->open(dbp, "foo.db", NULL, DB_BTREE, DB_CREATE, 0644);
#else
errno = db_open("foo.db", DB_BTREE, DB_CREATE, 0644, NULL, NULL, &dbp);
#endif
if(errno) return errno;

#if DB_VERSION >= 303011
errno = dbp->stat(dbp, &sp, 0); 
#else
errno = dbp->stat(dbp, &sp, NULL, 0); 
#endif
if(errno) return errno;

#if DB_VERSION >= 206004
errno = dbp->cursor(dbp, NULL, &dbcp, 0);
#else
errno = dbp->cursor(dbp, NULL, &dbcp);
#endif
if(errno) return errno;
  return 0;
}
],
	[ AC_MSG_RESULT([ok]) 
	  rm -f foo.db ],
	[ AC_MSG_RESULT([BerkeleyDB header and library mismatch, or API changes.])
	  exit 1 ])

if test x$with_db_lib = x; then
    with_db_lib="$test_db_lib"
else
    with_db_lib="$test_db_lib -rpath $with_db_lib"
fi
if test x$with_db_bin = x; then
    with_db_bin="$PATH"
else
    with_db_bin="$with_db_bin:$PATH"
fi
AC_SUBST(with_db_inc)
AC_SUBST(with_db_lib)
AC_SUBST(with_db_bin)

dnl-----------------------------------------------------------------------
dnl Checks for X11R6 library and header files.
dnl-----------------------------------------------------------------------
AC_PATH_X
if test x$no_x = xyes; then
    AC_MSG_RESULT([You need X11R6 header files to compile libbims.])
    AC_MSG_RESULT([Please use --x-includes to configure.])
    build_supports=
else
    build_supports=bims
fi
AC_SUBST(x_includes)
AC_SUBST(x_libraries)
AC_SUBST(build_supports)

dnl-----------------------------------------------------------------------
dnl Checks for header files.
dnl-----------------------------------------------------------------------
AC_HEADER_STDC
AC_CHECK_HEADERS(unistd.h)

dnl-----------------------------------------------------------------------
dnl Checks for typedefs, structures, and compiler characteristics.
dnl-----------------------------------------------------------------------
AC_C_CONST

dnl-----------------------------------------------------------------------
dnl Checks for library functions.
dnl-----------------------------------------------------------------------
AC_FUNC_MEMCMP
AC_CHECK_FUNCS(strdup strerror strstr)

AC_OUTPUT(Makefile:script/Makefile.in \
	  src/Makefile \
	  src/supports/bims/Makefile \
	  util/Makefile \
	  util/tsipackdb \
	  tsi-src/Makefile)

