AC_INIT(reconf)

AM_CONFIG_HEADER(config.h)
AM_INIT_AUTOMAKE(pptpd,1.2.1)
AC_PROG_CC
AC_PROG_RANLIB
AC_PROG_INSTALL
AC_PROG_LN_S
AC_PROG_MAKE_SET

AC_C_CONST
AC_C_INLINE

AC_EXEEXT
AC_OBJEXT
AC_SYS_INTERPRETER

AC_CHECK_FUNCS(setsid daemon setproctitle getservbyname strlcpy fork memmove strerror)

AC_CHECK_HEADERS(pty.h)
AC_CHECK_HEADERS(string.h)
AC_CHECK_HEADERS(syslog.h)
AC_CHECK_HEADERS(libintl.h)
AC_CHECK_HEADERS(libutil.h)

AC_CHECK_TYPE(size_t, unsigned int)
AC_CHECK_TYPE(ssize_t, int)
AC_CHECK_TYPE(u_int8_t, unsigned char)
AC_CHECK_TYPE(u_int16_t, unsigned short)
AC_CHECK_TYPE(u_int32_t, unsigned int)

dnl Check for type in sys/socket.h - from Squid source (GPL)
AC_CACHE_CHECK(for socklen_t, ac_cv_type_socklen_t, [
  AC_EGREP_CPP([socklen_t[^a-zA-Z_0-9]], [#include <sys/types.h>
#include <sys/socket.h>
#if STDC_HEADERS
#include <stdlib.h>
#include <stddef.h>
#endif],
    ac_cv_type_socklen_t=yes,
    ac_cv_type_socklen_t=no)
])
if test $ac_cv_type_socklen_t = no; then
  AC_DEFINE(socklen_t, int)
fi

dnl Check for libwrap (black magic check)
AC_ARG_WITH(libwrap,
	[  --with-libwrap          Use libwrap (tcp wrappers) ],
[
 XYZZY_LIBS="$LIBS"
 AC_MSG_CHECKING(for libwrap alone)
 LIBS="$XYZZY_LIBS -lwrap"
 AC_TRY_LINK([ int allow_severity, deny_severity; ],
	     [ hosts_access(); ],
	     [ AC_MSG_RESULT(yes)
	       AC_DEFINE(HAVE_LIBWRAP)
	       LIBWRAP="yes"
	       XTRALIBS_MGR="-lwrap" ],
	     [ AC_MSG_RESULT(no)
   LIBS="$XYZZY_LIBS -lwrap -lnsl"
   AC_MSG_CHECKING(for libwrap with libnsl)
   AC_TRY_LINK([ int allow_severity, deny_severity; ],
	       [ hosts_access(); ],
	       [ AC_MSG_RESULT(yes)
	         AC_DEFINE(HAVE_LIBWRAP)
		 LIBWRAP="yes"
	         XTRALIBS_MGR="-lwrap -lnsl" ],
	       [ AC_MSG_RESULT(no) ])
 ])
 LIBS="$XYZZY_LIBS"
])

dnl More ugliness; -lnsl, -lutil and -lsocket
XYZZY_LIBS="$LIBS"
AC_CHECK_LIB(c, accept)
AC_CHECK_LIB(c, gethostbyname)
AC_CHECK_LIB(c, openpty)
AC_CHECK_LIB(c, gettext)
AC_CHECK_LIB(socket, accept)
AC_CHECK_LIB(nsl, gethostbyname)
AC_CHECK_LIB(util, openpty)
AC_CHECK_LIB(intl, gettext)
LIBS="$XYZZY_LIBS"
if test "$ac_cv_lib_c_accept" = no; then
  if test "$ac_cv_lib_socket_accept" = yes; then
	LIBS="$LIBS -lsocket"
  else
	echo "Couldn't find a usable accept!" 1>&2
	exit 1
  fi
fi
if test "$ac_cv_lib_c_gethostbyname" = no; then
  if test "$ac_cv_lib_nsl_gethostbyname" = yes; then
	LIBS="$LIBS -lnsl"
	if test "$XTRALIBS_MGR" = "-lwrap -lnsl"; then
	  XTRALIBS_MGR="-lwrap"
	fi
  else
	echo "Couldn't find a usable gethostbyname!" 1>&2
	exit 1
  fi
fi
if test "$ac_cv_lib_c_openpty" = yes; then
  AC_DEFINE(HAVE_OPENPTY)
else
  if test "$ac_cv_lib_util_openpty" = yes; then
    AC_DEFINE(HAVE_OPENPTY)
    XTRALIBS_CTRL="-lutil"
  fi
fi
if test "$ac_cv_header_libintl_h" = yes; then
  if test "$ac_cv_lib_c_gettext" = no; then
    if test "$ac_cv_lib_intl_gettext" = yes; then
      XTRALIBS_MGR = "$XTRALIBS_MGR -lintl"
    else
      echo "Have libintl.h but no usable gettext!" 1>&2
      exit 1
    fi
  fi
fi

AC_SUBST(XTRALIBS_CTRL)
AC_SUBST(XTRALIBS_MGR)
AC_SUBST(HAVE_OPENPTY)

AC_ARG_WITH(bsdppp,
	[  --with-bsdppp           Use BSD user-space ppp ],
	[ AC_DEFINE(BSDUSER_PPP)
	  BSDUSER_PPP=yes ])

AC_ARG_WITH(slirp,
	[  --with-slirp            Use SLIRP instead of pppd ],
	[ AC_DEFINE(SLIRP)
	  SLIRP=yes ])

AC_ARG_WITH(pppd-ip-alloc,
	[  --with-pppd-ip-alloc    Let pppd allocate IP addresses ],
	[ AC_DEFINE(PPPD_IP_ALLOC)
	  PPPD_IP_ALLOC=yes ])

AC_ARG_WITH(bcrelay,
	[  --with-bcrelay          Enable broadcast relay function ],
	[ AC_DEFINE(BCRELAY)
	  BCRELAY=yes ])

AC_ARG_WITH(pns-mode,
	[  --with-pns-mode         Option is NOT FUNCTIONAL YET ],
	[ AC_DEFINE(PNS_MODE)
	  PNS_MODE = yes ])

if test "$BSDUSER_PPP" = "yes"; then
  AC_DEFINE(PPP_BINARY, "/usr/sbin/ppp")
else
  if test "$SLIRP" = "yes"; then
    AC_DEFINE(PPP_BINARY, "/bin/slirp")
  else
    AC_DEFINE(PPP_BINARY, "/usr/sbin/pppd")
  fi
fi

if test "$SLIRP" = "yes"; then
  if test "$PPPD_IP_ALLOC" = "yes"; then
    true
  else
    echo "SLIRP mode selected - disabling PoPToP internal IP allocation." 1>&2
    AC_DEFINE(PPPD_IP_ALLOC)
    PPPD_IP_ALLOC = yes
  fi
fi

if test "$BSDUSER_PPP" = "yes"; then
  if test "$PPPD_IP_ALLOC" = "yes"; then
    true
  else
    echo "BSD user-space ppp selected - disabling PoPToP internal IP allocation." 1>&2
    AC_DEFINE(PPPD_IP_ALLOC)
    PPPD_IP_ALLOC = yes
  fi
fi

if test "$BCRELAY" = "yes"; then
  if test "$BCRELAY" = "yes"; then
    XTRA_PROG="bcrelay"
    true
  else
    echo "No BCrelay selected." 1>&2
  fi
fi

if test "$PNS_MODE" = "yes"; then
  if test "$PPPD_IP_ALLOC" = "yes"; then
    true
  else
    echo "PNS mode selected - disabling PoPToP internal IP allocation." 1>&2
    AC_DEFINE(PPPD_IP_ALLOC)
    PPPD_IP_ALLOC = yes
  fi
fi

AC_SUBST(XTRA_PROG)

echo '==============================================================================='

echo 'Configuration chosen:'
echo -n '   PPPd IP allocation: '
if test "$PPPD_IP_ALLOC" = "yes"; then
  echo 'Yes.'
else
  echo 'No.'
fi

echo -n '   PPPd:               '
if test "$BSDUSER_PPP" = "yes"; then
  echo 'BSD user-space PPPd.'
else
  if test "$SLIRP" = "yes"; then
    echo 'SLIRP.'
  else
    echo 'Standard.'
  fi
fi

echo -n '   LIBWRAP security:   '
if test "$LIBWRAP" = "yes"; then
  echo 'Yes.'
else
  echo 'No.'
fi

echo -n '   Broadcast Relay:    '
if test "$BCRELAY" = "yes"; then
  echo 'Yes.'
else
  echo 'No.'
fi

echo -n '   Mode:               '
if test "$PNS_MODE" = "yes"; then
  echo 'Incoming call server (PNS - NOT COMPLETE YET).'
else
  echo 'IP tunneling server (PAC).'
fi

AC_OUTPUT(Makefile)
