dnl Process this file with autoconf to produce a configure script.
AC_INIT(rpsl/rpsl/object.cc)
AC_CONFIG_HEADER(config.h)
AC_CONFIG_SUBDIRS(Core)
AC_CONFIG_SUBDIRS(rpsl)

AC_CANONICAL_HOST

dnl Version number control
top_srcdir="`dirname $0`"
VERSION="(RAToolSet v`cat ${top_srcdir}/VERSION`) ${USER}@`hostname`"
VERSION2=RAToolSet`cat ${top_srcdir}/VERSION`
AC_SUBST(VERSION)
AC_SUBST(VERSION2)

AC_ARG_ENABLE(debug, [  --enable-debug          turn on debugging],
if eval "test x$enable_debug = xyes"; then
  OPTFLAG="-gstabs+ -DDEBUG -fno-inline"
else
  OPTFLAG="-O3 -gstabs+"
fi
,
OPTFLAG="-O3 -gstabs+"
)

case "${host_os}" in
      solaris* )
              EXTRA_LIBS="-lsocket -lnsl"
      ;;
      bsdi* )
dnl Commented out by wlee@isi.edu
dnl           EXTRA_INCLUDE_DIRS="-I/usr/include/g++ -I/usr/include/g++/stdc++"
              EXTRA_LIBS="-lipc"
      ;;
      osf*|aix* )
              OPTFLAG=
      ;;
esac

: ${CFLAGS="$OPTFLAG"}
: ${CXXFLAGS="$OPTFLAG -frtti"}

all_INCDIRS='-I$(top_srcdir)/src/Core -I$(top_srcdir)/src/rpsl'
AC_SUBST(all_INCDIRS)
AC_SUBST(EXTRA_INCLUDE_DIRS)
AC_SUBST(EXTRA_LIBS)
AC_DEFINE_UNQUOTED(EXTRA_INCLUDE_DIRS, ${EXTRA_INCLUDE_DIRS})
AC_DEFINE_UNQUOTED(EXTRA_LIBS, ${EXTRA_LIBS})

dnl Checks for programs.
AC_PROG_MAKE_SET
dnl AC_PATH_PROG(PERL_PATH,perl,/usr/local/bin/perl)
AC_PROG_CC
AC_PROG_CPP
AC_PROG_CXX
AC_PROG_CXXCPP
AC_PROG_INSTALL
AC_PROG_MAKE_SET(MAKE=make)
AC_ARG_PROGRAM
AC_PROG_LN_S
AC_PROG_LEX
dnl Warn if not using flex
XXXLEX=`echo $LEX | awk '{print $1}'`
if test "`basename $XXXLEX`" != "flex"
then
        echo '****************************************************************';
        echo '**';
        echo '**   WARNING:  You should compile RAToolSet using GNU flex.';
        echo "**   Currently, you're using ``$LEX''";
        echo '**   ';
        echo '**   To get GNU flex, please goto ftp://prep.ai.mit.edu/pub/gnu/';
        echo '**   ';
        echo '****************************************************************';
        sleep 5
fi

AC_PROG_YACC
dnl Warn if not using bison
XXXYACC=`echo $YACC | awk '{print $1}'`
if test "`basename $XXXYACC`" != "bison"
then
        echo '*****************************************************************';
        echo '**';
        echo '**   WARNING:  You should compile RAToolSet using GNU bison.';
        echo "**   Currently, you're using ``$YACC''";
        echo '**   ';
        echo '**   To get GNU bison, please goto ftp://prep.ai.mit.edu/pub/gnu/';
        echo '**   ';
        echo '*****************************************************************';
        sleep 5
fi

AC_PROG_INSTALL
AC_C_CONST
AC_C_BIGENDIAN

: ${LD=$CXX}
AC_SUBST(LD)

AC_PROG_RANLIB

dnl Checks for libraries.
AC_CHECK_LIB(dl, dlopen, [
TCL_NEEDS_DL="-ldl"
], [
TCL_NEEDS_DL=""
])

