dnl Template file for GNU Autoconf
dnl Copyright (C) 1995, 1996, 1997, 2001 Free Software Foundation, Inc.

dnl This program is free software; you can redistribute it and/or modify
dnl it under the terms of the GNU General Public License as published by
dnl the Free Software Foundation; either version 2 of the License, or
dnl (at your option) any later version.

dnl This program is distributed in the hope that it will be useful,
dnl but WITHOUT ANY WARRANTY; without even the implied warranty of
dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
dnl GNU General Public License for more details.

dnl You should have received a copy of the GNU General Public License
dnl along with this program; if not, write to the Free Software
dnl Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

dnl In addition, as a special exception, the Free Software Foundation
dnl gives permission to link the code of its release of Wget with the
dnl OpenSSL project's "OpenSSL" library (or with modified versions of it
dnl that use the same license as the "OpenSSL" library), and distribute
dnl the linked executables.  You must obey the GNU General Public License
dnl in all respects for all of the code used other than "OpenSSL".  If you
dnl modify this file, you may extend this exception to your version of the
dnl file, but you are not obligated to do so.  If you do not wish to do
dnl so, delete this exception statement from your version.

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

AC_INIT([src/version.c])
AC_PREREQ(2.57)

dnl
dnl What version of Wget are we building?
dnl
VERSION=`sed -e 's/^.*"\(.*\)";$/\1/' ${srcdir}/src/version.c`
AC_MSG_NOTICE([configuring for GNU Wget $VERSION])
AC_SUBST(VERSION)
PACKAGE=wget
AC_SUBST(PACKAGE)

dnl
dnl Get cannonical host
dnl
AC_CANONICAL_HOST
AC_DEFINE_UNQUOTED([OS_TYPE], "$host_os",
                   [Define to be the name of the operating system.])

dnl
dnl Process features.
dnl

AC_ARG_WITH(ssl,
[[  --with-ssl[=SSL-ROOT]   link with SSL support [default=auto]
  --without-ssl           disable SSL autodetection]])

AC_ARG_ENABLE(opie,
[  --disable-opie          disable support for opie or s/key FTP login],
USE_OPIE=$enableval, USE_OPIE=yes)
test x"${USE_OPIE}" = xyes && AC_DEFINE([USE_OPIE], 1,
   [Define if you want the Opie support for FTP compiled in.])

AC_ARG_ENABLE(digest,
[  --disable-digest        disable support for HTTP digest authorization],
USE_DIGEST=$enableval, USE_DIGEST=yes)
test x"${USE_DIGEST}" = xyes && AC_DEFINE([USE_DIGEST], 1,
   [Define if you want the HTTP Digest Authorization compiled in.])

AC_ARG_ENABLE(debug,
[  --disable-debug         disable support for debugging output],
ENABLE_DEBUG=$enableval, ENABLE_DEBUG=yes)
test x"${ENABLE_DEBUG}" = xyes && AC_DEFINE([ENABLE_DEBUG], 1,
   [Define if you want the debug output support compiled in.])

wget_need_md5=no

case "${USE_OPIE}${USE_DIGEST}" in
*yes*)
	wget_need_md5=yes
esac
if test x"$USE_OPIE" = xyes; then
  OPIE_OBJ='ftp-opie$o'
fi
AC_SUBST(OPIE_OBJ)

dnl
dnl Whether make sets $(MAKE)...
dnl
AC_PROG_MAKE_SET

dnl
dnl Find a good install
dnl
AC_PROG_INSTALL

dnl
dnl Find the compiler
dnl

dnl We want these before the checks, so the checks can modify their values.
test -z "$CFLAGS"  && CFLAGS= auto_cflags=1
test -z "$CC" && cc_specified=yes

AC_PROG_CC
AC_AIX
AM_PROG_CC_STDC

dnl Turn on optimization by default.  Specifically:
dnl
dnl if the user hasn't specified CFLAGS, then
dnl   if compiler is gcc, then
dnl     use -O2 and some warning flags
dnl   else
dnl     use os-specific flags or -O
if test -n "$auto_cflags"; then
  if test -n "$GCC"; then
    CFLAGS="$CFLAGS -O2 -Wall -Wno-implicit"
  else
    case "$host_os" in
      *hpux*)  CFLAGS="$CFLAGS +O3"                      ;;
      *ultrix* | *osf*) CFLAGS="$CFLAGS -O -Olimit 2000" ;;
      *)       CFLAGS="$CFLAGS -O" ;;
    esac
  fi
