
dnlAC_INIT(src/libosiris/utilities.h)
AC_INIT
AM_CONFIG_HEADER(config.h)
AC_CANONICAL_SYSTEM
AM_INIT_AUTOMAKE( osiris, 2.0.0 )
AM_MAINTAINER_MODE

OS_NAME=`uname`
AUTOHEADER=echo  

dnl configure db library.

echo "CONFIGURING LOCAL COPY OF BERKELY DB..."
cd ./src/db-4.1.24/build_unix && ../dist/configure --enable-shared=no --enable-static=yes && cd ../../.. 
echo "DONE CONFIGURING LOCAL COPY OF BERKELY DB."


dnl check for OpenSSL library and headers.

AC_ARG_WITH(ssl-dir,
        [  --with-ssl-dir=PATH     Specify path to OpenSSL installation ],
        [
                if test "x$withval" != "xno" ; then
                        tryssldir=$withval
                fi
        ]
)

if test "x$prefix" != "xNONE" ; then
        tryssldir="$tryssldir $prefix"
fi


AC_DEFUN([AC_TYPE_SOCKLEN_T], [
  dnl Since the old-style (autoconf 2.13) macro AC_CHECK_TYPE(type, replacement)
  dnl only checks in <sys/types.h> we have to be more awkward:
  dnl We have to check for socklen_t in <sys/types.h> -- if it's not defined
  dnl there, we also look in <sys/socket.h> (a more common place)
  AC_CHECK_TYPE(socklen_t,,
    AC_DEFINE(socklen_t, int, [Define to `int' if neither <sys/types.h> nor <sys/socket.h> define.]),
    [
#include <sys/types.h>
#include <sys/socket.h>
  ])
])

AC_CACHE_CHECK([for OpenSSL directory], ac_cv_openssldir, [
	for ssldir in $tryssldir "" /usr/local/openssl /usr/lib/openssl /usr/local/ssl /usr/lib/ssl /usr/local /usr/pkg /opt /opt/openssl ; do
                CPPFLAGS="$saved_CPPFLAGS"
                LDFLAGS="$saved_LDFLAGS"
                LIBS="$saved_LIBS -lssl -lcrypto"

                # Skip directories if they don't exist
                if test ! -z "$ssldir" -a ! -d "$ssldir" ; then
                        continue;
                fi
                if test ! -z "$ssldir" -a "x$ssldir" != "x/usr"; then
                        # Try to use $ssldir/lib if it exists, otherwise
                        # $ssldir
                        if test -d "$ssldir/lib" ; then
                                LDFLAGS="-L$ssldir/lib $saved_LDFLAGS"
                                if test ! -z "$need_dash_r" ; then
                                        LDFLAGS="-R$ssldir/lib $LDFLAGS"
                                fi
                        else
                                LDFLAGS="-L$ssldir $saved_LDFLAGS"
                                if test ! -z "$need_dash_r" ; then
                                        LDFLAGS="-R$ssldir $LDFLAGS"
                                fi
                        fi

                        # Try to use $ssldir/include if it exists, otherwise
                        # $ssldir
                        if test -d "$ssldir/include" ; then
                                CPPFLAGS="-I$ssldir/include $saved_CPPFLAGS"
                        else
                                CPPFLAGS="-I$ssldir $saved_CPPFLAGS"
                        fi
                fi

                # Basic test to check for compatible version and correct linking

                AC_TRY_RUN(
                        [
#include <string.h>
#include <openssl/rand.h>
int main(void)
{
        char a[2048];
        memset(a, 0, sizeof(a));
        RAND_add(a, sizeof(a), sizeof(a));
        return(RAND_status() <= 0);
}
                        ],
                        [
                                found_crypto=1
                                break;
                        ], []
                )

                if test ! -z "$found_crypto" ; then
                        break;
                fi
        done

        if test -z "$found_crypto" ; then
                AC_MSG_ERROR([Could not find working OpenSSL library, please install.])
        fi
        if test -z "$ssldir" ; then
                ssldir="(system)"
        fi

        ac_cv_openssldir=$ssldir
])

if (test ! -z "$ac_cv_openssldir" && test "x$ac_cv_openssldir" != "x(system)") ; then
#         AC_DEFINE(HAVE_OPENSSL)
        dnl Need to recover ssldir - test above runs in subshell
        ssldir=$ac_cv_openssldir
        if test ! -z "$ssldir" -a "x$ssldir" != "x/usr"; then
                # Try to use $ssldir/lib if it exists, otherwise
                # $ssldir
                if test -d "$ssldir/lib" ; then
                        LDFLAGS="-L$ssldir/lib $saved_LDFLAGS"
                        if test ! -z "$need_dash_r" ; then
                                LDFLAGS="-R$ssldir/lib $LDFLAGS"
                        fi
                else
                        LDFLAGS="-L$ssldir $saved_LDFLAGS"
                        if test ! -z "$need_dash_r" ; then
                                LDFLAGS="-R$ssldir $LDFLAGS"
                        fi
                fi
                # Try to use $ssldir/include if it exists, otherwise
                # $ssldir
                if test -d "$ssldir/include" ; then
                        CPPFLAGS="-I$ssldir/include $saved_CPPFLAGS"
                else
                        CPPFLAGS="-I$ssldir $saved_CPPFLAGS"
                fi
        fi
fi
LIBS="$saved_LIBS -lssl -lcrypto"

dnl look at msghdr structure a little more closely.

AC_CACHE_CHECK([for msg_accrights field in struct msghdr],
        ac_cv_have_accrights_in_msghdr, [
    AC_TRY_RUN(
        [
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/uio.h>
int main() {
#ifdef msg_accrights
exit(1);
#endif
struct msghdr m;
m.msg_accrights = 0;
exit(0);
}
        ],
        [ ac_cv_have_accrights_in_msghdr="yes" ],
        [ ac_cv_have_accrights_in_msghdr="no" ]
    )
])
if test "x$ac_cv_have_accrights_in_msghdr" = "xyes" ; then
    AC_DEFINE(HAVE_ACCRIGHTS_IN_MSGHDR,1,access rights in msghdr)
fi

AC_CACHE_CHECK([for msg_control field in struct msghdr],
        ac_cv_have_control_in_msghdr, [
    AC_TRY_RUN(
        [
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/uio.h>
int main() {
#ifdef msg_control
exit(1);
#endif
struct msghdr m;
m.msg_control = 0;
exit(0);
}
        ],
        [ ac_cv_have_control_in_msghdr="yes" ],
        [ ac_cv_have_control_in_msghdr="no" ]
    )
])
if test "x$ac_cv_have_control_in_msghdr" = "xyes" ; then
    AC_DEFINE(HAVE_CONTROL_IN_MSGHDR,1,ancillary data style fd pass)
fi


dnl check for programs

AC_PROG_CC
AC_PROG_CXX
AC_PROG_RANLIB

dnl check for network libraries.

AC_SEARCH_LIBS(socket, socket)
AC_SEARCH_LIBS(gethostbyname, nsl)

dnl check for GUI libraries.

dnl OSIRIS_LIB_QT
dnl OSIRIS_LIB_X11

case $OS_NAME in   
FreeBSD*)
    AC_DEFINE_UNQUOTED( SYSTEM_FREEBSD, 1, Darwin or Mac OS X )
    ;;
OpenBSD)
    AC_DEFINE_UNQUOTED( SYSTEM_OPENBSD, 1, Darwin or Mac OS X )
    ;;
Linux)
    AC_DEFINE_UNQUOTED( SYSTEM_LINUX, 1, Darwin or Mac OS X )
    ;;
BSD*)
    AC_DEFINE_UNQUOTED( SYSTEM_BSDOS, 1, Darwin or Mac OS X )
    ;;
Darwin*)
    QT_LIBS="-framework Carbon -framework Quicktime -lz"
    AC_DEFINE_UNQUOTED( SYSTEM_DARWIN, 1, Darwin or Mac OS X )
    ;;
*)
    QT_LIBS="-L${x11_libdir} -lXext -lX11 -lm"
    ;; 
esac

dnl check for pthreads.

#AC_HAVE_LIBRARY(pthread, LOAD_LIBS="$LOAD_LIBS -lpthread")
AC_CHECK_LIB(pthread, pthread_create)


dnl check for types we need to have.

AC_CHECK_TYPES(u_int16_t)
AC_CHECK_TYPES(u_int32_t)
AC_CHECK_TYPES(u_int64_t)

AC_TYPE_SOCKLEN_T

dnl replace functions that we don't have.

AC_REPLACE_FUNCS(inet_ntop)
AC_REPLACE_FUNCS(inet_pton)
AC_REPLACE_FUNCS(strptime)
AC_REPLACE_FUNCS(strcasestr)
AC_REPLACE_FUNCS(socketpair)
AC_REPLACE_FUNCS(pipe)
AC_REPLACE_FUNCS(recvmsg)
AC_REPLACE_FUNCS(sendmsg)
AC_REPLACE_FUNCS(getdirentries)

dnl check for header files

AC_CHECK_HEADERS(regex.h)
AC_CHECK_HEADERS(sys/types.h)
AC_CHECK_HEADERS(sys/time.h)
AC_CHECK_HEADERS(sys/utsname.h)
AC_CHECK_HEADERS(termios.h)
AC_CHECK_HEADERS(sys/uio.h)

dnl define whether or not we use privsep.

AC_MSG_CHECKING(if we can use privilege separation)

dnl we need: socketpair, getdirentries, sys/uio.h, sendmsg, and recvmsg.

if test "$ac_cv_func_getdirentries" = "yes" &&
   test "$ac_cv_func_sendmsg" = "yes" &&
   test "$ac_cv_func_recvmsg" = "yes" &&
   test "$ac_cv_func_socketpair" = "yes" ; then
        AC_DEFINE( USE_PRIVSEP,1,use privilege separation)
        CONFIG_USE_PRIVSEP="yes"
        AC_MSG_RESULT(yes)
else
    AC_MSG_RESULT(no)
    CONFIG_USE_PRIVESEP="no"
    echo ""
    echo "**************************************************************"
    echo ""
    echo "  WARNING: this system does not have the required methods "
    echo "  to support privilege separation.  This means the osiris "
    echo "  scanning daemon will run as root."
    echo ""
    echo "**************************************************************"
    echo ""
fi

AM_CONDITIONAL(BUILD_DARWIN,test "$OS_NAME" == "Darwin")
AM_CONDITIONAL(BUILD_QT_GUI,test "x$qt_moc" != "x" -a "x$qt_root" != "x" )

AC_HEADER_STDC
AC_C_BIGENDIAN

AC_SUBST(QT_LIBS)
AC_SUBST(qt_root)
AC_SUBST(qt_moc)
AC_SUBST(x11_libdir)

AC_OUTPUT( 	Makefile \
		    src/Makefile \
		    src/libosiris/Makefile \
            src/libfileapi/Makefile \
            src/libosirisdb/Makefile \
            src/libosirisctl/Makefile \
            src/libosirism/Makefile \
		    src/cli/Makefile \
            src/gui/Makefile \
		    src/osirisd/Makefile \
            src/osirismd/Makefile \
            src/tools/Makefile \
            src/db-4.1.24/Makefile )


    echo ""
    echo "Osiris (c) 2000-2003 The Shmoo Group (TSG)"
    echo "-----------------------------------------------------"
    echo ""
    echo " ==> Configuration Complete. "
    echo " ==> Osiris has been configured with the following options:"
    echo ""
    echo "                 Host: ${host}"
    echo "             Compiler: ${CC}"
    echo "       Compiler flags: ${CFLAGS}"
    echo "   Preprocessor flags: ${CPPFLAGS}"
    echo "         Linker flags: ${LDFLAGS}"
    echo "            Libraries: ${LIBS}"
    echo "  Privlege Separation: ${CONFIG_USE_PRIVSEP}"
    echo "         SSL Location: ${ssldir}"
    echo ""
    echo " ==> use one of the following targets:"
    echo ""
    echo "       install: install scanner and management console."
    echo "       package: create scanner intsallation package."
    echo "       clean:   remove object files."
    echo ""
