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

AC_INIT(install-sh)
AC_PREREQ([2.50])
AC_CONFIG_HEADER(config.h)

AC_SUBST(TARGETS)
AC_SUBST(PLTARGETS)
AC_SUBST(NETLIBS)
AC_SUBST(CRYPTLIBS)

m4_include([../ac_swi_c.m4])

TARGETS="random.$SO unix.$SO socket.$SO cgi.$SO memfile.$SO files.$SO mime.$SO crypt.$SO"

AC_CHECK_FUNC(socket, [], [
             AC_CHECK_LIB(socket, socket,
                      [NETLIBS="$NETLIBS -lsocket"; LIBS="$LIBS -lsocket"]
                      AC_DEFINE(HAVE_LIBSOCKET, 1,
                      "Define if you have the socket library (-lsocket)."))])
AC_CHECK_FUNC(gethostent, [], [
             AC_CHECK_LIB(nsl, gethostent,
                      [NETLIBS="$NETLIBS -lnsl"]
                      AC_DEFINE(HAVE_LIBNSL, 1,
                      "Define if you have the nsl library (-lnsl)."))])

oldlibs="$LIBS"
AC_CHECK_LIB(crypt, crypt)
CRYPTLIBS="$LIBS"
AC_CHECK_FUNCS(crypt)
LIBS="$oldlibs"

AC_MSG_CHECKING("Configuring MIME libraries")
case "$CC" in
	.*) ACC="../../$CC"
	    ;;
	*)  ACC="$CC"
esac
case "$LD" in
	.*) ALD="../../$LD"
	    ;;
	*)  ALD="$LD"
esac
case "$CPP" in
	.*) ACPP="../../$CPP"
	;;
	*) ACPP="$CPP"
esac
( CC=$ACC; LD=$ALD; CPP=$ACPP; CFLAGS=$CFLAGS; LDFLAGS=$LDFLAGS;
  export CC LD CFLAGS LDFLAGS

  (cd maildrop/rfc822 && $SHELL configure)
  (cd maildrop/rfc2045 && $SHELL configure)
)
AC_MSG_RESULT("Done")

AC_MSG_CHECKING(h_errno)
AC_TRY_COMPILE(
[
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
],
[ int x = h_errno;
], AC_DEFINE(HAVE_H_ERRNO, 1,
	     [Define of h_errno is provided])
   AC_MSG_RESULT(yes),
   AC_MSG_RESULT(no))

AC_CHECK_HEADERS(malloc.h alloca.h unistd.h sys/time.h fcntl.h utime.h)
AC_CHECK_HEADERS(execinfo.h sys/resource.h crypt.h)
AC_CHECK_HEADERS(sys/types.h sys/wait.h sys/stat.h)
AC_CHECK_HEADERS(netinet/tcp.h)

AC_CHECK_FUNCS(setsid strerror setitimer utime getrlimit)
AC_CHECK_TYPES(socklen_t, [], [],
[
#include <sys/types.h>
#include <sys/socket.h>
])
AC_CHECK_TYPES(ssize_t, [], [],
[
#include <sys/types.h>
#include <sys/socket.h>
])

AC_MSG_CHECKING(_XOPEN_SOURCE)
AC_TRY_COMPILE(
[
#define _XOPEN_SOURCE
#include <unistd.h>
],
[
], AC_DEFINE(DEFINE_XOPEN_SOURCE, 1,
	     "Define if _XOPEN_SOURCE is needed")
   AC_MSG_RESULT(yes),
   AC_MSG_RESULT(no))

with_time=yes
AC_ARG_WITH(time, [  --without-time		  Exclude alarm library],
	    [case "$withval" in
		yes)	with_time=yes ;;
		no)     with_time=no ;;
	     esac])

if test "$with_time" = yes; then
  if test "$ac_cv_func_setitimer" = "yes"; then
    TARGETS="$TARGETS time.$SO"
  else
    echo "WARNING: no setitimer(); dropping time.pl from targets"
  fi
fi

if test "$ac_cv_func_getrlimit" = "yes"; then
  TARGETS="$TARGETS rlimit.$SO"
else
  "WARNING: no getrlimit(); dropping rlimit.pl from targets"
fi

PLTARGETS=`echo $TARGETS | sed -e "s/\.$SO/.pl/g" -e "s/files.pl/filesex.pl/"`
if echo "$PLTARGETS" | grep socket.pl 2>&1 >/dev/null; then
  PLTARGETS="$PLTARGETS streampool.pl"
fi

AC_OUTPUT(Makefile)