AC_INIT(README)

# handle case when -srcdir is invoked: make location directory for generated
# header files
if test ! -d ./dis ; then
	mkdir ./dis
fi

AC_PROG_CC
AC_PROG_INSTALL
AC_PROG_RANLIB
AC_HEADER_STDC
AC_CHECK_HEADER(sys/time.h, AC_DEFINE(HAVE_SYS_TIME_H))
AC_CHECK_HEADERS(stdlib.h unistd.h string.h memory.h fcntl.h)
AC_HEADER_TIME

AC_CHECK_LIB(socket, bind, [LIBS="$LIBS -lsocket"]) 
AC_CHECK_LIB(nsl, xdr_free, [LIBS="$LIBS -lnsl"])
AC_CHECK_LIB(gdbm, dbm_fetch, [LIBS="$LIBS -lgdbm"])


AC_DEFUN(AC_STRUCT_MSG_ACCRIGHTS,
[AC_CACHE_CHECK([for msg_accrights in struct msghdr], ac_cv_struct_msg_accrights,
[AC_TRY_COMPILE([#include <sys/types.h>
#include <sys/socket.h>
#include <sys/uio.h>], [struct msghdr s; s.msg_accrights;],
ac_cv_struct_msg_accrights=yes, ac_cv_struct_msg_accrights=no)])
if test $ac_cv_struct_msg_accrights = yes; then
  AC_DEFINE(HAVE_MSG_ACCRIGHTS)
fi
])

AC_DEFUN(AC_STRUCT_MSG_CONTROL,
[AC_CACHE_CHECK([for msg_control in struct msghdr], ac_cv_struct_msg_control,
[AC_TRY_COMPILE([#include <sys/types.h>
#include <sys/socket.h>
#include <sys/uio.h>], [struct msghdr s; s.msg_control;],
ac_cv_struct_msg_control=yes, ac_cv_struct_msg_control=no)])
if test $ac_cv_struct_msg_control = yes; then
  AC_DEFINE(HAVE_MSG_CONTROL)
fi
])

AC_DEFUN(AC_HAVE_RECVMSG,
[AC_CACHE_CHECK([for recvmsg system call], ac_cv_have_recvmsg,
[AC_TRY_LINK([#include <sys/types.h>
#include <sys/socket.h>
#include <sys/uio.h>], [int s, f; struct msghdr m; recvmsg(s, &m, f);],
ac_cv_have_recvmsg=yes, ac_cv_have_recvmsg=no)])
if test $ac_cv_have_recvmsg = yes; then
  AC_DEFINE(HAVE_RECVMSG)
fi
])

AC_STRUCT_MSG_ACCRIGHTS
AC_STRUCT_MSG_CONTROL
AC_HAVE_RECVMSG

AC_CHECK_LIB(elf, elf_end, [LIBS="$LIBS -lelf"])

# Solaris special handling ...
if uname -sr | grep '^SunOS 5' >/dev/null ; then
        LIBS="$LIBS -R/usr/ucblib -L/usr/ucblib -lucb"
fi

AC_HAVE_LIBRARY(dnet_stub, [LIBS="$LIBS -ldnet_stub"])

if uname -srv | grep '^AIX 2 3' >/dev/null ; then
    :
else
    AC_HAVE_LIBRARY(bsd, [LIBS="$LIBS -lbsd"])
fi

LIBS="$LIBS -lm"
AC_REPLACE_FUNCS(strdup)

dnl Check for dbm_* routine buried in libc (Solaris)
AC_CHECK_LIB(c, dbm_fetch, [DBMLIB=""], [DBMLIB="-ldbm"])

AC_MSG_CHECKING("whether RPCGEN generates ANSI C function prototypes")
cat > conftest.x << EOF
program TESTPRG
{
    version TESTVER
    {
        void TESTFUN(void) = 0;
    } = 1;
} = 1;
EOF
if rpcgen -h conftest.x | grep testfun_1_svc > /dev/null
then 
AC_MSG_RESULT("prototyped")
AC_DEFINE(HAVE_NEW_RPCGEN)
else
AC_MSG_RESULT("classic")
fi

AC_SUBST(dsubdirs)
AC_SUBST(DBM)
AC_SUBST(DB_LOADER)
AC_SUBST(SIMX_INCLUDES)
dsubdirs="lib"

dnl Hide away non-dbm libraries, since mdw_CHECK_MANYLIBS always
dnl modifies LIBS. This might not be necessary - the only consequence
dnl of not doing this is that the test programs get needlessly linked 
dnl against dbm.
NON_DBM_LIBS="$LIBS"

mdw_CHECK_MANYLIBS(dbm_fetch,db dbm gdbm,[
  	AC_DEFINE(HAVE_NDBM)
 	DBM="$LIBS"
        DB_LOADER=create-tables-4
],[
  	dsubdirs="$dsubdirs sdbm"
 	DBM="$LIBS -L../sdbm -lsdbm"
  	SIMX_INCLUDES="-I../sdbm"
  	DB_LOADER=create-tables-5
])
LIBS="$NON_DBM_LIBS"

dsubdirs="$dsubdirs server data test"

AC_CONFIG_SUBDIRS(disgen)

AC_OUTPUT(Makefile lib/Makefile sdbm/Makefile server/Makefile data/Makefile test/Makefile)
