dnl Process this file with autoconf to produce a configure script.
dnl
dnl Copyright (C) 2001, 2002 Free Software Foundation, Inc.
dnl Copyright (C) 1999, 2000 Juniper Networks, inc.
dnl Copyright (C) 1993, 1994, 1995, 1996, 1997 ??? (FIXME: see Changelog.v3)
dnl
dnl This file is part of GNU GNATS.
dnl 
dnl GNU GNATS is free software; you can redistribute it and/or modify
dnl it under the terms of the GNU General Public License as published by
dnl the Free Software Foundation; either version 2, or (at your option)
dnl any later version.
dnl 
dnl GNU GNATS is distributed in the hope that it will be useful,
dnl but WITHOUT ANY WARRANTY; without even the implied warranty of
dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
dnl GNU General Public License for more details.
dnl 
dnl You should have received a copy of the GNU General Public License
dnl along with GNU GNATS; see the file COPYING.  If not, write to the Free
dnl Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111, USA.
dnl
AC_PREREQ(2.5)
AC_INIT(pr.c)
AC_CONFIG_HEADER(autoconf.h)

AC_ARG_WITH(kerberos,
[  --with-kerberos         include code for Kerberos authentication])
AC_ARG_WITH(gnats-service,
[  --with-gnats-service=NAME
                          set the network service or port to NAME])
AC_ARG_WITH(gnats-user,
[  --with-gnats-user=NAME  set the username for GNATS from the passwd file
                          to NAME])
AC_ARG_WITH(gnatsd-user-access-file,
[  --with-gnatsd-user-access-file=PATH  set global GNATSD access file to PATH])
AC_ARG_WITH(gnatsd-host-access-file,
[  --with-gnatsd-host-access-file=PATH  set global GNATSD access file to PATH])
AC_ARG_WITH(gnats-dblist-file,
[  --with-gnats-dblist-file=PATH  specify file containing list of databases])
AC_ARG_WITH(gnats-default-db,
[  --with-gnats-default-db=PATH   specify the default database directory to use])

AC_SUBST(PROGS)dnl
AC_SUBST(MAN)dnl
AC_PROG_CC
AC_PROG_CPP
AC_PROG_INSTALL
AC_PROG_RANLIB
AC_PROG_YACC
AC_PROG_LEX
AC_PATH_PROGS(M4, gm4 gnum4 m4, m4)
#AC_DIFF_OPT
AC_AIX
AC_MINIX
AC_ISC_POSIX
AC_ALLOCA
# Some Solaris C compilers have const problems without a special ANSI option
AC_CACHE_CHECK([for really working const],gnats_really_working_const,[
AC_TRY_COMPILE([],
               [const char *foo(const char *x) { return x; }
                int bar(const char *x) { return x == foo(x); }],
               [gnats_really_working_const=yes],
               [gnats_really_working_const=no])
if test "$gnats_really_working_const" = no; then
  CFLAGS="$CFLAGS -Dconst= "
fi])
# Needed on SCO for something or other.
AC_CHECK_LIB(intl, main)
# If we can't find connect, try looking in -lsocket and -lnsl.  The
# Irix 5 libc.so has connect and gethostbyname, but Irix 5 also has
# libsocket.so which has a bad implementation of gethostbyname (it
# only looks in /etc/hosts), so we only look for -lsocket if we need
# it.
AC_CHECK_FUNC(connect, :, [
# Needed on SCO for syslog.
AC_CHECK_LIB(socket, main)
# Needed under Solaris in addition to -lsocket for the network.
AC_CHECK_LIB(nsl, main)
])
# Needed on ISC for syslog.
AC_CHECK_LIB(inet, main)
# Needed under IRIX for a usable malloc.
AC_CHECK_LIB(malloc, main)
# Needed under Unixware 2.0 for syslog.
AC_CHECK_LIB(gen, main)
# Needed generally for encrypted passwords.
AC_CHECK_LIB(crypt, crypt)

AC_STDC_HEADERS
AC_CHECK_HEADERS(crypt.h fcntl.h libgen.h limits.h memory.h netdb.h string.h syslog.h unistd.h machine/endian.h sys/select.h)
AC_EGREP_HEADER(\<unsetenv\>, stdlib.h,
  AC_DEFINE(HAVE_DECL_UNSETENV,1,[Whether unsetenv is present in headers.]),
  AC_DEFINE(HAVE_DECL_UNSETENV,0,[Whether unsetenv is present in headers.]))
AC_CHECK_FUNCS(ftime mkdir mktemp mkstemp unsetenv)
AC_HEADER_DIRENT
AC_SIZE_T

AM_PATH_LISPDIR

