#MIN_CONFIG(3.3.5)

AM_INIT_AUTOMAKE(tork, 0.1)
AC_C_BIGENDIAN
AC_CHECK_KDEMAXPATHLEN

# ---- Taken from KOffice ----------------------
# --- Check for KDE 3.2 or 3.3 or 3.4 or 3.5 ---

AC_MSG_CHECKING([for KDE version])

AC_LANG_SAVE
AC_LANG_CPLUSPLUS
tdeversion_save_CXXFLAGS="$CXXFLAGS"
tdeversion_safe_LIBS="$LIBS"
LIBS="$LIBS $X_EXTRA_LIBS"
CXXFLAGS="$CXXFLAGS $all_includes"

AC_COMPILE_IFELSE([
#include <tdeversion.h>
#if ! ( KDE_IS_VERSION( 3, 2, 90 ) )
#error KDE 3.2
#endif
],
        need_kde32_compat="no"
,
        need_kde32_compat="yes"
)

AC_COMPILE_IFELSE([
#include <tdeversion.h>
#if ! ( KDE_IS_VERSION( 3, 3, 90 ) )
#error KDE 3.3
#endif
],
        need_kde33_compat="no"
,
        need_kde33_compat="yes"
)

AC_COMPILE_IFELSE([
#include <tdeversion.h>
#if ! ( KDE_IS_VERSION( 3, 4, 90 ) )
#error KDE 3.4
#endif
],
        need_kde34_compat="no"
,
        need_kde34_compat="yes"
)

AC_COMPILE_IFELSE([
#include <tdeversion.h>
#if ! ( KDE_IS_VERSION( 3, 5, 2 ) )
#error KDE 3.5.x (x < 2)
#endif
],
        need_kde351_compat="no"
,
        need_kde351_compat="yes"
)
CXXFLAGS="$tdeversion_save_CXXFLAGS"
LIBS="$tdeversion_safe_LIBS"
AC_LANG_RESTORE

if test "$need_kde32_compat" = "yes"; then
        AC_MSG_ERROR([You have KDE 3.2.x. TorK needs KDE 3.5.1 or greater.])
else
        if test "$need_kde33_compat" = "yes"; then
                AC_MSG_ERROR([You have KDE 3.3.x TorK needs KDE 3.5.1 or greater.])
        else 
                if test "$need_kde34_compat" = "yes"; then
                        AC_MSG_ERROR([You have KDE 3.4.x TorK needs KDE 3.5.1 or greater.])
                else
                        if test "$need_kde351_compat" = "yes"; then
                                 AC_MSG_RESULT([KDE 3.5.x (x >=1) or SVN trunk])
                        else
                                AC_MSG_RESULT([KDE 3.5.x (x >=2) or SVN trunk])
                        fi
                fi
        fi
fi


# --- End KDE 3.2 check ---

# --- Begin libgnutls macro ---

dnl Autoconf macros for libgnutls
dnl $id$

# Modified for LIBGNUTLS -- nmav
# Configure paths for LIBGCRYPT
# Shamelessly stolen from the one of XDELTA by Owen Taylor
# Werner Koch   99-12-09

