dnl $Id: configure.in,v 1.9 1999/09/01 09:59:01 gstein Exp $
dnl Process this file with autoconf to produce a configure script.

AC_INIT(mod_auth_pgsql.c)	
AC_CONFIG_HEADER(config.h)

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

AC_PROG_CC
AC_PROG_RANLIB

dnl Ugly hack to get around a problem with gcc on AIX.
if test "$CC" = "gcc" -a "$ac_cv_prog_cc_g" = "yes" -a \
   "`uname -sv`" = "AIX 4"; then
   CFLAGS=`echo $CFLAGS | sed -e 's/-g//'`
fi

if test "`uname -sv`" = "AIX 4" -a "$GCC" != "yes"; then
     APXS_EXP="-bE:mod_auth_pgsql.exp"
fi
AC_SUBST(APXS_EXP)

dnl Hack to work around a Mac OS X Server cpp problem
dnl Known versions needing this workaround are 5.3 and 5.4
if test "$ac_cv_prog_gcc" = "yes" -a "`uname -s`" = "Rhapsody"; then
        CPPFLAGS="$CPPFLAGS -traditional-cpp"
fi

dnl
dnl Check for /usr/pkg/{lib,include} which is where NetBSD puts binary
dnl and source packages.  This should be harmless on other OSs.
dnl
if test -d /usr/pkg/include -a -d /usr/pkg/lib ; then
       CFLAGS="$CFLAGS -I/usr/pkg/include"
       LDFLAGS="$LDFLAGS -L/usr/pkg/lib"
fi

AC_HEADER_STDC

AC_MSG_CHECKING(for static Apache module support)
AC_ARG_WITH(apache,
[  --with-apache=DIR      Build static Apache module.  DIR is the path
                         to the top-level Apache source directory.],
[
	if test "$withval" = "yes"; then
		AC_MSG_ERROR(You need to specify a directory with --with-apache)
	fi
	if test -f $withval/src/include/httpd.h; then
		APACHE_BASE=$withval
		APACHE_INCLUDE="$withval/src/include";
		INCLUDE="$INCLUDE -I$withval/src/include -I$withval/src/os/unix"
		TARGET=$withval/src/modules/auth_pgsql
		if test ! -d $TARGET; then
			mkdir $TARGET
		fi	
		BINNAME=mod_auth_pgsql.a
		LIBS="$extra_l"
		INSTALL_IT="mkdir -p $TARGET; cp $BINNAME  mod_auth_pgsql.c mod_auth_pgsql.exp Makefile.tmpl Makefile.libdir mod_auth_pgsql.module auth_pgsql_shared_stub.c $extra_copy $TARGET"
		AC_MSG_RESULT(yes - Apache 1.3.x)
	fi
],[
    AC_MSG_RESULT(no)
])


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".],
[
    if test "$BINNAME" != ""; then
      AC_MSG_ERROR(--with-apache and --with-apxs are mutually exclusive)
    fi

    if test "$withval" = "yes"; then
      APXS=apxs
    else
      APXS="$withval"
    fi
])

if test "$BINNAME" = "" -a "$APXS" = ""; then
  for i in /usr/sbin /usr/local/apache/bin ; do
    if test -f "$i/apxs"; then
      APXS="$i/apxs"
    fi
  done
fi

if test -n "$APXS"; then
    BINNAME=mod_auth_pgsql.so
    INSTALL_IT="\$(APXS) -i -a -n auth_pgsql $BINNAME"

    APXS_CC="`$APXS -q CC`"
    APXS_INCLUDE="`$APXS -q INCLUDEDIR`"
    INCLUDE="$INCLUDE -I$APXS_INCLUDE"

    AC_SUBST(APXS)
    AC_SUBST(BINNAME)
    AC_SUBST(INSTALL_IT)

    AC_MSG_RESULT(found at $APXS)
else
    AC_MSG_RESULT(no)
fi

