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

AC_INIT(wwwoffle,2.9)


dnl Specify the autoconf file.

AC_CONFIG_HEADER(src/autoconfig.h)


dnl Checks for programs.

AC_PROG_CC

AC_PROG_LEX
if test "x$LEX" == "x:"; then
    AC_MSG_ERROR([No lex/flex program could be found])
fi

AC_PROG_INSTALL
AC_PROG_AWK
AC_PROG_MAKE_SET

AC_CHECK_PROGS(TAR,gnutar gtar tar,tar)
AC_CHECK_PROG(PERL,perl,perl)


dnl Checks for header files.

AC_HEADER_STDC
AC_HEADER_DIRENT
AC_HEADER_TIME
AC_CHECK_HEADERS(sys/time.h sys/param.h sys/statvfs.h sys/vfs.h)
AC_CHECK_HEADERS(sys/mount.h,,,
[
#if SYS_PARAM_H
#include <sys/param.h>
#endif
])


dnl Checks for typedefs, structures, and compiler characteristics.

AC_STRUCT_TIMEZONE
AC_STRUCT_ST_BLOCKS
AC_STRUCT_ST_BLKSIZE


dnl Checks for libraries.

ETR_SOCKET_NSL

LIBS="$LIBS $ETR_SOCKET_LIBS"


dnl Checks for library functions.

AC_CHECK_FUNCS(getaddrinfo getnameinfo)

AC_CHECK_FUNCS(statfs statvfs)

if test "$ac_cv_func_statfs" = "no"; then
    if test "$ac_cv_func_statvfs" = "no"; then
        AC_MSG_ERROR([Neither of the two functions statfs or statvfs are available])
    fi
fi

AC_CHECK_FUNCS(setresuid setresgid getresuid getresgid setreuid setregid setgroups)


dnl Checks for zlib libraries.

AC_ARG_WITH(zlib,
[  --with-zlib=dir              Set the location of the zlib header and library],
WITH_ZLIB=$withval,WITH_ZLIB=yes)

if test "$WITH_ZLIB" = "no"; then
   ZLIB_INC=
   ZLIB_LIB=
   USE_ZLIB=0
else
   if test "$WITH_ZLIB" = "yes"; then
      ZLIB_LIB=-lz
      ZLIB_INC=
      USE_ZLIB=1
   else
      ZLIB_LIB="-L$WITH_ZLIB/lib -lz"
      ZLIB_INC="-I$WITH_ZLIB/include"
      USE_ZLIB=1
   fi

   AC_CHECK_HEADERS(zlib.h,
                    HAVE_ZLIB_H=yes,HAVE_ZLIB_H=no)

   if test "$HAVE_ZLIB_H" = "no"; then
      AC_MSG_WARN([Cannot find zlib.h header file; configuring without compression support])
      ZLIB_LIB=
      ZLIB_INC=
      USE_ZLIB=0
   else
      AC_CHECK_LIB(z, zlibVersion,
                   WITH_ZLIB=yes,WITH_ZLIB=no,$ZLIB_LIB)

      if test "$WITH_ZLIB" = "no"; then
         AC_MSG_WARN([Cannot find working libz library; configuring without compression support])
         ZLIB_LIB=
         ZLIB_INC=
         USE_ZLIB=0
      fi
   fi
fi

AC_DEFINE_UNQUOTED(USE_ZLIB,$USE_ZLIB)

AC_SUBST(ZLIB_LIB)
AC_SUBST(ZLIB_INC)


dnl Checks for using gnutls.

AC_ARG_WITH(gnutls,
[  --with-gnutls=dir            Set the location of the gnutls header and library],
WITH_GNUTLS=$withval,WITH_GNUTLS=no)

if test "$WITH_GNUTLS" = "no"; then
   GNUTLS_INC=
   GNUTLS_LIB=
   USE_GNUTLS=0
else
   if test "$WITH_GNUTLS" = "yes"; then
      GNUTLS_INC=
      GNUTLS_LIB="-lgnutls"
   else
      GNUTLS_INC="-I$WITH_GNUTLS/include"
      GNUTLS_LIB="-L$WITH_GNUTLS/lib -lgnutls"
   fi

   AC_CHECK_HEADERS(gnutls/gnutls.h,
                    HAVE_GNUTLS_GNUTLS_H=yes,HAVE_GNUTLS_GNUTLS_H=no)

   if test "$HAVE_GNUTLS_GNUTLS_H" = "no"; then
      AC_MSG_WARN([Cannot find gnutls/gnutls.h header file; configuring without SSL/https support])
      GNUTLS_LIB=
      GNUTLS_INC=
      USE_GNUTLS=0
   else
      AC_CHECK_LIB(gnutls, gnutls_check_version,
                   WITH_GNUTLS=yes,WITH_GNUTLS=no,$GNUTLS_LIB)

      if test "$WITH_GNUTLS" = "no"; then
         AC_MSG_WARN([Cannot find working libgnutls library, configuring without SSL/https support])
         GNUTLS_LIB=
         GNUTLS_INC=
         USE_GNUTLS=0
      else
         USE_GNUTLS=1
      fi
   fi
fi

AC_DEFINE_UNQUOTED(USE_GNUTLS,$USE_GNUTLS)

AC_SUBST(GNUTLS_LIB)
AC_SUBST(GNUTLS_INC)


dnl Checks for using gcrypt.

AC_ARG_WITH(gcrypt,
[  --with-gcrypt=dir            Set the location of the gcrypt header and library],
WITH_GCRYPT=$withval,WITH_GCRYPT=$WITH_GNUTLS)

