dnl Process this file with autoconf to produce a configure script.
dnl 
dnl configure.in for mod_backhand - sasha@mathforum.com
dnl - partially based on mod_auth_mysql configure.in
dnl - supports DSO via axps on Linux

AC_INIT(mod_backhand.c)

AC_CONFIG_HEADER(config.h)

test -z "$CFLAGS" && auto_cflags=1

dnl Checks for programs.
dnl AC_PROG_INSTALL
AC_PROG_CC
AC_PROG_CC_C_O

dnl Checks for libraries.

dnl Checks for header files.
AC_HEADER_STDC
AC_CHECK_HEADERS(fcntl.h sys/ioctl.h sys/time.h unistd.h dlfcn.h)

dnl Checks for typedefs, structures, and compiler characteristics.
AC_C_CONST
AC_TYPE_PID_T
AC_TYPE_SIZE_T
AC_HEADER_TIME
AC_STRUCT_TM
AC_TYPE_UID_T

dnl Checks for library functions.
AC_PROG_GCC_TRADITIONAL
AC_FUNC_MEMCMP
AC_CHECK_FUNCS(gethostname gettimeofday regcomp select socket strdup strstr)
AC_CHECK_LIB(m, pow)
AC_CHECK_LIB(kstat, kstat_open)

dnl Checks for AF_UNIX nuances.
SUNLEN_SUNFAMILY_MSG="no"
AC_MSG_CHECKING(for sun_len and sun_family in sockaddr_un)
AC_TRY_LINK(
	[
#include <stdio.h>
#include <sys/types.h>  
#include <sys/socket.h>
#include <sys/un.h>
	],
	[ struct sockaddr_un unix_addr;
  sizeof(unix_addr.sun_len) + sizeof(unix_addr.sun_family);
	],
	[
		AC_MSG_RESULT(yes)
		AC_DEFINE(HAVE_SUNLEN)
		SUNLUN_SUNFAMILY_MSG="yes"
	],
	[
		AC_MSG_RESULT(no)
	]
)

dnl
dnl Check for Debian
dnl

AC_MSG_CHECKING(for Debian)
if test -f /etc/debian_version ; then
	AC_MSG_RESULT(yes)
	#AC_PROG_INSTALL(/usr/bin/install)
	INSTALL="/usr/bin/install"
	AC_SUBST(INSTALL)
	DEBIAN="yes"
	AC_SUBST(DEBIAN)
else
	AC_MSG_RESULT(no)
fi

dnl
dnl 	DSO support via apxs
dnl

AC_MSG_CHECKING(for Apache module support via DSO through APXS)
AC_ARG_WITH(apxs,
[  --with-apxs[=FILE]   Build shared Apache module.  FILE is the optional
                        pathname to the Apache apxs tool; defaults to
                        "apxs".  (This option needs Perl installed)],
[
    if test "$withval" = "yes"; then
        withval=apxs
    fi
    APXS_ON=1
    APXS="$withval"
    APXS_LDFLAGS=
    APXS_LIBS="\$(LIBS)"
    APACHE_INCLUDE="-I`$APXS -q INCLUDEDIR`"
    BINNAME=mod_backhand.so
    BUILD_IT="\$(APXS) \$(APXS_LDFLAGS) \$(APXS_LIBS) -o \$(BINNAME) -c \$(SOURCES)"
    AC_SUBST(APXS)
    AC_SUBST(APXS_LDFLAGS)
    AC_SUBST(APXS_LIBS)
    AC_SUBST(BUILD_IT)
    AC_SUBST(BINNAME)
    dnl	AC_DEFINE(HAVE_AP_COMPAT_H)
    AC_MSG_RESULT(yes)
],[
    AC_MSG_RESULT(no)
])


dnl
dnl 	DSO support via apaci
dnl

AC_MSG_CHECKING(for Apache module support via DSO through APACI)
AC_ARG_WITH(shared-apache,
[  --with-shared-apache[=DIR] Build shared Apache module.  DIR is the
			      top-level Apache build directory,
			      defaults to /usr/local/etc/httpd/.
			      (This option needs Perl installed)],
[
        if test "$withval" = "yes"; then
          # Apache's default directory
          withval=/usr/local/etc/httpd
        fi
        DSO_APACI_ON=1
        if test "$withval" != "no"; then
          if test -f $withval/src/include/httpd.h; then
            APACHE_INCLUDE="-I$withval/src/include -I$withval/src/os/unix"
            APACHE_TARGET=$withval/src/modules/backhand
                if test ! -d $APACHE_TARGET; then
                        mkdir $APACHE_TARGET
                fi
            BUILD_IT="mkdir -p $APACHE_TARGET; cp *.c *.h Makefile.tmpl Makefile.libdir $APACHE_TARGET"
            AC_MSG_RESULT(yes - Shared Apache 1.3.x)
          else
            AC_MSG_RESULT(no)
            AC_MSG_ERROR(Invalid Apache directory - unable to find httpd.h under
 $withval/src/include)
          fi
        fi
        AC_SUBST(APACHE_INCLUDE)
        AC_SUBST(APACHE_TARGET)
        AC_SUBST(BUILD_IT)
],[
        AC_MSG_RESULT(no)
])   


AC_SUBST(APACHE_INCLUDE)
AC_SUBST(APACHE_TARGET)
dnl AC_SUBST(BUILD_IT)

dnl If we're using gcc and the user hasn't specified CFLAGS, add -O2.
test -n "$auto_cflags" && test -n "$GCC" && CFLAGS="$CFLAGS -O2"

AC_SUBST(CFLAGS)
AC_SUBST(CFLAGS_SHLIB)
AC_SUBST(LDFLAGS_SHLIB)
AC_SUBST(LDFLAGS_SHLIB_EXPORT)

AC_OUTPUT(Makefile)