dnl AM_PATH_LIBGNUTLS([MINIMUM-VERSION, [ACTION-IF-FOUND [, ACTION-IF-NOT-FOUND ]]])
dnl Test for libgnutls, and define LIBGNUTLS_CFLAGS and LIBGNUTLS_LIBS
dnl
AC_DEFUN([AM_PATH_LIBGNUTLS],
[dnl
dnl Get the cflags and libraries from the libgnutls-config script
dnl
AC_ARG_WITH(libgnutls-prefix,
          [  --with-libgnutls-prefix=PFX   Prefix where libgnutls is installed (optional)],
          libgnutls_config_prefix="$withval", libgnutls_config_prefix="")

  if test x$libgnutls_config_prefix != x ; then
     if test x${LIBGNUTLS_CONFIG+set} != xset ; then
        LIBGNUTLS_CONFIG=$libgnutls_config_prefix/bin/libgnutls-config
     fi
  fi

  AC_PATH_PROG(LIBGNUTLS_CONFIG, libgnutls-config, no)
  min_libgnutls_version=ifelse([$1], ,0.1.0,$1)
  AC_MSG_CHECKING(for libgnutls - version >= $min_libgnutls_version)
  no_libgnutls=""
  if test "$LIBGNUTLS_CONFIG" = "no" ; then
    no_libgnutls=yes
  else
    LIBGNUTLS_CFLAGS=`$LIBGNUTLS_CONFIG $libgnutls_config_args --cflags`
    LIBGNUTLS_LIBS=`$LIBGNUTLS_CONFIG $libgnutls_config_args --libs`
    libgnutls_config_version=`$LIBGNUTLS_CONFIG $libgnutls_config_args --version`


      ac_save_CFLAGS="$CFLAGS"
      ac_save_LIBS="$LIBS"
      CFLAGS="$CFLAGS $LIBGNUTLS_CFLAGS"
      LIBS="$LIBS $LIBGNUTLS_LIBS"
dnl
dnl Now check if the installed libgnutls is sufficiently new. Also sanity
dnl checks the results of libgnutls-config to some extent
dnl
      rm -f conf.libgnutlstest
      AC_TRY_RUN([
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <gnutls/gnutls.h>

int
main ()
{
    system ("touch conf.libgnutlstest");

    if( strcmp( gnutls_check_version(NULL), "$libgnutls_config_version" ) )
    {
      printf("\n*** 'libgnutls-config --version' returned %s, but LIBGNUTLS (%s)\n",
             "$libgnutls_config_version", gnutls_check_version(NULL) );
      printf("*** was found! If libgnutls-config was correct, then it is best\n");
      printf("*** to remove the old version of LIBGNUTLS. You may also be able to fix the error\n");
      printf("*** by modifying your LD_LIBRARY_PATH enviroment variable, or by editing\n");
      printf("*** /etc/ld.so.conf. Make sure you have run ldconfig if that is\n");
      printf("*** required on your system.\n");
      printf("*** If libgnutls-config was wrong, set the environment variable LIBGNUTLS_CONFIG\n");
      printf("*** to point to the correct copy of libgnutls-config, and remove the file config.cache\n");
      printf("*** before re-running configure\n");
    }
    else if ( strcmp(gnutls_check_version(NULL), LIBGNUTLS_VERSION ) )
    {
      printf("\n*** LIBGNUTLS header file (version %s) does not match\n", LIBGNUTLS_VERSION);
      printf("*** library (version %s)\n", gnutls_check_version(NULL) );
    }
    else
    {
      if ( gnutls_check_version( "$min_libgnutls_version" ) )
      {
        return 0;
      }
     else
      {
        printf("no\n*** An old version of LIBGNUTLS (%s) was found.\n",
                gnutls_check_version(NULL) );
        printf("*** You need a version of LIBGNUTLS newer than %s. The latest version of\n",
               "$min_libgnutls_version" );
        printf("*** LIBGNUTLS is always available from ftp://gnutls.hellug.gr/pub/gnutls.\n");
        printf("*** \n");
        printf("*** If you have already installed a sufficiently new version, this error\n");
        printf("*** probably means that the wrong copy of the libgnutls-config shell script is\n");
        printf("*** being found. The easiest way to fix this is to remove the old version\n");
        printf("*** of LIBGNUTLS, but you can also set the LIBGNUTLS_CONFIG environment to point to the\n");
        printf("*** correct copy of libgnutls-config. (In this case, you will have to\n");
        printf("*** modify your LD_LIBRARY_PATH enviroment variable, or edit /etc/ld.so.conf\n");
        printf("*** so that the correct libraries are found at run-time))\n");
      }
    }
  return 1;
}
],, no_libgnutls=yes,[echo $ac_n "cross compiling; assumed OK... $ac_c"])
       CFLAGS="$ac_save_CFLAGS"
       LIBS="$ac_save_LIBS"
  fi

  if test "x$no_libgnutls" = x ; then
     AC_MSG_RESULT(yes)
     ifelse([$2], , :, [$2])
  else
     if test -f conf.libgnutlstest ; then
        :
     else
        AC_MSG_RESULT(no)
     fi
     if test "$LIBGNUTLS_CONFIG" = "no" ; then
       echo "*** The libgnutls-config script installed by LIBGNUTLS could not be found"
       echo "*** If LIBGNUTLS was installed in PREFIX, make sure PREFIX/bin is in"
       echo "*** your path, or set the LIBGNUTLS_CONFIG environment variable to the"
       echo "*** full path to libgnutls-config."
     else
       if test -f conf.libgnutlstest ; then
        :
       else
          echo "*** Could not run libgnutls test program, checking why..."
          CFLAGS="$CFLAGS $LIBGNUTLS_CFLAGS"
          LIBS="$LIBS $LIBGNUTLS_LIBS"
          AC_TRY_LINK([
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <gnutls/gnutls.h>
],      [ return !!gnutls_check_version(NULL); ],
        [ echo "*** The test program compiled, but did not run. This usually means"
          echo "*** that the run-time linker is not finding LIBGNUTLS or finding the wrong"
          echo "*** version of LIBGNUTLS. If it is not finding LIBGNUTLS, you'll need to set your"
          echo "*** LD_LIBRARY_PATH environment variable, or edit /etc/ld.so.conf to point"
          echo "*** to the installed location  Also, make sure you have run ldconfig if that"
          echo "*** is required on your system"
          echo "***"
          echo "*** If you have an old version installed, it is best to remove it, although"
          echo "*** you may also be able to get things to work by modifying LD_LIBRARY_PATH"
          echo "***" ],
        [ echo "*** The test program failed to compile or link. See the file config.log for the"
          echo "*** exact error that occured. This usually means LIBGNUTLS was incorrectly installed"
          echo "*** or that you have moved LIBGNUTLS since it was installed. In the latter case, you"
          echo "*** may want to edit the libgnutls-config script: $LIBGNUTLS_CONFIG" ])
          CFLAGS="$ac_save_CFLAGS"
          LIBS="$ac_save_LIBS"
       fi
     fi
     LIBGNUTLS_CFLAGS=""
     LIBGNUTLS_LIBS=""
     ifelse([$3], , :, [$3])
  fi
  rm -f conf.libgnutlstest
  AC_SUBST(LIBGNUTLS_CFLAGS)
  AC_SUBST(LIBGNUTLS_LIBS)
])

