AC_INIT(xinetd/main.c)

AC_CANONICAL_SYSTEM

AC_PROG_CC
AC_PROG_INSTALL
AC_PROG_RANLIB

AC_CHECK_LIB(compat, main)
AC_SEARCH_LIBS(gethostbyname, nsl socket resolv)
AC_SEARCH_LIBS(getnameinfo, nsl socket resolv)
AC_SEARCH_LIBS(inet_ntop, nsl socket resolv)
AC_CHECK_FUNCS(getnameinfo inet_ntop)
AC_CHECK_FUNCS(difftime, ,
   AC_CHECK_LIB(m, difftime))
AC_CHECK_FUNCS(fcvt, ,
   AC_CHECK_LIB(m, fcvt))
AC_CHECK_FUNCS(ecvt, ,
   AC_CHECK_LIB(m, ecvt))
AC_CHECK_FUNCS(gcvt, ,
   AC_CHECK_LIB(m, gcvt))

AC_CHECK_FUNC(strerror, [AC_DEFINE(HAVE_STRERROR)])
AC_CHECK_FUNC(strcasecmp, [AC_DEFINE(HAVE_STRCASECMP)])
AC_CHECK_FUNC(socket, ,
   AC_CHECK_LIB(socket, socket, ,
      AC_CHECK_LIB(nsl, socket)))
AC_CHECK_FUNC(inet_aton, ,
	AC_CHECK_LIB(nsl, inet_aton, ,
		AC_CHECK_LIB(socket, inet_aton, ,
			AC_CHECK_LIB(resolv, inet_aton, ,[AC_DEFINE(NO_INET_ATON)]))))
AC_CHECK_FUNC(setenv,[AC_DEFINE(HAVE_SETENV)])
AC_CHECK_FUNC(strsignal, [AC_DEFINE(HAVE_STRSIGNAL)])
AC_CHECK_LIB(c, sys_siglist, [AC_DEFINE(HAVE_SYS_SIGLIST)])
AC_CHECK_FUNC(gai_strerror,[AC_DEFINE(HAVE_GAI_STRERROR)])
AC_CHECK_FUNC(freeaddrinfo,[AC_DEFINE(HAVE_FREEADDRINFO)])
AC_CHECK_FUNC(getaddrinfo,[AC_DEFINE(HAVE_GETADDRINFO)])

AC_CHECK_HEADERS(sys/types.h sys/termios.h termios.h sys/ioctl.h sys/select.h rpc/rpc.h rpc/rpcent.h sys/file.h ftw.h machine/reg.h netdb.h)
AC_CHECK_HEADER(sys/resource.h, [AC_DEFINE(HAVE_SYS_RESOURCE_H)])
AC_CHECK_HEADER(arpa/inet.h, [AC_DEFINE(HAVE_ARPA_INET_H)])
AC_CHECK_HEADER(grp.h, [AC_DEFINE(HAVE_GRP_H)])
AC_CHECK_HEADER(rpc/pmap_clnt.h, [AC_DEFINE(HAVE_RPC_PMAP_CLNT_H)])
AC_CHECK_HEADER(sys/socket.h, [AC_DEFINE(HAVE_SYS_SOCKET_H)])
AC_CHECK_HEADER(sys/signal.h, [AC_DEFINE(HAVE_SYS_SIGNAL_H)])
AC_CHECK_HEADER(crypt.h, [AC_DEFINE(HAVE_CRYPT_H)])
AC_CHECK_HEADER(stdint.h, [AC_DEFINE(HAVE_STDINT_H)])
AC_CHECK_HEADER(stdbool.h, [AC_DEFINE(HAVE_STDBOOL_H)])
AC_CHECK_HEADER(sys/filio.h, [AC_DEFINE(HAVE_SYS_FILIO_H)])
AC_CHECK_HEADER(DNSServiceDiscovery/DNSServiceDiscovery.h, [AC_DEFINE(HAVE_DNSREGISTRATION) AC_DEFINE(HAVE_MDNS)])

