dnl Process this file with autoconf to produce a configure script.
AC_INIT(include/netinet/if_ether.h)
dnl Checks for programs.
AC_PROG_CC
dnl AC_PROG_INSTALL
dnl AC_PROG_MAKE_SET

AC_CHECK_LIB(nsl, main, result=yes, result=no)
if test $result = "yes"; then
    LIBS="$LIBS -lnsl"
fi
AC_CHECK_LIB(resolv, main, result=yes, result=no)
if test $result = "yes"; then
    LIBS="$LIBS -lresolv"
fi
AC_CHECK_LIB(rpc, main, result=yes, result=no)
if test $result = "yes"; then
    LIBS="$LIBS -lrpc"
fi

AC_SUBST(HOSTNAME)
HOSTNAME=$HOSTNAME
AC_SUBST(HOSTNAME)

if test "`uname -r | grep \"\-co\-\"`"; then
AC_SUBST(COLINUX)
COLINUX="YES"
AC_SUBST(COLINUX)
fi

dnl check for socket compatibility libraries (Solaris)
check_tirpc=""
AC_MSG_CHECKING(for rpc socket compatibility)
echo "main() {return(1);}" > configtest.c
if test -z "`${CC-cc} -o configtest.out configtest.c /usr/ucblib/librpcsoc.so 2>&1`"; then
    AC_MSG_RESULT(yes)
    # do we accept the -R option?
    AC_MSG_CHECKING(for runtime linkage option)
    if test -z "`${CC-cc} -o configtest.out -R/usr/lib configtest.c 2>&1`"; then
	AC_MSG_RESULT(yes)
	LIBS="-L/usr/ucblib -R/usr/ucblib -lrpcsvc -lrpcsoc $LIBS"
    else
	AC_MSG_RESULT(no)
	check_tirpc="y"
    fi
else
    AC_MSG_RESULT(no)
    check_tirpc="y"
fi
rm -f configtest.*

dnl check for librpcsvc only if we're not already using
dnl /usr/ucblib/librpcsvc
if test -n "$check_tirpc"; then
  AC_CHECK_LIB(rpcsvc, main, result=yes, result=no)
  if test $result = "yes"; then
    LIBS="$LIBS -lrpcsvc"
  fi
fi
AC_CHECK_LIB(socket, main, result=yes, result=no)
if test $result = "yes"; then
    LIBS="$LIBS -lsocket"
fi
AC_CHECK_LIB(sun, getpwnam, result=yes, result=no)
if test $result = "yes"; then
    LIBS="$LIBS -lsun"
fi
AC_CHECK_LIB(PW, main, result=yes, result=no)
if test $result = "yes"; then
    dnl must go before other libs on HP-UX 11
    LIBS="-lPW $LIBS"
fi

dnl check for +DAportable option (HP-UX)
AC_MSG_CHECKING(for +DAportable)
echo "main() {return(1);}" > configtest.c
if test -z "`${CC-cc} -c $CFLAGS +DAportable $CPPFLAGS configtest.c 2>&1`"; then
    AC_MSG_RESULT(yes)
    AC_MSG_CHECKING(for +DAportable when linking)
    if test -z "`${CC-cc} -o configtest $CFLAGS +DAportable $CPPFLAGS $LDFLAGS configtest.c $LIBS 2>&1`"; then
	AC_MSG_RESULT(yes)
        CFLAGS="$CFLAGS +DAportable"
    else
	AC_MSG_RESULT(no)
    fi
else
    AC_MSG_RESULT(no)
fi
rm -f configtest.*

dnl check for missing /usr/include/asm link
skip_limits_h=""
AC_CHECK_HEADER(asm/socket.h, result=yes, result=no)
if test $result = no; then
    AC_MSG_CHECKING(for socket.h under /usr/src/linux)
    echo "#include <asm/socket.h>" > configtest.c
    echo "main() {return(1);}" >> configtest.c
    if test -z "`${CC-cc} -I/usr/src/linux/include -c -o configtest.o configtest.c 2>&1`"; then
        AC_MSG_RESULT(yes)
	CPPFLAGS="$CPPFLAGS -I/usr/src/linux/include"
	skip_limits_h="y"
    else
        AC_MSG_RESULT(no)
	AC_MSG_CHECKING(for socket.h in asm-i386)
	if test -r /usr/src/linux/include/asm-i386/socket.h; then
	    CPPFLAGS="$CPPFLAGS -I /usr/src/linux/include/asm-i386"
	    AC_MSG_RESULT(yes)
	else
	    AC_MSG_RESULT(no)
	fi
    fi
    rm configtest.*
fi