fi

dnl
dnl Configure our included libtool and make sure it's regenerated when needed
dnl
AC_PROG_LIBTOOL
AC_SUBST(LIBTOOL_DEPS)

dnl
dnl In case of {cyg,gnu}win32.  Should be a _target_ test.
dnl Might also be erelevant for DJGPP.
dnl
case "$host_os" in
  *win32) exeext='.exe';;
  *) exeext='';;
esac
AC_SUBST(exeext)

dnl
dnl Checks for basic compiler characteristics.
dnl
AC_C_CONST
AC_C_INLINE
AC_C_VOLATILE
AC_C_BIGENDIAN
AC_C_PROTOTYPES

if test x"$am_cv_prog_cc_stdc" != xno; then :
  AC_SUBST(U, [])
  AC_SUBST(ANSI2KNR, [])
else
  AC_SUBST(U, [_])
  AC_SUBST(ANSI2KNR, [./ansi2knr])
fi

dnl
dnl Checks for header files that might be missing.
dnl

dnl Check for basic headers, even those we assume the presence of.
dnl This is because Autoconf default includes check for STDC_HEADERS,
dnl HAVE_SYS_TYPES_H, etc. before including them.
AC_HEADER_STDC
AC_CHECK_HEADERS(sys/types.h sys/stat.h)
dnl Now check for the others.
AC_CHECK_HEADERS(string.h strings.h stdarg.h limits.h unistd.h sys/time.h)
AC_CHECK_HEADERS(termios.h sys/ioctl.h sys/select.h utime.h sys/utime.h)
AC_CHECK_HEADERS(stdint.h inttypes.h signal.h setjmp.h pwd.h)
AC_HEADER_TIME

dnl
dnl Check integral type sizes.
dnl
AC_CHECK_SIZEOF(short)
AC_CHECK_SIZEOF(int)
AC_CHECK_SIZEOF(long)
AC_CHECK_SIZEOF(long long)

dnl
dnl Checks for non-universal or system-specific types.
dnl
AC_TYPE_SIZE_T
AC_TYPE_PID_T
AC_CHECK_TYPES(uint32_t)
AC_TYPE_SIGNAL
AC_CHECK_TYPES(sig_atomic_t, [], [], [
#include <stdio.h>
#include <sys/types.h>
#if HAVE_INTTYPES_H
# include <inttypes.h>
#endif
#ifdef HAVE_SIGNAL_H
# include <signal.h>
#endif
])

dnl
dnl Checks for library functions.
dnl
AC_FUNC_ALLOCA
AC_FUNC_MMAP
AC_CHECK_FUNCS(strdup strstr strcasecmp strncasecmp strpbrk memmove)
AC_CHECK_FUNCS(gettimeofday mktime strptime strerror snprintf vsnprintf)
AC_CHECK_FUNCS(usleep select sigblock sigsetjmp signal symlink access isatty)

dnl
dnl Call Wget's local macros defined in aclocal.
dnl
WGET_STRUCT_UTIMBUF
WGET_SOCKLEN_T
WGET_FNMATCH
WGET_NANOSLEEP
WGET_NSL_SOCKET

dnl
dnl Check if we need to compile in getopt.c.
dnl
AC_CHECK_FUNC(getopt_long, [], [
  GETOPT_OBJ='getopt$o'
])
AC_SUBST(GETOPT_OBJ)

dnl
dnl Checks for libraries.
dnl

dnl $with_ssl can be one of:
dnl  - empty string or "auto", meaning autodetect SSL and use it if found.
dnl  - "yes", meaning link with SSL or bail out.
dnl  - "no", meaning don't link with SSL.
dnl  - anything else, meaning use that as the SSL root, and bail out
dnl    if it fails.