# --- End libgnutls macro ---


dnl Compile with GNUTLS
AC_ARG_ENABLE(gnutls,
     AS_HELP_STRING(--enable-gnutls, Compile with GnuTLS))

if test x$enable_gnutls = xyes; then

# gnu tls
#AM_PATH_LIBGNUTLS(1.0.0, , [
PKG_CHECK_MODULES([LIBGNUTLS], [gnutls >= 1.0.0], , [
       AC_DEFINE(USE_OPENSSL, 1, [use openssl for ssl session decryption instead of gnutls])
        AC_SUBST(USE_OPENSSL)
])

fi

# --- If no gnutls check for openssl ---

if test "x$LIBGNUTLS_LIBS" = "x"; then

AC_DEFINE(USE_OPENSSL, 1, [use openssl for ssl session decryption instead of gnutls])
AC_SUBST(USE_OPENSSL)

fi

# --- End of If no gnutls check for openssl ---

# Even if gnutls is required, we still need openssl for shallot.

dnl ------------------------------------------------------
dnl Where do you live, openssl?  And how do we call you?

AC_CACHE_CHECK([for OpenSSL directory], ac_cv_openssl_dir, [
  saved_LIBS="$LIBS"
  saved_LDFLAGS="$LDFLAGS"
  saved_CPPFLAGS="$CPPFLAGS"
  ssl_found=no
  for ssldir in $tryssldir "" $prefix /usr/local/openssl /usr/lib/openssl /usr/local/ssl /usr/lib/ssl /usr/local /usr/athena /usr/pkg /opt /opt/openssl ; do
    LDFLAGS="$saved_LDFLAGS"
    SSL_LIBS="-lssl -lcrypto"
    LIBS="$saved_LIBS $SSL_LIBS"

    # Skip the directory if it isn't there.
    if test ! -z "$ssldir" -a ! -d "$ssldir" ; then
       continue;
    fi
    if test ! -z "$ssldir" ; then
      if test -d "$ssldir/lib" ; then
        LDFLAGS="-L$ssldir/lib $LDFLAGS"
      else
        LDFLAGS="-L$ssldir $LDFLAGS"
      fi
      if test -d "$ssldir/include" ; then
        CPPFLAGS="-I$ssldir/include $CPPFLAGS"
      else
        CPPFLAGS="-I$ssldir $CPPFLAGS"
      fi
    fi
    # Can I link it?
    AC_TRY_LINK([#include <openssl/rand.h>],
       [ RAND_add((void*)0,0,0); ],
       [ openssl_linked=yes ], [ openssl_linked=no ])
    if test $openssl_linked = yes; then
       if test ! -z "$ssldir" ; then
         ac_cv_openssl_dir=$ssldir
       else
         ac_cv_openssl_dir="(system)"
       fi
       ssl_found=yes
       break
    fi
  done
  LIBS="$saved_LIBS"
  LDFLAGS="$saved_LDFLAGS"
  CPPFLAGS="$saved_CPPFLAGS"
  if test $ssl_found = no ; then
    AC_MSG_ERROR([Could not find a linkable OpenSSL. You can specify an explicit path using --with-ssl-dir])
  fi
])

saved_LIBS="$LIBS"
SSL_LIBS="-lssl -lcrypto"
LIBS="$saved_LIBS $SSL_LIBS"

if test "$ac_cv_openssl_dir" != "(system)"; then
  if test -d "$ac_cv_openssl_dir/lib" ; then
    LDFLAGS="-L$ac_cv_openssl_dir/lib $LDFLAGS"
    ssl_libdir="$ac_cv_openssl_dir/lib"
  else
    LDFLAGS="-L$ac_cv_openssl_dir $LDFLAGS"
    ssl_libdir="$ac_cv_openssl_dir"
  fi
  if test -d "$ac_cv_openssl_dir/include" ; then
    CPPFLAGS="-I$ac_cv_openssl_dir/include $CPPFLAGS"
  else
    CPPFLAGS="-I$ac_cv_openssl_dir $CPPFLAGS"
  fi
fi

AC_CACHE_CHECK([whether we need extra options to link OpenSSL],
               ac_cv_openssl_linker_option, [
    saved_LDFLAGS="$LDFLAGS"
    ssl_runs=no
    linked_with=nothing
    for ssl_extra in "" "-Wl,-R$ssl_libdir" "-R$ssl_libdir" ; do
      LDFLAGS="$ssl_extra $saved_LDFLAGS"
      AC_TRY_RUN([
#include <string.h>
#include <openssl/rand.h>
int main(void)
{
        char a[2048];
        memset(a, 0, sizeof(a));
        RAND_add(a, sizeof(a), sizeof(a));
        return(RAND_status() <= 0);
}
                        ],
                openssl_runs=yes, openssl_runs=no)
      if test $openssl_runs = yes ; then
        if test "$linked_with" = nothing; then
           linked_with="$ssl_extra"
        fi
        AC_TRY_RUN([
#include <openssl/opensslv.h>
#include <openssl/crypto.h>
int main(void) {
return (OPENSSL_VERSION_NUMBER == SSLeay()) == 0;
}],
        right_version=yes, right_version=no)
        if test "$right_version" = yes; then
          if test -z "$ssl_extra" ; then
            ac_cv_openssl_linker_option='(none)'
          else
            ac_cv_openssl_linker_option=$ssl_extra
          fi
          ssl_runs=yes
          break
        fi
     fi
    done
    if test $ssl_runs = no ; then
      if test "$linked_with" = 'nothing' ; then
        AC_MSG_ERROR([Found linkable OpenSSL in $ac_cv_openssl_dir, but it doesn't run, even with -R.  Maybe specify another using --with-ssl-dir?])
      else
        if test -z "$linked_with" ; then
            ac_cv_openssl_linker_option='(none)'
        else
            ac_cv_openssl_linker_option=$linked_with
        fi
        AC_MSG_WARN([I managed to make OpenSSL link and run, but I couldn't make it link against the same version I found header files for.])
      fi
    fi
    LDFLAGS="$saved_LDFLAGS"
])

if test "$ac_cv_openssl_linker_option" != '(none)' ; then
   LDFLAGS="$ac_cv_openssl_linker_option $LDFLAGS"
fi

LIBS="$saved_LIBS"

# --- Decide which library to use for linking Tork ---
# --- SSL will be used for shallot ---

if test "x$LIBGNUTLS_LIBS" = "x"; then
    CRYPTO_LIBS=$SSL_LIBS
else
    CRYPTO_LIBS=$LIBGNUTLS_LIBS
fi
AC_SUBST(CRYPTO_LIBS)
AC_SUBST(SSL_LIBS)



dnl - GeoIP

AC_ARG_WITH(external-geoip,
	AC_HELP_STRING([--with-external-geoip], [Build against an external copy of GeoIP]))

if test "x$with_external_geoip" != "xyes"; then
   GEOVERSION=1.4.2
   dnl PACKAGE=GeoIP
   GEOIP_VERSION_INFO=`echo $GEOVERSION | awk -F. '{ printf "%d:%d:%d", $1+$2, $3, $2 }'`
   AC_SUBST(GEOIP_VERSION_INFO)

   AC_ARG_WITH(city,
   [  --with-city=\"no\"   Enable/disable support for GeoIP City Edition. (yes)])
   if test "$with_city" = "no" ; then
     echo Disabling GeoIP City Edition support
     WITH_CITY=1
   else
     echo Enabling GeoIP City Edition support
     WITH_CITY=0
   fi
   AC_SUBST(WITH_CITY)
else
   with_city=no

   AC_CHECK_HEADERS([GeoIP.h],,
     [AC_MSG_ERROR([External GeoIP requested but GeoIP not found.])])
   AC_CHECK_LIB([GeoIP], [GeoIP_new], [GEOIPLIB=-lGeoIP],
     [AC_MSG_ERROR([External GeoIP requested but GeoIP not found.])])
fi

AM_CONDITIONAL(WITH_CITY, test "$with_city" != "no")
AM_CONDITIONAL([EXTERNAL_GEOIP], [test "x$with_external_geoip" = "xyes"])

if test x$with_external_geoip = xyes; then
  AC_DEFINE(EXTERNAL_GEOIP, 1, "Using External GeoIP")
fi



dnl - libwhich

which_cv_iberty=no
AC_ARG_ENABLE(iberty,
  [  --disable-iberty        do not try to find and use the iberty library],
  which_cv_iberty=$enableval)

dnl Checks for programs.
AC_PROG_CC
AC_PROG_INSTALL
AC_LANG_C


AC_DEFUN([BASH_CHECK_GETPW_FUNCS],
[AC_MSG_CHECKING(whether programs are able to redeclare getpw functions)
AC_CACHE_VAL(bash_cv_can_redecl_getpw,
[AC_TRY_COMPILE([#include <sys/types.h>
#include <pwd.h>
extern struct passwd *getpwent();
extern struct passwd *getpwuid();
extern struct passwd *getpwnam();],
[struct passwd *z; z = getpwent(); z = getpwuid(0); z = getpwnam("root");],
  bash_cv_can_redecl_getpw=yes,bash_cv_can_redecl_getpw=no)])
AC_MSG_RESULT($bash_cv_can_redecl_getpw)
if test $bash_cv_can_redecl_getpw = no; then
AC_DEFINE([HAVE_GETPW_DECLS], 1, [Define this when you are able to redeclare getpw functions.])
fi
])


dnl Checks for header files.
AC_HEADER_STDC
AC_CHECK_FUNCS(strchr memcpy getopt_long)
AC_CHECK_HEADERS(unistd.h string.h libiberty.h)
AC_HEADER_STAT

AC_HEADER_TIME
AC_CHECK_HEADERS(sys/time.h)

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

dnl Checks for library functions.
AC_TYPE_GETGROUPS
AC_CHECK_FUNCS(getcwd getwd)

AC_DEFINE([NEED_XMALLOC], 1, [Define if you need xmalloc().])

dnl Additional stuff needed for the tilde directory
AC_CHECK_PROG(AR, ar, ar, ar)
AC_PROG_RANLIB
AC_CHECK_HEADERS(stdlib.h)
BASH_CHECK_GETPW_FUNCS

if test "x$CONFDIR" = "x"; then
  CONFDIR=`eval echo $sysconfdir`
fi
AC_SUBST(CONFDIR)
AH_TEMPLATE([CONFDIR],[tork's configuration directory])
AC_DEFINE_UNQUOTED(CONFDIR,"$CONFDIR")


dnl Disable stuff that's not suitable for a Live CD
AC_ARG_ENABLE(live-cd,
     AS_HELP_STRING(--enable-live-cd, Disable Features Unsuitable for LiveCD))

if test x$enable_live_cd = xyes; then
  AC_DEFINE(LIVECD, 1, "Define to compile for a LiveCD")
fi

dnl Detect OS for Shallot compilation

case "$host" in 
  *-*-linux*) AC_DEFINE(LINUX_PORT, 1, "Shallot will compile for Linux") ;;
  *-*-freebsd*) AC_DEFINE(BSD, 1, "Shallot will compile for BSD") ;;
  *-*-netbsd*) AC_DEFINE(BSD, 1, "Shallot will compile for BSD") ;;
  *-*-openbsd*) AC_DEFINE(BSD, 1, "Shallot will compile for BSD") ;;
  *) AC_DEFINE(GENERIC, 1, "Shallot will compile for GENERIC") ;;
esac
#AC_DEFINE(_GNU_SOURCE, 1, "Shallot will compile with _GNU_SOURCE")
# USE_GNU_SOURCE above is used instead and _GNU_SOURCE defined in shallot.

dnl Check for presence of torsocks
AC_CHECK_PROG(TORSOCKS, torsocks, torsocks)
if test "${TORSOCKS}" = ""; then
  AC_MSG_ERROR('TorK requires torsocks: http://code.google.com/p/torsocks')
fi

dnl detect netfilter
AC_CHECK_HEADERS(sys/types.h sys/socket.h)
AC_CHECK_HEADERS(linux/netfilter_ipv4.h,
        linux_netfilter_ipv4=1, linux_netfilter_ipv4=0)


failsafe_ok=0
if test x$linux_netfilter_ipv4 = x1 ; then
  failsafe_ok=1
fi
if test x$failsafe_ok = x1 ; then
  AC_DEFINE(USE_FAILSAFE, 1, "Define to enable failsafe support")
fi

AC_CONFIG_FILES([src/menu/tork])