READLINE_LIBS=""
AC_ARG_ENABLE(readline, [  --enable-readline       support fancy command line editing (default: yes)],
if eval "test x$enable_readline = xyes"; then
  AC_CHECK_LIB(termcap, main)
  AC_CHECK_LIB(readline, main)
  AC_CHECK_LIB(history, main)
  READLINE_LIBS="$LIBS"
fi
,
AC_CHECK_LIB(termcap, main)
AC_CHECK_LIB(readline, main)
AC_CHECK_LIB(history, main)
READLINE_LIBS="$LIBS"
)

AC_SUBST(READLINE_LIBS)
LIBS=""

dnl Checks for header files.
AC_HEADER_STDC
AC_CHECK_HEADERS(limits.h sys/time.h unistd.h)
AC_CHECK_HEADERS(string.h memory.h malloc.h)
AC_CHECK_HEADERS(sys/sockio.h)

dnl Checks for typedefs, structures, and compiler characteristics.
AC_TYPE_SIZE_T
AC_HEADER_TIME
AC_FUNC_SETPGRP

AC_MSG_CHECKING(for struct timeval)
AC_TRY_COMPILE([#ifdef TIME_WITH_SYS_TIME
#include <sys/time.h>
#include <time.h>
#else
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#else
#include <time.h>
#endif
#endif], [static struct timeval x; x.tv_sec = x.tv_usec;],
  [AC_MSG_RESULT(yes)
   AC_DEFINE(HAVE_TIMEVAL)],
  AC_MSG_RESULT(no))

AC_MSG_CHECKING(for whether rusage uses timeval)
AC_EGREP_CPP([timeval[[ \t]]*ru_utime],
[#include <sys/resource.h>
#if HAVE_SYS_RUSAGE_H
#include <sys/rusage.h>
#endif
], [AC_MSG_RESULT(yes)
   AC_DEFINE(RUSAGE_USES_TIMEVAL)],
  AC_MSG_RESULT(no)) 

dnl Check if socklen_t is defined
AC_MSG_CHECKING(for whether accept uses type (socklen_t *) as parameter)
AC_TRY_COMPILE([
#include <sys/types.h>
#include <sys/socket.h>
], [
	int sock;
    	struct sockaddr socknew;	
	int addrlen = 5;
	int newsock = accept (sock, &socknew, (socklen_t *) &addrlen);
], [AC_DEFINE(ACCEPT_USES_SOCKLEN_T) 
AC_MSG_RESULT(yes)], AC_MSG_RESULT(no)) 	


dnl Checks for library functions.
AC_CHECK_FUNCS(gettimeofday socket strstr memcpy vsnprintf)

AC_LANG_SAVE
AC_LANG_CPLUSPLUS

AC_MSG_CHECKING(if we need to declare sys_errlist)
AC_EGREP_CPP(sys_errlist, [
#include<cerrno>
#include<cstdio>
], [AC_MSG_RESULT(no)], [
AC_DEFINE(NEED_TO_DECLARE_SYS_ERRLIST)
AC_MSG_RESULT(yes)
])

AC_MSG_CHECKING(if struct ip contains ip_vhl instead of ip_v and ip_hl)
AC_EGREP_CPP(ip_vhl, [
extern "C" {
#include<netinet/ip.h>
}
], [
AC_DEFINE(STRUCT_IP_USES_VHL)
AC_MSG_RESULT(yes)
], [AC_MSG_RESULT(no)])

AC_MSG_CHECKING(if struct udphdr contains uh_ulen or len)
AC_TRY_COMPILE([
#include <sys/types.h>
#include <netinet/udp.h>
], [
    	struct udphdr u;
	u.uh_ulen = 1;
], [AC_DEFINE(HAVE_BSD_STYLE_UDPHDR) 
AC_MSG_RESULT(uh_ulen)], AC_MSG_RESULT(len)) 	

########################## htons ##################################

AC_MSG_CHECKING(if a declaration for htons is needed)
htons=notneeded
AC_TRY_COMPILE([
extern "C" {
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
}
], [
   int   port = 5;
   struct sockaddr_in server_sockaddr;
   server_sockaddr.sin_port = htons((u_short) port);
], [AC_MSG_RESULT(no)], [htons=needed])
if test $htons = needed; then
AC_TRY_COMPILE([
extern "C" {
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
extern u_short htons(...);
}
], [
   int   port = 5;
   struct sockaddr_in server_sockaddr;
   server_sockaddr.sin_port = htons((u_short) port);
], [AC_DEFINE(DEFINE_HTONS_WITH_ELLIPSIS) AC_MSG_RESULT(with ellipsis)], [
AC_DEFINE(DEFINE_HTONS_WITH_USHORT)  AC_MSG_RESULT(with ushort)])
fi
AC_LANG_RESTORE

########################## X11 ##################################

dnl Checks for X11
AC_PATH_X
if test "${x_libraries}" != NONE && test -n "${x_libraries}"; then
  LD_SWITCH_X_SITE=-L`echo ${x_libraries} | sed -e "s/:/ -L/g"`
fi
if test "${x_includes}" != NONE && test -n "${x_includes}"; then
  C_SWITCH_X_SITE=-I`echo ${x_includes} | sed -e "s/:/ -I/g"`
fi
AC_SUBST(LD_SWITCH_X_SITE)
AC_SUBST(C_SWITCH_X_SITE)
AC_DEFINE_UNQUOTED(LD_SWITCH_X_SITE, ${LD_SWITCH_X_SITE})
AC_DEFINE_UNQUOTED(C_SWITCH_X_SITE,  ${C_SWITCH_X_SITE})

dnl Use tcl/tk include path for testing (!)
dnl Save the original value
TMP_CPPFLAGS="$CPPFLAGS"
CPPFLAGS="${CPPFLAGS} ${C_SWITCH_X_SITE}"

dnl Checks for tcl/Tk
AC_CHECK_HEADER(tk.h, , [
if test -n "${TCL_TK_LIBRARY_DIR}"; then
  TMP=-L`echo ${TCL_TK_LIBRARY_DIR} | sed -e "s/:/ -L/g"`
  LD_SWITCH_X_SITE="${LD_SWITCH_X_SITE} ${TMP}"
fi
if test -n "${TCL_TK_INCLUDE_DIR}"; then
  TMP=-I`echo ${TCL_TK_INCLUDE_DIR} | sed -e "s/:/ -I/g"`
  C_SWITCH_X_SITE="${C_SWITCH_X_SITE} ${TMP}"
fi
])
CPPFLAGS="${TMP_CPPFLAGS}"

AC_SUBST(LD_TCL_TK_LIBS)
if test -z "${LD_TCL_TK_LIBS}"; then
  LD_TCL_TK_LIBS=" -ltk -ltcl"
fi
LD_TCL_TK_LIBS="${LD_TCL_TK_LIBS} ${TCL_NEEDS_DL}"

dnl Check if tk.h contains Tk_CreateMainWindow()
dnl
dnl Save the original value
TMP_CPPFLAGS="$CPPFLAGS"
dnl Assign new value
CPPFLAGS="$CPPFLAGS ${C_SWITCH_X_SITE}"
AC_MSG_CHECKING(for Tk_CreateMainWindow)
AC_EGREP_HEADER(Tk_CreateMainWindow, tk.h,
	[AC_DEFINE(HAVE_TK_CREATEMAINWINDOW)
	 AC_MSG_RESULT(yes)],
	[AC_MSG_RESULT(no)]
)
dnl Restore the original value
CPPFLAGS="$TMP_CPPFLAGS"


############################ prefix ################################

AC_SUBST(prefix)
AC_SUBST(exec_prefix)
AC_SUBST(bindir)
AC_SUBST(mandir)
# Remove any trailing slashes in these variables.
changequote(, )dnl
test "${prefix}" != NONE &&
  prefix=`echo "${prefix}" | sed 's,\([^/]\)/*$,\1,'`
test "${exec_prefix}" != NONE &&
  exec_prefix=`echo "${exec_prefix}" | sed 's,\([^/]\)/*$,\1,'`
changequote([, ])dnl

############################ OUTPUT ################################

AC_OUTPUT(
	Makefile
	util/Makefile
	util/version.cc
	gnug++/Makefile
	rpslcheck/Makefile
)