if test x"$with_ssl" = x; then
  dnl Canonicalize unspecified with-ssl setting to "auto".  This
  dnl implements the "auto-detect by default" feature.  If you want to
  dnl change this to "ignore SSL by default", change "auto" to "no".
  with_ssl=auto
fi

dnl Detection of OpenSSL is much hairier than the detection of other
dnl libraries because OpenSSL can be compiled as a third-party
dnl library, which means it will not be found without additional
dnl linker magic.  It would be really nice to rework this check into
dnl an AC_DEFUN so that we can reuse it for other third-party
dnl libraries.

if test x"$with_ssl" != x"no"; then

  wget_force_ssl=no
  if test x"$with_ssl" = x"yes"; then
    wget_force_ssl=yes
  fi

  if test x"$with_ssl" = x"yes" || test x"$with_ssl" = x"auto"; then
    dnl OpenSSL's root was not specified, so we have to guess.  First
    dnl try the system default location, then "/usr/local/ssl" (where
    dnl OpenSSL installs by default), then "/usr/local" (traditional
    dnl choice for installation root), then "/opt".
    ssl_all_roots="system-default /usr/local/ssl /usr/local /opt"
  else
    dnl Root has been specified by the user.
    ssl_all_roots=$with_ssl
    wget_force_ssl=yes
  fi

  wget_save_CC=$CC
  wget_save_LIBS=$LIBS
  wget_save_LDFLAGS=$LDFLAGS
  wget_save_CPPFLAGS=$CPPFLAGS

  dnl Use libtool for OpenSSL tests so we can specify "-R<rpath>"
  dnl without having to know how the linker handles it.
  CC="$SHELL ./libtool $CC"

  dnl Unfortunately, as of this writing (OpenSSL 0.9.6), the libcrypto
  dnl shared library doesn't record its dependency on libdl, so we
  dnl need to check for it ourselves so we won't fail to link due to a
  dnl lack of -ldl.  Most OSes use dlopen(), but HP-UX uses
  dnl shl_load().
  AC_CHECK_LIB(dl,dlopen)
  AC_CHECK_LIB(dl,shl_load)

  ssl_success=no

  dnl Now try to find SSL libraries in each of the likely SSL roots.
  for ssl_root in $ssl_all_roots
  do
    if test x"$ssl_root" = x"system-default"; then
      dnl Try the default include and library locations.
      SSL_INCLUDES=
    else
      dnl Try this specific root.
      SSL_INCLUDES=-I$ssl_root/include
      dnl It's OK to use -R directly because libtool will
      dnl interpret it for us.
      LDFLAGS="-L$ssl_root/lib -R$ssl_root/lib $wget_save_LDFLAGS"
    fi

    AC_MSG_NOTICE([looking for SSL libraries in $ssl_root])

    dnl Check whether the compiler can find the include files.  On
    dnl some systems Gcc finds libraries in /usr/local/lib, but fails
    dnl to find the includes in /usr/local/include.

    ssl_found_includes=no
    CPPFLAGS="$SSL_INCLUDES $wget_save_CPPFLAGS"

    AC_MSG_CHECKING([for includes])
    AC_COMPILE_IFELSE([
#include <openssl/ssl.h>
#include <openssl/rsa.h>
    ], [
      AC_MSG_RESULT(found)
      ssl_found_includes=yes
    ], [
      AC_MSG_RESULT([not found])
    ])

    if test x"$ssl_found_includes" = xno; then
      continue
    fi

    ssl_link_failure=no

    dnl Make sure that the checks don't run afoul of the cache.  It
    dnl would be nicer to temporarily turn off the cache, but
    dnl apparently Autoconf doesn't allow that.

    unset ac_cv_lib_crypto_RSA_new
    unset ac_cv_lib_ssl_SSL_new

    dnl These checks need to be in this order, or you'll get a link
    dnl failure if you use a static libcrypto.a and libssl.a rather
    dnl than shared libraries.

    AC_CHECK_LIB(crypto, RSA_new, , ssl_link_failure=yes)
    AC_CHECK_LIB(ssl, SSL_new, , ssl_link_failure=yes)

    if test x"$ssl_link_failure" = xyes; then
      dnl One or both libs failed to link.
      continue
    fi

    dnl The libraries link.  But we still need to check if the program
    dnl linked with those libraries under these settings with run.  On
    dnl some systems (Solaris), Gcc adds -L/usr/local/lib to the
    dnl linking line, but fails to add -R/usr/local/lib, thus creating
    dnl executables that link, but fail to run.

    dnl If we are cross-compiling, just assume that working linkage
    dnl implies working executable.

    ssl_run_failure=no

    AC_MSG_CHECKING([whether SSL libs are resolved at runtime])
    AC_TRY_RUN([
int RSA_new();
int SSL_new();
main(){return 0;}
],
      AC_MSG_RESULT(yes),
      AC_MSG_RESULT(no); ssl_run_failure=yes,
      AC_MSG_RESULT(cross)
    )

    if test x"$ssl_run_failure" = xno; then
      ssl_success=yes
      break
    fi
  done

  if test x"$ssl_success" = xyes; then
    AC_MSG_NOTICE([compiling in support for SSL in $ssl_root])
    AC_DEFINE([HAVE_SSL], 1,
              [Define if SSL support should be compiled in.])
    AC_SUBST(SSL_INCLUDES)
    SSL_OBJ='gen_sslfunc$o'
    AC_SUBST(SSL_OBJ)
  else
    LDFLAGS=$wget_save_LDFLAGS
    LIBS=$wget_save_LIBS

    dnl If linking with SSL was forced rather than auto-detected, then
    dnl bail out if SSL failed.
    if test x"$wget_force_ssl" = x"yes"; then
      AC_MSG_ERROR([failed to find OpenSSL libraries], 2)
    fi
  fi

  dnl Restore the compiler setting.
  CC=$wget_save_CC

  dnl Restore the CPPFLAGS.  Do this regardless of whether linking
  dnl with SSL succeeded -- SSL includes will be handled using
  dnl @SSL_INCLUDES@.
  CPPFLAGS=$wget_save_CPPFLAGS
