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

AC_INIT(configure.in)
AM_INIT_AUTOMAKE(dc_gui, 0.53)
AM_CONFIG_HEADER(config.h)

dnl Pick up the Gnome macros.
AM_ACLOCAL_INCLUDE(macros)

GNOME_INIT
AC_ISC_POSIX
AC_PROG_CC
AM_PROG_CC_STDC
AC_HEADER_STDC

GNOME_COMPILE_WARNINGS
GNOME_X_CHECKS

dnl Add the languages which your application supports here.
ALL_LINGUAS="nl"
AM_GNU_GETTEXT

dnl Set PACKAGE_LOCALE_DIR in config.h.
if test "x${prefix}" = "xNONE"; then
  AC_DEFINE_UNQUOTED(PACKAGE_LOCALE_DIR, "${ac_default_prefix}/${DATADIRNAME}/locale")
else
  AC_DEFINE_UNQUOTED(PACKAGE_LOCALE_DIR, "${prefix}/${DATADIRNAME}/locale")
fi

dnl check for dctc
AC_CHECK_PROG(HAVE_DCTC,dctc,yes,no)
if test "x$HAVE_DCTC" = "xno"; then
    AC_MSG_ERROR(Cannot find dctc: Is dctc in path?)
fi

dctc_mini_maj_version=0
dctc_mini_min_version=79

AC_MSG_CHECKING(DCTC version)
tmp_dctc_version=`dctc 2>&1 | grep "^INFO" | sed "s/^.*v//" | sed "s/|//"`
dctc_major=`echo $tmp_dctc_version | cut -d . -f 1`
dctc_minor=`echo $tmp_dctc_version | cut -d . -f 2`
dctc_micro=`echo $tmp_dctc_version | cut -d . -f 3`
dctc_version="$dctc_major.$dctc_minor.$dctc_micro"
AC_MSG_RESULT($dctc_version)

if test $dctc_major -lt $dctc_mini_maj_version; then
	AC_MSG_ERROR(DCTC min version is $dctc_mini_maj_version.$dctc_mini_min_version.x)
fi

if test $dctc_major -eq $dctc_mini_maj_version; then
	if test $dctc_minor -lt $dctc_mini_min_version; then
		AC_MSG_ERROR(DCTC min version is $dctc_mini_maj_version.$dctc_mini_min_version.x)
	fi
fi

dnl check for hublist
AC_CHECK_PROG(HAVE_HUBLIST,hublist,yes,no)
if test "x$HAVE_HUBLIST" = "xno"; then
    AC_MSG_ERROR(Cannot find hublist: Is hublist in path?)
fi

dnl alter the location of Berkeley DB directories (lib/ and include/)
db_dir="";
AC_ARG_WITH(db,[  --with-db=DIR            Specify the directory where Berkeley DB is stored (the directory containing lib/ and include/], [db_dir=$withval])
if test "x$db_dir" != "x"; then
        LIBS="$LIBS -L$db_dir/lib"
        CPPFLAGS="$CPPFLAGS -I$db_dir/include"
fi

dnl Check if BerkeleyDB include is available
AC_CHECK_HEADERS(db.h, ,AC_MSG_ERROR(BerkeleyDB include required.))

dnl choose between auto-detected Berkeley DB using db.h content and manually try to link db
AC_ARG_ENABLE(manual-db-detect,[  --enable-manual-db-detect  Disable automatic Berkeley DB version detection using db.h], [manu_db="yes"], [manu_db="no"])

if test "x$manu_db" = "xyes"; then
   dnl choose which berkeleyDB library to use (4.0, 3.3, 3.2 or 3.1)
   AC_CHECK_LIB(db-4.0,db_env_create,HAVE_DB40=yes)
   if test "x$HAVE_DB40" = "xyes"; then
      AC_SUBST(berkeleydb_libs)
      berkeleydb_libs="-ldb-4.0"
   else
      AC_CHECK_LIB(db-3.3,db_env_create,HAVE_DB33=yes)
      if test "x$HAVE_DB33" = "xyes"; then
         AC_SUBST(berkeleydb_libs)
         berkeleydb_libs="-ldb-3.3"
      else
         AC_CHECK_LIB(db-3.2,db_env_create,HAVE_DB32=yes)
         if test "x$HAVE_DB32" = "xyes"; then
            AC_SUBST(berkeleydb_libs)
            berkeleydb_libs="-ldb-3.2"
         else
            AC_CHECK_LIB(db-3.1,db_env_create,HAVE_DB31=yes)
            if test "x$HAVE_DB31" = "xyes"; then
               AC_SUBST(berkeleydb_libs)
               berkeleydb_libs="-ldb-3.1"
            else
               AC_CHECK_LIB(db-3,db_env_create,HAVE_DB3=yes)
               if test "x$HAVE_DB3" = "xyes"; then
                  AC_SUBST(berkeleydb_libs)
                  berkeleydb_libs="-ldb-3"
                  AC_MSG_WARN(***************************************************************);
                  AC_MSG_WARN(***************************************************************);
                  AC_MSG_WARN(You use a BerkeleyDB that has not been tested with this program);
                  AC_MSG_WARN(result can be unexpected);
                  AC_MSG_WARN(***************************************************************);
                  AC_MSG_WARN(***************************************************************);
               else
                  AC_MSG_ERROR(no supported BerkeleyDB found (4.0,3.1, 3.2, 3.3))
               fi
            fi
         fi
      fi
   fi
else
   dnl try to guess Berkeley DB version using DB_VERSION_MAJOR and DB_VERSION_MINOR from db.h
   AC_MSG_CHECKING(Berkeley Database library version)

   if test "x$db_dir" != "x"; then
      db_include="$db_dir/include/db.h"
   else
      db_include="/usr/include/db.h"
   fi

   db_major=`grep DB_VERSION_MAJOR $db_include | awk '{print $3;}'`
   db_minor=`grep DB_VERSION_MINOR $db_include | awk '{print $3;}'`
   db_version="$db_major.$db_minor"

   AC_MSG_RESULT($db_version)

   if test $db_major -ne 3 -a $db_major -ne 4; then
      AC_MSG_ERROR(no supported BerkeleyDB found (4.x or 3.x). Your version is $db_version)
   fi

   AC_CHECK_LIB(db-$db_version,db_env_create,HAVE_DB=yes)
   if test "x$HAVE_DB" = "xyes"; then
      AC_SUBST(berkeleydb_libs)
      berkeleydb_libs="-ldb-$db_version"
   else
      AC_MSG_ERROR(db.h reports Berkeley DB version is $db_version but no db-$db_version library exists)
   fi
fi


AC_OUTPUT([
Makefile
macros/Makefile
src/Makefile
intl/Makefile
po/Makefile.in
doc/Makefile
doc/C/Makefile
doc/es/Makefile
])