dnl If we did not find a way to build/install mod_auth_pgsql, then bail out.
if test "$BINNAME" = ""; then
	AC_MSG_ERROR(You must specify either --with-apxs or --with-apache)
fi

AC_MSG_CHECKING(for pgsql)
AC_ARG_WITH(pgsql,
[  --with-pgsql[=DIR]        Use this to specify expat install dir overriding
                             default search.],
[
  if test "$withval" != "no" ; then
    if test -f "$withval/include/libpq-fe.h" ; then
        PG_DIR=$withval
        PG_INCLUDE="$withval/include"
        PG_LIB="$withval/lib"
    fi
    if test "$PG_DIR" = "" ; then
      if test -f "$withval/include/libpq-fe.h" ; then
        XML_DIR=$withval
        XML_INCLUDE="$withval/include"
        XML_LIB="$withval/lib"
      fi
    fi
    if test "$PG_DIR" = "" ; then
      if test -f "$withval/lib/libpq.a" -a -f "$withval/include/libpq-fe.h" ; then
	XML_DIR=$withval
	XML_INCLUDE="$withval/include"
	XML_LIB=$withval/lib
      fi
    fi
    if test "$PG_DIR" = "" ; then
      AC_MSG_ERROR(cannot find PostgreSQL include files at specified location)
    fi
  fi
])

AC_MSG_CHECKING(for pgsql lib)
AC_ARG_WITH(pgsql-lib,
[  --with-pgsql-lib=DIR        Use this to specify expat install dir overriding
                             default search.],
[
  if test "$withval" != "no" ; then
      if test -f "$withval/libpq.a" ; then
		PG_LIB=$withval
      fi
  fi
])


AC_MSG_CHECKING(for pgsql include)
AC_ARG_WITH(pgsql-include,
[  --with-pgsql-include=DIR        Use this to specify expat install dir overriding
                             default search.],
[
  if test "$withval" != "no" ; then
      if test -f "$withval/libpq-fe.h" ; then
		PG_INCLUDE=$withval
      fi
  fi
])


dnl Try to figure out where PostgreSQL might be found if not specified using
dnl --with-pgsql. 
if test "$PG_DIR" = ""; then
	for i in /usr /usr/local /usr/local/postgres; do
		if test -f "$i/include/libpq-fe.h"; then
			PG_DIR=$i
			PG_INCLUDE="$i/include"
			PG_LIB="$i/lib"
		elif test -f "$i/include/pgsql/libpq-fe.h"; then
			PG_DIR=$i
			PG_INCLUDE="$i/include/pgsql"
			PG_LIB="$i/lib"
		fi
	done
fi

dnl Give up. We could not find PostgreSQL on the command line, or
dnl somewhere in the system.
if test "$PG_DIR" = "" -a "$PG_LIB" = "" -a "$PG_INCLUDE" = "" ; then
	AC_MSG_ERROR(cannot find postgreSQL include files - install postgreSQL or use --with-pgsql=<dir> or --with-pgsq-lib=<dir> --with-pgsql-include=<dir>)
fi

dnl the search process above should have set PG_LIB and PG_INCLUDE.
if test "$PG_LIB" != "" ; then
	LIBS="$LIBS -L$PG_LIB -lpq"
fi	

dnl set up the various includes -- direct and via APXS
INCLUDE="$INCLUDE -I$PG_INCLUDE"

AC_MSG_RESULT(found in $PG_DIR)


AC_SUBST(INCLUDE)
AC_SUBST(LIBS)

AC_OUTPUT(Makefile Makefile.tmpl mod_auth_pgsql.module)

if test -n "$APXS_CC" && test "$APXS_CC" != "$CC" ; then
  echo "=================================================================="
  echo "WARNING: You have chosen to compile mod_auth_pgsql with a different"
  echo "         compiler than the one used to compile Apache."
  echo ""
  echo "    Current compiler:      $CC"
  echo "   Apache's compiler:      $APXS_CC"
  echo ""
  echo "This could cause some problems."
  echo "=================================================================="
fi