AC_ARG_WITH(howl, [  --with-howl=PATH        Compile in howl support.  
                          PATH is the prefix where howl is installed,
                          and is not optional.],
   [ OLDCFLAGS=$CFLAGS; CFLAGS="-I$withval/include/howl/ $CFLAGS"; 
     OLDLDFLAGS=$LDFLAGS; LDFLAGS="-L$withval/lib/ $LDFLAGS"; 
     OLDCPPFLAGS=$CPPFLAGS; CPPFLAGS="-I$withval/include/howl/ $CPPFLAGS";
     AC_CHECK_HEADER($withval/include/howl/howl.h, 
        [AC_DEFINE(HAVE_HOWL) AC_DEFINE(HAVE_MDNS)], 
        [CFLAGS=$OLDCFLAGS; LDFLAGS=$OLDLDFLAGS; CPPFLAGS=$OLDCPPFLAGS;]) 
     AC_CHECK_LIB(howl, sw_discovery_publish, [LIBS="-lhowl $LIBS"], [
        OLDLIBS=$LIBS; LIBS="-lpthread $LIBS"; 
        AC_CHECK_LIB(howl, main, [LIBS="-lhowl $LIBS"],
           [LIBS=$OLDLIBS]) 
     ])
   ])

XINETD_CHECK_TYPE(uint16_t, unsigned short)
XINETD_CHECK_TYPE(uint32_t, unsigned int)
XINETD_CHECK_TYPE(uint64_t, unsigned long long)
XINETD_CHECK_TYPE(uid_t, unsigned int)
XINETD_CHECK_TYPE(gid_t, unsigned int)
XINETD_CHECK_TYPE(socklen_t, unsigned int)
XINETD_CHECK_TYPE(rlim_t, long long)

AC_CACHE_CHECK([for struct addrinfo], ac_cv_have_struct_addrinfo, [
        AC_TRY_COMPILE(
                [
#include <sys/types.h>
#include <sys/socket.h>
#ifdef HAVE_NETDB_H
#include <netdb.h>
#endif
                ],
                [ struct addrinfo s; s.ai_flags = AI_PASSIVE; ],
                [ ac_cv_have_struct_addrinfo="yes" ],
                [ ac_cv_have_struct_addrinfo="no" ]
        )
])
if test "x$ac_cv_have_struct_addrinfo" = "xyes" ; then
        AC_DEFINE(HAVE_STRUCT_ADDRINFO)
fi

AC_CACHE_CHECK([for struct in6_addr], ac_cv_have_struct_in6_addr, [
        AC_TRY_COMPILE(
                [
#include <sys/types.h>
#include <netinet/in.h>
                ],
                [ struct in6_addr s; s.s6_addr[0] = 0; ],
                [ ac_cv_have_struct_in6_addr="yes" ],
                [ ac_cv_have_struct_in6_addr="no" ]
        )
])
if test "x$ac_cv_have_struct_in6_addr" = "xyes" ; then
        AC_DEFINE(HAVE_STRUCT_IN6_ADDR)
fi

AC_CACHE_CHECK([for struct sockaddr_in6], ac_cv_have_struct_sockaddr_in6, [
        AC_TRY_COMPILE(
                [
#include <sys/types.h>
#include <netinet/in.h>
                ],
                [ struct sockaddr_in6 s; s.sin6_family = 0; ],
                [ ac_cv_have_struct_sockaddr_in6="yes" ],
                [ ac_cv_have_struct_sockaddr_in6="no" ]
        )
])
if test "x$ac_cv_have_struct_sockaddr_in6" = "xyes" ; then
        AC_DEFINE(HAVE_STRUCT_SOCKADDR_IN6)
fi