if test "$WITH_GCRYPT" = "no"; then
   GCRYPT_INC=
   GCRYPT_LIB=
   USE_GCRYPT=0
else
   if test "$WITH_GCRYPT" = "yes"; then
      GCRYPT_INC=
      GCRYPT_LIB="-lgcrypt"
   else
      GCRYPT_INC="-I$WITH_GCRYPT/include"
      GCRYPT_LIB="-L$WITH_GCRYPT/lib -lgcrypt"
   fi

   AC_CHECK_HEADERS(gcrypt.h,
                    HAVE_GCRYPT_H=yes,HAVE_GCRYPT_H=no)

   if test "$HAVE_GCRYPT_H" = "no"; then
      AC_MSG_WARN([Cannot find gcrypt.h header file; configuring without some SSL/https features])
      GCRYPT_LIB=
      GCRYPT_INC=
      USE_GCRYPT=0
   else
      AC_CHECK_LIB(gcrypt, gcry_check_version,
                   WITH_GCRYPT=yes,WITH_GCRYPT=no,$GCRYPT_LIB)

      if test "$WITH_GCRYPT" = "no"; then
         AC_MSG_WARN([Cannot find working libgcrypt library, configuring without some SSL/https features])
         GCRYPT_LIB=
         GCRYPT_INC=
         USE_GCRYPT=0
      else
         USE_GCRYPT=1
      fi
   fi
fi

AC_DEFINE_UNQUOTED(USE_GCRYPT,$USE_GCRYPT)

AC_SUBST(GCRYPT_LIB)
AC_SUBST(GCRYPT_INC)


dnl Checks for using IPv6.

AC_ARG_WITH(ipv6,
[  --with-ipv6                  Enable the use of IPv6 sockets],
WITH_IPV6=$withval,WITH_IPV6=yes)

if test "$WITH_IPV6" = "no"; then
   USE_IPV6=0
else
   USE_IPV6=1

   AC_MSG_CHECKING(for IPv6 support)

   if test "$ac_cv_func_getnameinfo" = "no"; then
      AC_MSG_RESULT([no, cannot find getnameinfo() function])
      USE_IPV6=0
   elif test "$ac_cv_func_getaddrinfo" = "no"; then
      AC_MSG_RESULT([no, cannot find getaddrinfo() function])
      USE_IPV6=0
   else
      AC_MSG_RESULT(yes)
   fi
fi

AC_DEFINE_UNQUOTED(USE_IPV6,$USE_IPV6)


dnl Check for Cygwin.

AC_CYGWIN

if test "$CYGWIN" = "yes"; then
    install_win32=install-win32
else
    install_win32=
fi

AC_SUBST(install_win32)


dnl The WWWOFFLE specific settings that we need.

AC_ARG_WITH(spooldir,
[  --with-spooldir=directory    Set the default spool directory],
SPOOLDIR=$withval,)

if test "x$SPOOLDIR" = "x"; then
    SPOOLDIR=/var/spool/wwwoffle
fi

AC_DEFINE_UNQUOTED(DEF_SPOOLDIR,"$SPOOLDIR")
AC_SUBST(SPOOLDIR)


AC_ARG_WITH(confdir,
[  --with-confdir=directory     Set the default config file directory],
CONFDIR=$withval,)

if test "x$CONFDIR" = "x"; then
    CONFDIR=/etc/wwwoffle
fi

AC_DEFINE_UNQUOTED(DEF_CONFDIR,"$CONFDIR")
AC_SUBST(CONFDIR)


AC_ARG_WITH(server,
[  --with-server=host:port:port:port Set the default host, HTTP port, WWWOFFLE port and HTTPS port],
SERVER=$withval,)

if test "x$SERVER" = "x"; then
    SERVER=localhost:8080:8081:8443
fi

LOCALHOST=`echo $SERVER | awk -F: '{print $1}'`

if test "x$LOCALHOST" = "x"; then
    LOCALHOST=localhost
fi

HTTP_PORT=`echo $SERVER | awk -F: '{print $2}'`

if test "x$HTTP_PORT" = "x"; then
    HTTP_PORT=8080
fi

WWWOFFLE_PORT=`echo $SERVER | awk -F: '{print $3}'`

if test "x$WWWOFFLE_PORT" = "x"; then
    WWWOFFLE_PORT=8081
fi

HTTPS_PORT=`echo $SERVER | awk -F: '{print $4}'`

if test "x$HTTPS_PORT" = "x"; then
    HTTPS_PORT=8443
fi

AC_SUBST(LOCALHOST)
AC_SUBST(HTTP_PORT)
AC_SUBST(HTTPS_PORT)
AC_SUBST(WWWOFFLE_PORT)


dnl Language settings.

AC_MSG_CHECKING(the default language)

LANGUAGES="de en es fr it nl pl ru"

AC_SUBST(LANGUAGES)

AC_ARG_WITH(default-language,
[  --with-default-language=lang Set the default language to install],
WITH_LANG=$withval,WITH_LANG=en)

DEFLANG=
for lang in $LANGUAGES; do
    if test "$WITH_LANG" = $lang; then
        DEFLANG=$lang
    fi
done

if test "x$DEFLANG" = "x"; then
    AC_MSG_ERROR([Invalid value $WITH_LANG for default language, choose from: $LANGUAGES.])
else
    AC_MSG_RESULT($DEFLANG)
fi

AC_SUBST(DEFLANG)


dnl Checks for installation location.


dnl Create the output files.

AC_OUTPUT(
          Makefile
          cache/Makefile
          conf/Makefile
          doc/Makefile
          src/Makefile
          src/testprogs/Makefile
          src/autoconfig.h.in
         )