fi

dnl
dnl Find an md5 implementation.
dnl

if test x"$wget_need_md5" = xyes
then
  dnl This should be moved to an AC_DEFUN, but I'm not sure how to
  dnl manipulate MD5_OBJ from the defun.

  MD5_OBJ='gen-md5$o'
  found_md5=no

  dnl Check for the system MD5 library on Solaris.  We don't check for
  dnl something simple like "MD5Update" because there are a number of
  dnl MD5 implementations that use that name.  md5_calc is, hopefully,
  dnl specific to the Solaris MD5 library.
  if test x"$found_md5" = xno; then
    AC_CHECK_LIB(md5, md5_calc, [
      dnl Some installations have bogus <md5.h> in the compiler's
      dnl include path, making the system md5 library useless.
      AC_MSG_CHECKING([for working md5.h])
      AC_COMPILE_IFELSE([#include <md5.h>
                        ], [
        AC_MSG_RESULT(yes)
        AC_DEFINE([HAVE_SOLARIS_MD5], 1, [Define to use Solaris MD5.])
        LIBS="-lmd5 $LIBS"
        found_md5=yes
        AC_MSG_NOTICE([using the Solaris MD5 implementation])
      ], [AC_MSG_RESULT(no)])
    ])
  fi

  dnl Then see if we're linking OpenSSL anyway; if yes, use its md5
  dnl implementation.
  if test x"$found_md5" = xno; then
    if test x"$ssl_success" = xyes; then
      AC_DEFINE([HAVE_OPENSSL_MD5], 1, [Define to use OpenSSL MD5.])
      found_md5=yes
      AC_MSG_NOTICE([using the OpenSSL MD5 implementation])
    fi
  fi

  dnl If none of the above worked, use the one we ship with Wget.
  if test x"$found_md5" = xno; then
    AC_DEFINE([HAVE_BUILTIN_MD5], 1, [Define to use built-in MD5.])
    MD5_OBJ="$MD5_OBJ gnu-md5\$o"
    found_md5=yes
    AC_MSG_NOTICE([using the GNU MD5 implementation])
  fi