AC_CACHE_CHECK([for struct sockaddr_storage], ac_cv_have_struct_sockaddr_storage, [
        AC_TRY_COMPILE(
                [
#include <sys/types.h>
#include <sys/socket.h>
                ],
                [ struct sockaddr_storage s; ],
                [ ac_cv_have_struct_sockaddr_storage="yes" ],
                [ ac_cv_have_struct_sockaddr_storage="no" ]
        )
])
if test "x$ac_cv_have_struct_sockaddr_storage" = "xyes" ; then
        AC_DEFINE(HAVE_STRUCT_SOCKADDR_STORAGE)
fi


case "$target_os" in

solaris*)
	AC_DEFINE(N0_SIGLIST)
	AC_DEFINE(solaris)

	AC_MSG_CHECKING(whether to compile in loadavg)
	AC_ARG_WITH(loadavg,
	[  --with-loadavg[=PATH]   Compile in loadavg support.],
	[ case "$withval" in
		no)
			AC_MSG_RESULT(no)
			;;
		yes)
			AC_MSG_RESULT(yes)
			AC_CHECK_LIB(kstat, main)
			AC_CHECK_HEADER(kstat.h, [AC_DEFINE(HAVE_KSTAT_H)])
			AC_DEFINE(HAVE_LOADAVG)
			;;
		*)
			AC_MSG_RESULT(yes)
			AC_DEFINE(HAVE_LOADAVG)
			;;
	esac ], AC_MSG_RESULT(no) )
	;;
osf*)
	LDFLAGS=-oldstyle_liblookup
	AC_MSG_CHECKING(whether to compile in loadavg)
	AC_ARG_WITH(loadavg,,
	[ case "$withval" in
		no)
			AC_MSG_RESULT(no)
			;;
		yes)
			AC_MSG_RESULT(yes)
			AC_DEFINE(HAVE_LOADAVG)
			;;
		*)
			AC_MSG_RESULT(yes)
			AC_DEFINE(HAVE_LOADAVG)
			;;
	esac ], AC_MSG_RESULT(no))
	;;
linux*|freebsd*)
	AC_CHECK_LIB(crypt, main)

	AC_MSG_CHECKING(whether to compile in loadavg)
	AC_ARG_WITH(loadavg,,
	[ case "$withval" in
		no)
			AC_MSG_RESULT(no)
			;;
		yes)
			AC_MSG_RESULT(yes)
			AC_DEFINE(HAVE_LOADAVG)
			;;
		*)
			AC_MSG_RESULT(yes)
			AC_DEFINE(HAVE_LOADAVG)
			;;
	esac ], AC_MSG_RESULT(no))
	;;
darwin* | "Mac OS"*)
	ac_cv_prog_RANLIB="ranlib"
	# AC_CHECK_FUNCS doesn't look in the proper header file...
	AC_DEFINE(HAVE_ISATTY)
	CFLAGS="$CFLAGS -no-cpp-precomp"
	AC_MSG_CHECKING(whether to compile in loadavg)
	AC_ARG_WITH(loadavg,,
	[ case "$withval" in
		no)
			AC_MSG_RESULT(no)
			;;
		yes)
			AC_MSG_RESULT(yes)
			AC_DEFINE(HAVE_LOADAVG)
			;;
		*)
			AC_MSG_RESULT(yes)
			AC_DEFINE(HAVE_LOADAVG)
			;;
	esac ], AC_MSG_RESULT(no))
	;;
*bsdi*)
       AC_MSG_CHECKING(whether to compile in loadavg)
       AC_ARG_WITH(loadavg,,
       [ case "$withval" in
               no)
                       AC_MSG_RESULT(no)
                       ;;
               yes)
                       AC_MSG_RESULT(yes)
                       AC_DEFINE(bsdi)
                       AC_DEFINE(HAVE_LOADAVG)
                       ;;
               *)
                       AC_MSG_RESULT(yes)
                       AC_DEFINE(bsdi)
                       AC_DEFINE(HAVE_LOADAVG)
                       ;;
       esac ], AC_MSG_RESULT(no))
       ;;
esac


