dnl configure.in
dnl
dnl $Id: configure.in,v 1.13.2.2 2003/06/20 02:17:38 jp Exp $
dnl
dnl Copyright (c) 2001 Jean-Pierre Lefebvre <helix@step.polymtl.ca>
dnl                and Remi Lefebvre <remi@debian.org>
dnl
dnl atftp is free software; you can redistribute them and/or modify them
dnl under the terms of the GNU General Public License as published by the
dnl Free Software Foundation; either version 2 of the License, or (at your
dnl option) any later version.

AC_INIT(tftp.c)
AM_INIT_AUTOMAKE(atftp, 0.6.2)
AC_SUBST(VERSION)
AM_CONFIG_HEADER(config.h)

ISODATE=`date +%Y-%m-%d`
AC_SUBST(ISODATE)

dnl canonicalize the host
AC_CANONICAL_HOST
AC_CANONICAL_TARGET 
PLATFORM="${host_vendor}-${host_cpu}-${host_os}"
AC_SUBST(PLATFORM)

dnl Additional argument
AC_ARG_ENABLE(libwrap,
	[  --enable-libwrap        enable libwrap support in server],
	[ case "${enableval}" in
		yes) libwrap=true ;;
		no) libwrap=flase ;;
	  esac], [libwrap=true])

AC_ARG_ENABLE(debug,
	[  --enable-debug          enable debug code in server/client],
	[ case "${enableval}" in
		yes) debug=true ;;
		no)  debug=false ;;
	  esac], [debug=false])

dnl Check for programs
AC_PROG_CC

dnl Check for AIX
AC_AIX

CFLAGS="-g -Wall -D_REENTRANT"

if test x$debug = xtrue; then
   CFLAGS="$CFLAGS -O0 -DDEBUG"
else
  if test -n "$auto_cflags"; then
    if test -n "$GCC"; then
      CFLAGS="$CFLAGS -g -O2 -Wall -Wno-implicit"
    else
      case "$host_os" in
        *hpux*)  CFLAGS="$CFLAGS +O3"
	  ;;
        *ultrix* | *osf*) CFLAGS="$CFLAGS -O -Olimit 2000"
	  ;;
        *)       CFLAGS="$CFLAGS -O2"
	  ;;
      esac
    fi
  else
    CFLAGS="$CFLAGS -O2"
  fi
fi

AC_PROG_MAKE_SET
AC_PROG_INSTALL

dnl Checks for libraries
dnl Thread support is needed in the server
AC_CHECK_LIB(pthread, pthread_create, [LIBPTHREAD=-lpthread],
		      [AC_MSG_ERROR(not found)])
AC_SUBST(LIBPTHREAD)

dnl Debian's readline is already linked to ncurses. It is not the case for
dnl all other systems.
AC_CHECK_LIB(readline, tgetent, LIBTERMCAP="",
	AC_CHECK_LIB(ncurses, tgetent, LIBTERMCAP=-lncurses,
		AC_CHECK_LIB(curses, tgetent, LIBTERMCAP=-lcurses,
			AC_CHECK_LIB(termcap, tgetent, LIBTERMCAP=-ltermcap,
			AC_MSG_ERROR(no termcap library found)))))
AC_SUBST(LIBTERMCAP)

dnl Check for readline
AC_CHECK_LIB(readline, readline, LIBREADLINE=-lreadline,
	 AC_MSG_ERROR(libreadline not found), $LIBTERMCAP)
AC_SUBST(LIBREADLINE)

dnl Verify readline has completion support (this function changes from
dnl release to release :(
AC_CHECK_LIB(readline, rl_completion_matches,
	     AC_DEFINE(HAVE_RL_COMPLETION_MATCHES),
	     AC_CHECK_LIB(readline, completion_matches,
		          AC_DEFINE(HAVE_COMPLETION_MATCHES),
		          AC_MSG_WARN(no comlpetion support), $LIBTERMCAP),
	     $LIBTERMCAP)

dnl If we want to compile libwrap stuff, check for libraries
if test x$libwrap = xtrue; then
   dnl Checking to libwrap. Some systems don't define default values for
   dnl allow_severity and deny_severity so we need a somewhat more complicated
   dnl test here. We automatically link against nsl since libwrap is not
   dnl already linked with it on all systems.
   orig_LIBS="$LIBS"
   LIBS="-lnsl -lwrap"
   AC_MSG_CHECKING([for hosts_ctl in -lwrap])
   AC_TRY_LINK([
        #include <tcpd.h>
        int allow_severity;
        int deny_severity;
        ],
	hosts_ctl("", STRING_UNKNOWN, STRING_UNKNOWN, STRING_UNKNOWN);
        ,
        LIBWRAP="-lnsl -lwrap"
        LIBS="$orig_LIBS"
        AC_MSG_RESULT(yes)
        ,
        LIBWRAP=""
        LIBS="$orig_LIBS"
	AC_MSG_ERROR(libwrap no found)
   )
   AC_SUBST(LIBWRAP)
fi

dnl Checks for header files.
AC_HEADER_STDC
AC_CHECK_HEADERS(sys/time.h sys/types.h sys/socket.h)
AC_CHECK_HEADERS(arpa/inet.h arpa/tftp.h)
AC_CHECK_HEADERS(getopt.h unistd.h signal.h pthread.h argz.h)
AC_CHECK_HEADERS(netdb.h)
AC_CHECK_HEADERS(readline/readline.h)
AC_CHECK_HEADERS(readline/history.h)
if test x$libwrap = xtrue; then
AC_CHECK_HEADERS(tcpd.h)
fi

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

dnl Checks for library functions.
AC_CHECK_FUNCS(strchr memcpy strstr strcmp strncmp strncpy strlen)
AC_CHECK_FUNCS(strncasecmp strcasecmp strncmp)
AC_CHECK_FUNCS(socket gethostbyname gethostbyaddr)

dnl Write platform to file for support reporting
AC_OUTPUT_COMMANDS([
	outfile=PLATFORM
	tmpfile=${outfile}
	cat > $tmpfile << _EOF_
$PLATFORM
_EOF_
], [PLATFORM=$PLATFORM])

AC_OUTPUT(Makefile)