# Linux thang.
AC_TRY_CPP([#include <sys/socket.h>],
  AC_EGREP_CPP(socklen_t,
	       [#include <sys/socket.h>],
               AC_DEFINE(HAVE_SOCKLEN_T)))

# ***** How much do we want? *****

AC_SUBST(GNATS_ALL)dnl
AC_SUBST(GNATS_INSTALL)dnl

GNATS_ALL=all-gnats
GNATS_INSTALL=install-gnats

# See if we want Kerberos.
AC_MSG_CHECKING(for --with-kerberos)
if test -n "$with_kerberos" ; then
  AC_MSG_RESULT(yes)
  with_krb=true
else
  AC_MSG_RESULT(no)
fi

if test -n "$with_krb"; then
dnl
dnl set $(KRB4) from --with-krb4=value -- WITH_KRB4
dnl
AC_MSG_CHECKING(Kerberos path)
AC_ARG_WITH([krb4],
[  --with-krb4		  support Kerberos 4],
KRB4=$withval,
# Default to system version if BSD-style, else /usr/kerberos always.
if test ! -d /usr/kerberos && test -d /usr/include/kerberosIV ; then
  KRB4=/usr
else
  KRB4=/usr/kerberos
fi
)dnl
AC_MSG_RESULT($KRB4)
AC_SUBST(KRB4)

krb_h=
KRBINCLUDE=
# XXX this should use a different approach at some point
AC_MSG_CHECKING(for krb.h)
AC_TRY_LINK([#include <des.h>
#include <krb.h>],[int i;],
  [krb_h=yes krb_incdir=
   AC_MSG_RESULT(yes)],
  [AC_MSG_RESULT(no)
   if test -r "$KRB4/include/kerberosIV/krb.h" ; then
     krbincdir=$KRB4/include/kerberosIV
   else
     krbincdir=$KRB4/include
   fi
   if test "$cross_compiling" = "no" && test -r $krbincdir/krb.h; then
     hold_cflags=$CFLAGS
     CFLAGS="$CFLAGS -I$krbincdir"
     AC_MSG_CHECKING(for krb.h in $krbincdir)
     AC_TRY_LINK([#include <des.h>
#include <krb.h>],[int i;],
	[krb_h=yes krb_incdir=$krbincdir
	 AC_MSG_RESULT(yes)],
	[AC_MSG_RESULT(no)])
     CFLAGS=$hold_cflags
     if test -n "$krb_incdir"; then
       KRBINCLUDE="-I$krb_incdir"
     fi
   fi])
if test -n "$krb_h"; then
  krb_lib=
  if test "$cross_compiling" = "no" && test -r $KRB4/lib/libkrb.a; then
	# Fake some sensible log messages.
        AC_MSG_CHECKING(for -lkrb in $KRB4/lib)
        AC_MSG_RESULT(yes)
        krb_lib=-lkrb krb_libdir=$KRB4/lib
  else
	AC_CHECK_LIB(krb,main,krb_lib=-lkrb,
		     AC_CHECK_LIB(krb4,main,krb_lib=-lkrb4))
  fi
  if test x"$krb_lib" != x""; then
    AC_DEFINE(HAVE_KERBEROS)
    test -n "${krb_libdir}" && LIBS="${LIBS} -L${krb_libdir}"
    LIBS="${LIBS} $krb_lib"
    AC_CHECK_LIB(des,main,[LIBS="${LIBS} -ldes"])
    AC_CHECK_LIB(des425,main,[LIBS="${LIBS} -ldes425"])
    AC_CHECK_LIB(krb5,main,[LIBS="${LIBS} -lkrb5"])
    AC_CHECK_LIB(crypto,main,[LIBS="${LIBS} -lcrypto"])
    AC_CHECK_LIB(com_err,main,[LIBS="${LIBS} -lcom_err"])
    AC_CHECK_FUNC(krb_mk_auth, :,
      # BSD Kerberos doesn't have these functions.
      EXTRA_OBJS="mk_auth.o $EXTRA_OBJS"
    )
  fi
fi
AC_CHECK_FUNCS(krb_get_err_text)
AC_SUBST(HAVE_KERBEROS)
AC_SUBST(KRBINCLUDE)
AC_SUBST(EXTRA_OBJS)
fi

if test "x$GCC" = "xyes" ; then
	GCC_CFLAGS="-W -Wall -ansi -pedantic  -Wstrict-prototypes -Wmissing-prototypes -Wwrite-strings -Wno-format"
fi

# ***** Guess the configuration *****

# echo guessing optimal GNATS configuration

AC_SUBST(GNATS_SERVICE)dnl
AC_SUBST(GNATS_USER)dnl
AC_SUBST(DEFAULT_RELEASE)dnl
AC_SUBST(DEFAULT_ORGANIZATION)dnl
AC_SUBST(SUBMITTER)dnl
AC_SUBST(DEFAULT_MAIL_AGENT)dnl
AC_SUBST(BWEEK_START)dnl
AC_SUBST(BWEEK_END)dnl
AC_SUBST(BDAY_START)dnl
AC_SUBST(BDAY_END)dnl
AC_SUBST(GNATSD_USER_ACCESS_FILE)dnl
AC_SUBST(GNATSD_HOST_ACCESS_FILE)dnl
AC_SUBST(GLOBAL_DB_LIST_FILE)dnl
AC_SUBST(GNATS_DEFAULT_DB_DIR)dnl

AC_SUBST(GCC_CFLAGS)dnl

# And GNATS_SERVICE
AC_MSG_CHECKING(for --with-gnats-service)
if test -n "$with_gnats_service"; then
	AC_MSG_RESULT($with_gnats_service)
	GNATS_SERVICE=$with_gnats_service
fi
if test -z "${GNATS_SERVICE}"; then
	AC_MSG_RESULT(default to support)
	GNATS_SERVICE=support
fi

# And GNATS_USER
AC_MSG_CHECKING(for --with-gnats-user)
if test -n "$with_gnats_user"; then
	AC_MSG_RESULT($with_gnats_user)
	GNATS_USER=$with_gnats_user
fi
if test -z "${GNATS_USER}"; then
	AC_MSG_RESULT(default to gnats)
	GNATS_USER=gnats
fi

AC_MSG_CHECKING(for --with-gnatsd-user-access-file)
if test -n "${with_gnatsd_user_access_file}"; then
	AC_MSG_RESULT($with_gnatsd_user_access_file)
	GNATSD_USER_ACCESS_FILE=$with_gnatsd_user_access_file
fi

if test -z "${GNATSD_USER_ACCESS_FILE}"; then
	AC_MSG_RESULT(default to ${sysconfdir}/gnats/gnatsd.user_access)
	GNATSD_USER_ACCESS_FILE=${sysconfdir}/gnats/gnatsd.user_access
fi

AC_MSG_CHECKING(for --with-gnatsd-host-access-file)
if test -n "${with_gnatsd_host_access_file}"; then
	AC_MSG_RESULT($with_gnatsd_host_access_file)
	GNATSD_HOST_ACCESS_FILE=$with_gnatsd_host_access_file
fi

if test -z "${GNATSD_HOST_ACCESS_FILE}"; then
	AC_MSG_RESULT(default to ${sysconfdir}/gnats/gnatsd.host_access)
	GNATSD_HOST_ACCESS_FILE=${sysconfdir}/gnats/gnatsd.host_access
fi

AC_MSG_CHECKING(for --with-gnats-dblist-file)
if test -n "${with_gnats_dblist_file}"; then
	AC_MSG_RESULT($with_gnats_dblist_file)
	GLOBAL_DB_LIST_FILE=$with_gnats_dblist_file
fi

if test -z "${GLOBAL_DB_LIST_FILE}"; then
	AC_MSG_RESULT(default to ${sysconfdir}/gnats/databases)
	GLOBAL_DB_LIST_FILE=${sysconfdir}/gnats/databases
fi

AC_MSG_CHECKING(for --with-gnats-default-db)
if test -n "${with_gnats_default_db}"; then
	AC_MSG_RESULT($with_gnats_default_db)
	GNATS_DEFAULT_DB_DIR="$with_gnats_default_db"
fi

if test -z "${GNATS_DEFAULT_DB_DIR}"; then
	AC_MSG_RESULT(default to ${sharedstatedir}/gnatsdb)
	GNATS_DEFAULT_DB_DIR=${sharedstatedir}/gnatsdb
fi

# Set up default values to be overridden

_h=`(hostname || uname -n) 2>/dev/null | sed 1q`

DEFAULT_RELEASE=unknown-1.0
DEFAULT_ORGANIZATION=unknown
SUBMITTER=unknown
DEFAULT_MAIL_AGENT=false
BDAY_START=8
BDAY_END=17
BWEEK_START=1
BWEEK_END=5

AC_PATH_PROG(SENDMAIL,sendmail,[],$PATH:/usr/lib:/usr/sbin:/usr/ucblib)
if test -n "$SENDMAIL" ; then
  DEFAULT_MAIL_AGENT="$SENDMAIL -oi -t"
else
  echo "configure: error: sendmail binary not available"
  exit 1
fi

# If the config file exists, it may have more recent info than send-pr

if test -n "$verbose"; then
	echo "	setting GNATS_SERVICE to $GNATS_SERVICE"
	echo "	setting GNATS_USER to $GNATS_USER"
	echo "	setting SUBMITTER to $SUBMITTER"
	echo "	setting DEFAULT_RELEASE to $DEFAULT_RELEASE"
	echo "	setting DEFAULT_ORGANIZATION to $DEFAULT_ORGANIZATION"
	echo "	setting BDAY_START to $BDAY_START"
	echo "	setting BDAY_END to $BDAY_END"
	echo "	setting BWEEK_START to $BWEEK_START"
	echo "	setting BWEEK_END to $BWEEK_END"
	echo "	setting DEFAULT_MAIL_AGENT to $DEFAULT_MAIL_AGENT"
fi

# ***** End of configuration section *****

AC_OUTPUT(Makefile)