AC_CHECK_LIB(c, crypt, [:], [
 AC_CHECK_LIB(crypt, crypt, [
  LIBS="-lcrypt $LIBS"
  AC_DEFINE(HAVE_LIBCRYPT) ], []) ])

AC_CHECK_LIB(m, log10, [ LIBS="-lm $LIBS" ], [])

AC_MSG_CHECKING(whether to default to ipv6)
AC_ARG_WITH(inet6, [  --with-inet6    Compile in ipv6 support. (obsolete)],
AC_MSG_RESULT(ignored), AC_MSG_RESULT(ignored) )

AC_MSG_CHECKING(whether to use libwrap)
AC_ARG_WITH(libwrap,
[  --with-libwrap[=PATH]   Compile in libwrap (tcp_wrappers) support.],
[ case "$withval" in
	no)
		AC_MSG_RESULT(no)
		;;
	yes)
		AC_MSG_RESULT(yes)
		AC_CHECK_LIB(wrap, request_init, [
			AC_DEFINE(LIBWRAP)
			WRAPLIBS="-lwrap"
			AC_DEFINE(HAVE_LIBWRAP) ])
		AC_CHECK_LIB(nsl, yp_get_default_domain, [ 
			WRAPLIBS="$WRAPLIBS -lnsl" ])
		LIBS="$WRAPLIBS $LIBS"
		;;
	*)
		AC_MSG_RESULT(yes)
		AC_DEFINE(LIBWRAP)
		if test -d "$withval"; then
			WRAPLIBS="-L$withval -lwrap"
		else
			WRAPLIBS="$withval"
		fi
		AC_CHECK_LIB(nsl, yp_get_default_domain, [ 
			WRAPLIBS="$WRAPLIBS -lnsl" ])
		OLDLIBS="$LIBS"
		LIBS="$WRAPLIBS $LIBS"
		AC_TRY_LINK([ int allow_severity; int deny_severity; ], 
			[ hosts_access(); ], [], [ AC_MSG_ERROR(Could not find the $withval library.  You must first install tcp_wrappers.) ])
		;;
	esac ],
	AC_MSG_RESULT(no)
)

AC_MSG_CHECKING(whether to use labeled-networking)
AC_ARG_WITH(labeled-networking,
[  --with-labeled-networking[=PATH]   Compile in labeled networking support.],
[ case "$withval" in
	no)
		AC_MSG_RESULT(no)
		;;
	yes)
		AC_MSG_RESULT(yes)
		AC_CHECK_LIB(selinux, setexeccon, [
			AC_DEFINE(LABELED_NET)
			LABELLIBS="-lselinux" ])
		LIBS="$LABELLIBS $LIBS"
		;;
	*)
		AC_MSG_RESULT(yes)
		AC_DEFINE(LABELED_NET)
		if test -d "$withval"; then
			LABELLIBS="-L$withval -lselinux"
		else
			LABELLIBS="$withval"
		fi
		LIBS="$LABELLIBS $LIBS"
		;;
	esac ],
	AC_MSG_RESULT(no)
)

AC_FUNC_MMAP

AC_CHECK_FUNCS(isatty)
AC_CHECK_FUNCS(memcpy)
AC_CHECK_FUNCS(waitpid)
AC_CHECK_FUNCS(sigvec)
AC_CHECK_FUNCS(setsid)
AC_CHECK_FUNCS(strftime)

# AC_CHECK_TYPE(R_OK,4)


mkdir -p libs/src/misc
mkdir -p libs/src/pset
mkdir -p libs/src/sio
mkdir -p libs/src/str
mkdir -p libs/src/xlog
mkdir -p libs/include
mkdir -p libs/lib
mkdir -p libs/man

AC_CONFIG_HEADER(config.h)
AC_OUTPUT(Makefile libs/src/misc/Makefile libs/src/pset/Makefile libs/src/sio/Makefile libs/src/str/Makefile libs/src/xlog/Makefile libs/src/portable/Makefile xinetd/Makefile)

cp config.h libs/include