fi
AC_DEFINE([HAVE_MD5], 1, [Define if we're compiling support for MD5.])
AC_SUBST(MD5_OBJ)

dnl **********************************************************************
dnl Checks for IPv6
dnl **********************************************************************

dnl
dnl If --enable-ipv6 is specified, we try to use IPv6 (as long as
dnl getaddrinfo is also present).  If --disable-ipv6 is specified, we
dnl don't use IPv6 or getaddrinfo.
dnl

ipv6=
check_for_ipv6=no
AC_ARG_ENABLE(ipv6,
  AC_HELP_STRING([--disable-ipv6],[disable IPv6 support]),
  [case "${enable_ipv6}" in
  no)
    AC_MSG_NOTICE([disabling IPv6 at user request])
    ipv6=no
    ;;
  *)
    ipv6=yes
    ;;
  esac],
  dnl If unspecified, check for IPv6 and use it where available.
  [check_for_ipv6=yes]
)

if test "X$ipv6" = "Xyes" || test "X$check_for_ipv6" = "Xyes"; then
  AC_CHECK_FUNCS(getaddrinfo, [
    GETADDRINFO_AI_ADDRCONFIG([
      AC_DEFINE(
        [HAVE_GETADDRINFO_AI_ADDRCONFIG], 1, 
        [Define if the system headers support the AI_ADDRCONFIG flag.]
      )
    ])
  ], [
    AC_MSG_NOTICE([Disabling IPv6 support: your system does not support getaddrinfo(3)])
    ipv6=no
  ])
fi

if test "X$ipv6" = "Xyes" || test "X$check_for_ipv6" = "Xyes"; then
  PROTO_INET6([], [
    AC_MSG_NOTICE([Disabling IPv6 support: your system does not support the PF_INET6 protocol family])
    ipv6=no
  ])
fi

if test "X$ipv6" = "Xyes" || test "X$check_for_ipv6" = "Xyes"; then
  TYPE_STRUCT_SOCKADDR_IN6([
    ipv6=yes
  ],[
    AC_MSG_NOTICE([Disabling IPv6 support: your system does not support 'struct sockaddr_in6'])
    ipv6=no
  ])
  if test "X$ipv6" = "Xyes"; then
    WGET_STRUCT_SOCKADDR_STORAGE
    MEMBER_SIN6_SCOPE_ID
  fi
fi

if test "X$ipv6" = "Xyes"; then
  AC_DEFINE([ENABLE_IPV6], 1, [Define if IPv6 support is enabled.])
fi


dnl
dnl Set of available languages.
dnl
ALL_LINGUAS=`(cd ${srcdir}/po && ls *.po | sed -e 's/\.po$//' | tr '\012' ' ')`

dnl Originally this used to be static, looking like this:
dnl     ALL_LINGUAS="cs de hr it ..."
dnl The downside was that configure needed to be rebuilt whenever a
dnl new language was added.

dnl internationalization macros
WGET_WITH_NLS

dnl
dnl Find makeinfo.  We used to provide support for Emacs processing
dnl Texinfo using `emacs -batch -eval ...' where makeinfo is
dnl unavailable, but that broke with the addition of makeinfo-specific
dnl command-line options, such as `-I'.  Now we depend on makeinfo to
dnl build the Info documentation.
dnl

AC_CHECK_PROGS(MAKEINFO, makeinfo)

dnl
dnl Find perl and pod2man
dnl

AC_PATH_PROGS(PERL, perl5 perl, no)
AC_PATH_PROG(POD2MAN, pod2man, no)

if test "x${POD2MAN}" = xno; then
  COMMENT_IF_NO_POD2MAN="# "
else
  COMMENT_IF_NO_POD2MAN=
fi
AC_SUBST(COMMENT_IF_NO_POD2MAN)

dnl
dnl Create output
dnl
AC_CONFIG_FILES([Makefile src/Makefile doc/Makefile util/Makefile
                 po/Makefile.in windows/Makefile])
AC_CONFIG_HEADERS([src/config.h])
AH_BOTTOM([
#include "config-post.h"
])
AC_CONFIG_FILES([stamp-h], [echo timestamp > stamp-h])
AC_CONFIG_COMMANDS([default], [WGET_PROCESS_PO])
AC_OUTPUT