dnl check for missing /usr/include/linux link
if test -z "$skip_limits_h"; then
    AC_CHECK_HEADER(linux/limits.h, result=yes, result=no)
    if test $result = no; then
        AC_MSG_CHECKING(for limits.h under /usr/src/linux)
        AC_TRY_COMPILE([#include "/usr/src/linux/include/linux/limits.h"], [return(0)], result=yes, result=no)
        if test $result = yes; then
	    CPPFLAGS="$CPPFLAGS -I/usr/src/linux/include"
        fi
        AC_MSG_RESULT($result)
    fi
fi

AC_CHECK_HEADER(stdint.h, result=yes, result=no)
if test $result = yes; then
     AC_DEFINE(USE_STDINT)
fi

dnl header files
#AC_HEADER_STDC

dnl check for TIRPC only if we're not using the rpc socket compatibility libraries
if test -n "$check_tirpc"; then
    AC_MSG_CHECKING(for TIRPC compatibility)
    AC_TRY_COMPILE([#include <rpc/rpc.h>
#include <sys/tiuser.h>
#include <stropts.h>], [return(0)], result=yes, result=no)
    if test $result = yes; then
	AC_DEFINE(TIRPC)
    fi
    AC_MSG_RESULT($result)
fi

dnl typedefs
AC_TYPE_GETGROUPS

dnl check whether sys_errlist is declared in stdio.h or errno.h
AC_MSG_CHECKING(if sys_errlist is declared)
AC_TRY_COMPILE([#include <stdio.h>
#include <errno.h>], [char *msg = *(sys_errlist + 1)], result=yes, result=no)
if test $result = yes; then
    AC_DEFINE(SYS_ERRLIST_DECLARED)
fi
AC_MSG_RESULT($result)

dnl check whether we need our own netinet
AC_MSG_CHECKING(if system netinet headers work)
AC_TRY_COMPILE([#include <sys/types.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
#include <netinet/tcp.h>], [int i = ICMP_UNREACH; int i1 = ICMP_UNREACH_HOST;
int j = ICMP_MINLEN; int j1 = ICMP_UNREACH_PORT;
struct icmp k; struct tcphdr *l;
int m = sizeof(l->th_dport)], result=yes, result=no)
AC_MSG_RESULT($result)
if test $result = no; then
#    CPPFLAGS="$CPPFLAGS -I`pwd`/include"
    AC_DEFINE(_BSD_SOURCE)
    dnl check for rpc includes
    AC_MSG_CHECKING(for rpc includes)
    AC_TRY_COMPILE([#include <rpc/rpc.h>], [return(0)], result=yes, result=no)
    if test $result = yes; then
        AC_MSG_RESULT(no)
    else
        CPPFLAGS="$CPPFLAGS -I`pwd`/include/ansi"
        AC_MSG_RESULT(yes)
    fi
fi
AC_SUBST(SQLSTATE)
SQLSTATE=YES
AC_ARG_WITH(sqlat,
[  --with-sqlat[=arg]        Build SQLAT programs (arg=yes/no) (yes)])
if test "$with_sqlat" = "no"; then
AC_SUBST(SQLSTATE)
SQLSTATE=NO
fi
AC_SUBST(SSHSTATE)
SSHSTATE=YES
AC_ARG_WITH(ssh,
[  --with-ssh[=arg]          Build SARA ssh (arg=yes/no) (yes)])
if test "$with_ssh" = "no"; then
  AC_SUBST(SSHSTATE)
  SSHSTATE=NO
else
  if (test -f /usr/local/ssl/lib/libssl.a); then
    AC_SUBST(SSLPATH)
    SSLPATH="/usr/local/ssl"
    echo "checking for ssl libraries to build ssh .. yes"
  elif (test -f /usr/lib/libssl.a); then
    AC_SUBST(SSLPATH)
    SSLPATH="/usr/ssl"
    echo "checking for ssl libraries to build ssh .. yes"
  else
    echo "checking for ssl libraries to build ssh .. no"
    echo "ssh will not be built (ssl libraries not found)"
    AC_SUBST(SSHSTATE)
    SSHSTATE=NO
  fi
  if (test -f "/usr/local/lib/libz.a"); then
    echo "checking for zlib libraries to build ssh .. yes"
  elif (test -f "/usr/lib/libz.a"); then
    echo "checking for zlib libraries to build ssh .. yes"
  else
    echo "checking for zlib libraries to build ssh .. no"
    echo " -- install latest version of zlib (www.zlib.org) --"
    echo "ssh will not be built (zlib libraries not found)"
    AC_SUBST(SSHSTATE)
    SSHSTATE=NO
  fi
fi

AC_SUBST(VERSION)
VERSION=OZOB

AC_SUBST(PROGS)
PROGS=all

dnl check whether we need to compile rpcgen
mypath=$PATH:/usr/bin:/usr/sbin:/bin:/sbin:/usr/local/bin:/usr/local/sbin
AC_PATH_PROG(RPCGEN,rpcgen,../../bin/rpcgen,$mypath)
if test $RPCGEN = "../../bin/rpcgen"; then
    PROGS="rpcgen $PROGS"
fi
AC_OUTPUT(Makefile)
