#MIN_CONFIG(3.3)

AM_INIT_AUTOMAKE(ktorrent,2.2.8)

dnl #### Check for FSEEK variants
AC_SYS_LARGEFILE
AC_FUNC_FSEEKO
AC_CHECK_FUNCS(fopen64, ktorrent_cv_func_fopen64=yes, ktorrent_cv_func_fopen64=no)
AC_CHECK_FUNCS(fseeko64, ktorrent_cv_func_fseek64=yes, ktorrent_cv_func_fseek64=no)
AC_CHECK_FUNCS(ftello64, ktorrent_cv_func_ftell64=yes, ktorrent_cv_func_ftell64=no)
AC_CHECK_FUNCS(fstat64, ktorrent_cv_func_fstat64=yes, ktorrent_cv_func_fstat64=no)
AC_CHECK_FUNCS(stat64, ktorrent_cv_func_stat64=yes, ktorrent_cv_func_stat64=no)
AC_CHECK_FUNCS(ftruncate64, ktorrent_cv_func_ftruncate64=yes, ktorrent_cv_func_ftruncate64=no)
AC_CHECK_FUNCS(lseek64, ktorrent_cv_func_lseek64=yes, ktorrent_cv_func_lseek64=no)
AC_CHECK_FUNCS(mmap64, ktorrent_cv_func_mmap64=yes, ktorrent_cv_func_mmap64=no)
AC_CHECK_FUNCS(munmap64, ktorrent_cv_func_munmap64=yes, ktorrent_cv_func_munmap64=no)
AC_CHECK_FUNCS(posix_fallocate, ktorrent_cv_func_posix_fallocate=yes,ktorrent_cv_func_posix_fallocate=no)
AC_CHECK_FUNCS(posix_fallocate64, ktorrent_cv_func_posix_fallocate64=yes,ktorrent_cv_func_posix_fallocate64=no)
AC_CHECK_FUNCS(statvfs, ktorrent_cv_func_statvfs=yes,ktorrent_cv_func_statvfs=no)
AC_CHECK_FUNCS(statvfs64, ktorrent_cv_func_statvfs64=yes,ktorrent_cv_func_statvfs64=no)

AC_LANG_PUSH([C++])
AC_CHECK_HEADERS([xfs/xfs.h])
AC_CHECK_TYPES([__u64, __s64], [], [], [#include <xfs/xfs.h>])
AC_LANG_POP

if test "$ktorrent_cv_func_posix_fallocate" = yes ; then
    AC_DEFINE(HAVE_POSIX_FALLOCATE, 1)
else
    AC_DEFINE(HAVE_POSIX_FALLOCATE, 0)
fi

if test "$ktorrent_cv_func_posix_fallocate64" = yes ; then
    AC_DEFINE(HAVE_POSIX_FALLOCATE64, 1)
else
    AC_DEFINE(HAVE_POSIX_FALLOCATE64, 0)
fi

if test "$ktorrent_cv_func_fopen64" = yes ; then
    AC_DEFINE(HAVE_FOPEN64, 1)
else
    AC_DEFINE(HAVE_FOPEN64, 0)
fi

if test "$ktorrent_cv_func_fseek64" = yes -a "$ktorrent_cv_func_ftell64" = yes; then
    AC_DEFINE(HAVE_FSEEKO64, 1)
else
    AC_DEFINE(HAVE_FSEEKO64, 0)
fi

if test "$ktorrent_cv_func_fstat64" = yes -a "$ktorrent_cv_func_stat64" = yes ; then
	AC_DEFINE(HAVE_STAT64,1)
else
	AC_DEFINE(HAVE_STAT64,0)
fi

if test "$ktorrent_cv_func_ftruncate64" = yes ; then
	AC_DEFINE(HAVE_FTRUNCATE64,1)
else
	AC_DEFINE(HAVE_FTRUNCATE64,0)
fi

if test "$ktorrent_cv_func_lseek64" = yes ; then
	AC_DEFINE(HAVE_LSEEK64,1)
else
	AC_DEFINE(HAVE_LSEEK64,0)
fi

if test "$ktorrent_cv_func_mmap64" = yes ; then
    AC_DEFINE(HAVE_MMAP64, 1)
else
    AC_DEFINE(HAVE_MMAP64, 0)
fi

if test "$ktorrent_cv_func_munmap64" = yes ; then
    AC_DEFINE(HAVE_MUNMAP64, 1)
else
    AC_DEFINE(HAVE_MUNMAP64, 0)
fi

if test "$ktorrent_cv_func_statvfs" = yes ; then
    AC_DEFINE(HAVE_STATVFS, 1)
else
    AC_DEFINE(HAVE_STATVFS, 0)
fi

if test "$ktorrent_cv_func_statvfs64" = yes ; then
    AC_DEFINE(HAVE_STATVFS64, 1)
else
    AC_DEFINE(HAVE_STATVFS64, 0)
fi

KDE_ENABLE_HIDDEN_VISIBILITY

KDE_CHECK_LIB(gmp, __gmpz_init,	[have_gmp=yes],
	[
		have_gmp=no
		AC_MSG_ERROR(libgmp is required to install this program)
	])

KDE_CHECK_HEADER(gmp.h, [have_gmp=yes],
	[
		have_gmp=no
		AC_MSG_ERROR(libgmp is required to install this program)
	])

KDE_CHECK_HEADER(GeoIP.h, [have_system_geoip=yes], [have_system_geoip=no])

KDE_INIT_DOXYGEN([The API Reference], [Version $VERSION])

AC_ARG_ENABLE(
	memleak-check,
	[AS_HELP_STRING(--enable-memleak-check,[Enable memory leak checking])],
	[CXXFLAGS="$CXXFLAGS -DKT_LEAKTRACE"])
	
AC_ARG_ENABLE(
	profile,
	[AS_HELP_STRING(--enable-profile,[Enable profiling])],
	[CXXFLAGS="$CXXFLAGS -DKT_PROFILE"])


AC_ARG_ENABLE(geoip,
     [AS_HELP_STRING(--enable-geoip,[Enables builtin GeoIP database installation])],
     [case "${enableval}" in
       yes) geoip=true ;;
       no)  geoip=false ;;
       *) AC_MSG_ERROR(bad value ${enableval} for --enable-geoip) ;;
     esac],[geoip=true])

