dnl Process this file with autoconf to produce a configure script.
dnl $Id: configure.in,v 1.63 2002/04/10 20:07:40 richard Exp $
AC_INIT(src/jigdo-file.cc)
AC_CONFIG_HEADER(src/config.h)
dnl ______________________________________________________________________

dnl Version number of our program.
read JIGDO_VERSION < "$srcdir/VERSION"
AC_DEFINE_UNQUOTED(JIGDO_VERSION, "$JIGDO_VERSION")
AC_SUBST(JIGDO_VERSION)
dnl ______________________________________________________________________

dnl installDevel is given 2 arguments: Name of "devel" package on
dnl Debian-based systems, name on RPM-based systems
if test -f "/etc/debian_version"; then
    installDevel() {
    AC_MSG_RESULT([   * (Your system appears to be Debian-based; try])
    AC_MSG_RESULT([   * installing the \`$1-dev' package.)])
    }
elif test -x "/usr/bin/rpm" -o -x "/usr/local/bin/rpm"; then
    installDevel() {
    AC_MSG_RESULT([   * (Your system appears to be RPM-based; try])
    AC_MSG_RESULT([   * installing the package named \`$2-devel' or])
    AC_MSG_RESULT([   * similar.)])
    }
else
    installDevel() {
    AC_MSG_RESULT([   * (If the software on your system is managed by a])
    AC_MSG_RESULT([   * package manager like RPM, try installing the])
    AC_MSG_RESULT([   * package named \`$2-devel' or similar.)])
    }
fi
dnl ______________________________________________________________________

dnl Checks for programs.
AC_PROG_AWK
AC_LANG_C
AC_PROG_CC
AC_LANG_CPLUSPLUS
AC_PROG_CXX
if test "$GXX" = "yes"; then
    CFLAGS="$CFLAGS -Wall -W"
    CXXFLAGS="$CXXFLAGS -Wall -W -Wpointer-arith -Wconversion -Wmissing-prototypes -Woverloaded-virtual"
fi
AC_PROG_INSTALL
dnl ____________________

AC_CACHE_CHECK(whether the C++ compiler is recent enough,
               jigdo_cv_prog_cxx_recent,
    AC_TRY_COMPILE(
    #include <vector>
    namespace A {
      namespace B {
        template<typename T = double> struct X {
          template<class U> void function(T* t, U* u);
        };
      }
      template<> void B::X<void>::function(void* t, int* u);
    },, jigdo_cv_prog_cxx_recent="yes", jigdo_cv_prog_cxx_recent="no")
)
if test "$jigdo_cv_prog_cxx_recent" = "no"; then
    AC_MSG_RESULT([   * Your compiler failed to recognize some advanced C++])
    AC_MSG_RESULT([   * constructs - it might be too old to compile jigdo.])
    AC_MSG_RESULT([   * In case compilation fails, try upgrading to a newer])
    AC_MSG_RESULT([   * compiler, e.g. GCC 2.95 or later.])
fi
AC_CACHE_SAVE
dnl ______________________________________________________________________

dnl Checks for header files.
AC_HEADER_STDC
AC_CHECK_HEADERS(stddef.h getopt.h unistd.h limits.h string.h)

dnl Checks for libraries and accompanying header files
AC_CHECK_LIB(z, zlibVersion, have_zlib="yes", have_zlib="no")
AC_CHECK_HEADER(zlib.h, have_zlib_h="yes", have_zlib_h="no")
if test "$have_zlib" = "no" -o "$have_zlib_h" = "no"; then
    AC_MSG_RESULT([   * Please install zlib, it is needed by all of the])
    AC_MSG_RESULT([   * programs.])
    installDevel "zlib1g" "zlib"
fi
LIBS="-lz $LIBS"


AC_MSG_CHECKING(for value of --with-libdb)
AC_ARG_WITH(libdb,
    [  --with-libdb            Use libdb3 (necessary for jigdo-file's cache) [yes]], #'
    jigdo_libdb="$withval", jigdo_libdb="yes")
AC_MSG_RESULT(\"$jigdo_libdb\")
if test "$jigdo_libdb" = "yes"; then
    dnl libdb V3 by "Sleepycat". Apparently the only -l switch
    dnl compatible across RedHat and Debian is -ldb. In case -ldb isn't
    dnl there or is V2 or V1, try a few alternative locations. db_create
    dnl is only available in the lib from V3 onwards.
    for db in "db" "db3" "db-3" "db-3.1" "db-3.2" "db-3.3"; do
        AC_CHECK_LIB($db, db_create, have_libdb="$db", have_libdb="no")
        if test "$have_libdb" != "no"; then break; fi
    done
    AC_CHECK_HEADER(db.h, have_db_h="yes", have_db_h="no")
    if test "$have_libdb" = "no" -o "$have_db_h" = "no"; then
        AC_MSG_RESULT([   * libdb3 not found - either install it, or use])
        AC_MSG_RESULT([   * \`configure --without-libdb' to disable the db3])
        AC_MSG_RESULT([   * file cache of jigdo-file. If this check should not])
        AC_MSG_RESULT([   * have failed, force e.g. -ldb to be used with])
        AC_MSG_RESULT([   * \`configure --with-libdb=-ldb'])
        installDevel "libdb3" "libdb3.3"
        AC_MSG_ERROR(libdb3 not found.)
    else
        LIBS="-l$have_libdb $LIBS"
    fi
    AC_DEFINE(HAVE_LIBDB, 1) dnl make compilation fail if libdb not there
elif test "$jigdo_libdb" != "no" -a "$jigdo_libdb" != "NO"; then
    dnl Pass argument (e.g. "-ldb") to compiler
    LIBS="$jigdo_libdb $LIBS"
    AC_DEFINE(HAVE_LIBDB, 1)
else
    AC_DEFINE(HAVE_LIBDB, 0)
fi
dnl ________________________________________

AC_MSG_CHECKING(for value of --with-gui)
AC_ARG_WITH(gui,
    [  --with-gui              Build the jigdo GUI application [yes]],
    jigdo_gui="$withval", jigdo_gui="yes")
AC_MSG_RESULT(\"$jigdo_gui\")
dnl ____________________

dnl GTK+
AC_MSG_CHECKING(for GTK+ version)
jigdo_gtkver="`gtk-config --version 2>&1`"
AC_MSG_RESULT($jigdo_gtkver)
case "$jigdo_gtkver" in
    1.2.*|1.3.*) ;;
    *)
    if test "$jigdo_gui" = "yes"; then
    AC_MSG_RESULT([   * GTK+ not installed, or the installed version is not])
    AC_MSG_RESULT([   * 1.2.x, or gtk-config is not in \$PATH. Please])
    AC_MSG_RESULT([   * install GTK+ 1.2, it is needed by \`jigdo'.])
    installDevel "libgtk1.2" "libgtk1.2"
    fi
esac
noConfig="# gtk-config not found - add compiler switches here"
GTKCFLAGS="`gtk-config --cflags 2>/dev/null || echo $noConfig`"
GTKLIBS="`gtk-config --libs 2>/dev/null || echo $noConfig`"
AC_SUBST(GTKCFLAGS)
AC_SUBST(GTKLIBS)
dnl ____________________

dnl libwww
AC_MSG_CHECKING(for libwww version)
jigdo_libwwwver="`libwww-config --version 2>&1`"
AC_MSG_RESULT($jigdo_libwwwver)
LIBWWWCFLAGS="# libwww-config not found - add compiler switches here"
LIBWWWLIBS="$LIBWWWCFLAGS"
case "$jigdo_libwwwver" in
    dnl Extra [] around [0-9] style wildcards, or M4 swallows them
    [[6-9]*|5.[4-9]*|5.[1-9][0-9]*|5.3.[2-9]*|5.3.[1-9][0-9]*])
        LIBWWWCFLAGS="`libwww-config --cflags 2>/dev/null`"
        LIBWWWLIBS="`libwww-config --libs 2>/dev/null`"
        dnl Some versions of libwww-config include "-I/usr/include" in their
        dnl --cflags output, which completely upsets some versions of GCC.
        LIBWWWCFLAGS=`echo " $LIBWWWCFLAGS " | sed -e 's% -I/usr/include/* % %'`
        ;;
    *)
    if test "$jigdo_gui" = "yes"; then
    AC_MSG_RESULT([   * libwww not installed, or the installed version is])
    AC_MSG_RESULT([   * too old, or libwww-config is not in \$PATH. Please])
    AC_MSG_RESULT([   * install libwww 5.3.2 or later, it is needed by the])
    AC_MSG_RESULT([   * \`jigdo' program.])
    installDevel "libwww" "w3c-libwww-devel/libopenssl0"
    fi
esac
AC_SUBST(LIBWWWCFLAGS)
AC_SUBST(LIBWWWLIBS)
dnl ____________________

dnl Checks for typedefs, structures, and compiler characteristics.
AC_TYPE_SIZE_T
AC_MSG_CHECKING(whether we are compiling for Windows)
AC_EGREP_CPP(poorsoul, [
#if defined WINNT || defined __WINNT || defined __WINNT__ || defined WIN32 \
    || defined _WIN32 || defined __WIN32 || defined __WIN32__
  poorsoul
#endif], is_windows=yes, is_windows=no)
AC_MSG_RESULT($is_windows)
if test "$is_windows" = yes; then
    AC_DEFINE(WINDOWS)
    exe=".exe"
else
    AC_DEFINE(UNIX)
    exe=""
fi
AC_SUBST(exe)
dnl ____________________

dnl Both MinGW32 and glib declare 'struct dirent' and 'struct DIR'. If
dnl there are problems using the relevant headers together, do some
dnl really dirty things with the preprocessor to disable the glib
dnl declaration.
dirent_hack="no"
if test "$is_windows" = yes; then
    CPPFLAGS_orig="$CPPFLAGS"
    CPPFLAGS="$CPPFLAGS $GTKCFLAGS"
    AC_MSG_CHECKING(for conflicting 'struct dirent' in dirent.h and glib.h)
    AC_TRY_COMPILE([#include <dirent.h>
                    #include <glib.h>], , dirent_hack="no",dirent_hack="yes")
    if test "$dirent_hack" = "yes"; then
        AC_TRY_COMPILE([#include <dirent.h>
                        #define dirent other_dirent
                        #define DIR other_DIR
                        #include <glib.h>], , , dirent_hack="no")
    fi
    AC_MSG_RESULT($dirent_hack)
    CPPFLAGS="$CPPFLAGS_orig"
fi
if test "$dirent_hack" = "yes"; then
    AC_DEFINE(DIRENT_HACK, 1)
else
    AC_DEFINE(DIRENT_HACK, 0)
fi
dnl ____________________

AC_MSG_CHECKING(for value of --with-uint64)
AC_ARG_WITH(uint64,
    [  --with-uint64=TYPE      Specify unsigned type of at least 64 bits [auto]
                          (Replace spaces with underscores in TYPE)],
    jigdo_uint64="yes", jigdo_uint64="auto")
AC_MSG_RESULT(\"$jigdo_uint64\")

AC_CACHE_CHECK(for unsigned long long, jigdo_cv_have_ulonglong,
    AC_TRY_COMPILE(, unsigned long long x = 0,
                   jigdo_cv_have_ulonglong=yes, jigdo_cv_have_ulonglong=no))
if test "$jigdo_cv_have_ulonglong" = "yes"; then
    AC_DEFINE(HAVE_UNSIGNED_LONG_LONG, 1)
else
    AC_DEFINE(HAVE_UNSIGNED_LONG_LONG, 0)
fi

if test "$jigdo_uint64" = auto; then
    AC_CHECK_SIZEOF(unsigned long, 0)
    AC_CHECK_SIZEOF(unsigned long long, 0)
    if test "$ac_cv_sizeof_unsigned_long" -ge 8; then
        jigdo_uint64='unsigned long'
    elif test "$ac_cv_sizeof_unsigned_long_long" -ge 8; then
        jigdo_uint64='unsigned long long'
    else
        if test "$jigdo_cv_have_ulonglong" = "yes"; then
            jigdo_uint64='unsigned long long'
        else
            jigdo_uint64='unsigned long'
        fi
        AC_MSG_RESULT([   * Could not find an unsigned type with at least 64])
        AC_MSG_RESULT([   * bits (or maybe char has >8 bits?) - defaulting to])
        AC_MSG_RESULT([   * \`$jigdo_uint64'. You may want to override])
        AC_MSG_RESULT([   * this using --with-uint64=TYPE])
    fi
elif test "$jigdo_uint64" = yes -o "$jigdo_uint64" = no; then
    AC_MSG_ERROR(Invalid argument to --with-uint64 option)
else
    jigdo_uint64=`echo $jigdo_uint64 | sed -e 's/_/ /g'`
fi
AC_MSG_CHECKING(for 64 bit unsigned integer type)
AC_MSG_RESULT($jigdo_uint64)
AC_DEFINE_UNQUOTED(TYPE_UINT64, $jigdo_uint64)

AC_CACHE_CHECK(whether operator <<(uint64) works,
               jigdo_cv_prog_cxx_outuint64,
    AC_TRY_COMPILE(
    [ #include <iostream>
      namespace std { }
      using namespace std; ],
    [ $jigdo_uint64 x;
      cout << x << endl; ],
    jigdo_cv_prog_cxx_outuint64="yes", jigdo_cv_prog_cxx_outuint64="no")
)
if test "$jigdo_cv_prog_cxx_outuint64" = "yes"; then
    AC_DEFINE(HAVE_OUTUINT64, 1)
else
    AC_DEFINE(HAVE_OUTUINT64, 0)
fi
dnl ____________________

dnl Checks for library functions.
AC_CHECK_FUNCS(lstat truncate ftruncate mmap fileno snprintf _snprintf)

dnl Check whether reading width of TTY via ioctl() works
AC_MSG_CHECKING(for TIOCGWINSZ ioctl)
AC_TRY_COMPILE(
    [ #include <stdio.h>
      #include <sys/ioctl.h>],
    [ struct winsize w; int i;
      ioctl(fileno(stdout), TIOCGWINSZ, &w);
      i = w.ws_col; ],
    jigdo_have_ioctl_winsz="yes", jigdo_have_ioctl_winsz="no")
AC_MSG_RESULT($jigdo_have_ioctl_winsz)
if test "$jigdo_have_ioctl_winsz" = "yes"; then
    AC_DEFINE(HAVE_IOCTL_WINSZ, 1)
elif test "$is_windows" = no; then
    AC_MSG_RESULT([   * jigdo-file progress reports will not be formatted])
    AC_MSG_RESULT([   * nicely, because no way has been found to determine])
    AC_MSG_RESULT([   * the screen width in characters.])
    AC_DEFINE(HAVE_IOCTL_WINSZ, 0)
else
    AC_DEFINE(HAVE_IOCTL_WINSZ, 0)
fi

dnl On native Windows (MinGW32), there is no snprintf, just _snprintf
if test "$ac_cv_func_snprintf" = "no" -a "$ac_cv_func__snprintf" = "yes"; then
    AC_DEFINE(snprintf, _snprintf)
fi
dnl ______________________________________________________________________

dnl Check for Native Language Support (gettext)
AC_MSG_CHECKING(for value of --enable-nls)
AC_ARG_ENABLE(nls,
    [  --enable-nls            Use Native Language Support [yes]],
    USE_NLS=$enableval, USE_NLS=yes)
AC_MSG_RESULT(\"$USE_NLS\")
AC_SUBST(USE_NLS)
CATALOGS=""
if test "$USE_NLS" = "yes"; then
    AC_DEFINE(ENABLE_NLS, 1)

    AC_CHECK_LIB(intl, dgettext, x="yes", x="no")
    if test "$x" = "yes"; then
        LIBS="$LIBS -lintl";
        AC_CHECK_LIB(iconv, iconv, LIBS="$LIBS -liconv")
    fi
    AC_CHECK_HEADER(libintl.h, x="yes", x="no")
    if test "$x" = "no"; then
        AC_MSG_RESULT([   * Make sure gettext is installed, or use])
        AC_MSG_RESULT([   * --disable-nls to switch off gettext support.])
        AC_MSG_ERROR(libintl.h not found.)
    fi
    AC_MSG_CHECKING(whether dgettext works)
    AC_TRY_LINK([#include <libintl.h>], [dgettext("", "")], x="yes", x="no")
    AC_MSG_RESULT("$x")
    if test "$x" = "no"; then
        AC_MSG_RESULT([   * Make sure gettext is installed, or use])
        AC_MSG_RESULT([   * --disable-nls to switch off gettext support.])
        AC_MSG_ERROR(dgettext() call could not be linked.)
    fi
    dnl ____________________

    AC_MSG_CHECKING(for catalogs to be installed)
    ALL_LINGUAS=`(cd "$srcdir/po" && echo *.po *.pox "") \
        | sed -e "s/\**\.pox* / /g"`
    if test "$LINGUAS"; then
        NEW_LINGUAS=""
        for provided in $ALL_LINGUAS; do
            x=""
            for wanted in $LINGUAS; do
                case "$provided" in "$wanted"*) x=" $provided";; esac
                case "$wanted" in "$provided"*) x=" $provided";; esac
            done
            NEW_LINGUAS="$NEW_LINGUAS$x"
        done
        LINGUAS="$NEW_LINGUAS"
        AC_MSG_RESULT(LINGUAS:$LINGUAS)
    else
        LINGUAS="$ALL_LINGUAS"
        AC_MSG_RESULT($LINGUAS)
    fi
    for x in $LINGUAS; do CATALOGS="$CATALOGS $x.gmo"; done
else
    AC_DEFINE(ENABLE_NLS, 0)
fi
AC_SUBST(CATALOGS)
dnl ______________________________________________________________________

dnl Checks for other --enable-xxx, --with-xxx switches
AC_MSG_CHECKING(for value of --enable-debug)
AC_ARG_ENABLE(debug,
    [  --enable-debug          Compile in lots of additional debugging code [no]],
    jigdo_debug=$enableval, jigdo_debug=no)
AC_MSG_RESULT(\"$jigdo_debug\")
DEBUG_PROGRAMS=""
if test "$jigdo_debug" = yes; then
    AC_DEFINE(DEBUG, 1)
    DEBUG_PROGRAMS='$(debug-programs)'
elif test "$jigdo_debug" != no; then
    AC_MSG_ERROR(Invalid argument to --enable-debug option)
else
    AC_DEFINE(DEBUG, 0)
fi
AC_SUBST(DEBUG_PROGRAMS)
dnl ________________________________________

AC_MSG_CHECKING(for value of --enable-ccmalloc)
AC_ARG_ENABLE(ccmalloc,
    [  --enable-ccmalloc       Use the ccmalloc memory leak detector [no]],
    jigdo_ccmalloc=$enableval, jigdo_ccmalloc=no)
AC_MSG_RESULT(\"$jigdo_ccmalloc\")
CCMALLOC=""
if test "$jigdo_ccmalloc" = yes; then
    AC_DEFINE(CCMALLOC, 1)
    CCMALLOC='$(CCMALLOC)'
elif test "$jigdo_ccmalloc" != no; then
    AC_MSG_ERROR(Invalid argument to --enable-ccmalloc option)
else
    AC_DEFINE(CCMALLOC, 0)
fi
AC_SUBST(CCMALLOC)
dnl ______________________________________________________________________

dnl Touch TAGS to prevent it from being recreated when a normal user
dnl builds the program. TAGS should only be updated once changes are
dnl made to the source.
touch "$srcdir/src/TAGS"

dnl Now that both jigdo_gui and exe are set, build list of programs to
dnl build.
if test "$jigdo_gui" = "yes"; then
    BUILD_TARGETS="jigdo-file$exe jigdo$exe #"
    INSTALL_TARGETS="install-jigdo-file install-jigdo-lite install-jigdo #"
else
    BUILD_TARGETS="jigdo-file$exe # jigdo$exe"
    INSTALL_TARGETS="install-jigdo-file install-jigdo-lite # install-jigdo"
fi
AC_SUBST(BUILD_TARGETS)
AC_SUBST(INSTALL_TARGETS)

SRC_MAKEDEPS="$srcdir/src/Makedeps"
AC_SUBST_FILE(SRC_MAKEDEPS)

AC_OUTPUT(Makefile doc/Makefile src/Makefile po/Makefile)