AC_ARG_ENABLE(system-geoip,
    [AS_HELP_STRING(--enable-system-geoip,[Enables use of system-wide GeoIP database (implies --disable-geoip) (auto by default)])],
    [case "${enableval}" in
        yes) if test "$have_system_geoip" = yes; then
                system_geoip=true
                geoip=false
             else
                AC_MSG_ERROR(cannot enable system geoip. GeoIP library headers were not found on your system)
             fi ;;
        no) system_geoip=false ;;
        *) AC_MSG_ERROR(bad value ${enableval} for --enable-system-geoip) ;;
    esac],[system_geoip=auto])

if test x$system_geoip = xauto; then
    if test "$have_system_geoip" = yes; then
        system_geoip=true
        geoip=false
    else
        system_geoip=false
    fi
fi

AM_CONDITIONAL(ENABLE_GEOIP, test x$geoip = xtrue)
AM_CONDITIONAL(USE_SYSTEM_GEOIP, test x$system_geoip = xtrue)

if test x$system_geoip = xtrue; then
    AC_DEFINE(USE_SYSTEM_GEOIP, 1, [Use system geoip])
fi


AC_ARG_ENABLE(builtin-country-flags,
    [AS_HELP_STRING(--enable-builtin-country-flags,[Install and prefer builtin country flags to the ones provided by KDE (auto by default)])],
    [case "${enableval}" in
        yes) builtin_country_flags=true ;;
        no) builtin_country_flags=false ;;
        *) AC_MSG_ERROR(bad value ${enableval} for --enable-builtin-country-flags) ;;
    esac],[builtin_country_flags=auto])

if test x$builtin_country_flags = xauto; then
    if test x$geoip = xtrue; then
        builtin_country_flags=true
    else
        builtin_country_flags=false
    fi
fi

AM_CONDITIONAL(ENABLE_BUILTIN_COUNTRY_FLAGS, test x$builtin_country_flags = xtrue)

AC_ARG_ENABLE(torrent-mimetype,
     [  --enable-torrent-mimetype Enables the installation of the torrent mimetype which uses a KTorrent based icon],
     [case "${enableval}" in
       yes) mimetype_install=true ;;
       no)  mimetype_install=false ;;
       *) AC_MSG_ERROR(bad value ${enableval} for --enable-torrent-mimetype) ;;
     esac],[mimetype_install=false])

AM_CONDITIONAL(ENABLE_TORRENT_MIMETYPE, test x$mimetype_install = xtrue)

## -- BEGIN -- ADDED BY Kevin ---

# Check for pkg-config manually first, as if its not installed the
# PKG_PROG_PKG_CONFIG macro won't be defined.
AC_CHECK_PROG(have_pkg_config, pkg-config, yes, no)

if test x"$have_pkg_config" = "xno"; then
    AC_MSG_ERROR(pkg-config is required to install this program)
fi

m4_pattern_allow(PKG_CONFIG_MIN_VERSION)
PKG_PROG_PKG_CONFIG

AC_ARG_WITH([avahi], AS_HELP_STRING([--without-avahi], [Build without avahi/ZeroConf support (default: test)]))

avahi=false
if test "x$with_avahi" != "xno"; then
 PKG_CHECK_MODULES( AVAHI,  avahi-client >= 0.6.10,
 [
   AC_SUBST(AVAHI_CFLAGS)
   AC_SUBST(AVAHI_LIBS)
   avahi=true
 ],
 [
   missing_avahi_warning=yes
   avahi=false
 ])

 PKG_CHECK_MODULES( AVAHI_TQT,  avahi-tqt >= 0.6.10,
 [
   AC_SUBST(AVAHI_TQT_CFLAGS)
   AC_SUBST(AVAHI_TQT_LIBS)
   avahi=true
 ],
 [
   missing_avahi_warning=yes
   avahi=false
 ])
fi

AM_CONDITIONAL(COMPILE_ZEROCONF, test x$avahi = xtrue)
