dnl Process this file with autoconf to produce a configure script.
AC_INIT(bin/rip/whois_rip.c)
AC_CONFIG_HEADER(include/config.h)

AC_CONFIG_SUBDIRS(utils/whoisRIP)
AC_PREFIX_DEFAULT(/usr/local/whoisd)

dnl Local RIP things.

PRODUCT=`cat PRODUCT | cut -f2 -d'=' | tr -d " "`
VERSION=`cat VERSION | cut -f2 -d'=' | tr -d " "`
RELEASE_TAG=`cat VERSION | cut -f2 -d'=' | tr -d " .\n"`
RIP=`pwd`
AC_DEFINE_UNQUOTED(VERSION, "$VERSION")
AC_SUBST(PRODUCT)
AC_SUBST(VERSION)
AC_SUBST(RELEASE_TAG)
AC_SUBST(RIP)

dnl
dnl installation directory layout
dnl
topdir=$prefix
bindir=${topdir}/bin
confdir=${topdir}/conf
scriptdir=${topdir}/scripts
utildir=${topdir}/utils
docdir=${topdir}/doc
vardir=${topdir}/var
tmpdir=${vardir}/tmp
lockdir=${vardir}/lock

libdir=${topdir}/lib/gnudl
mandir=${topdir}/man/man1

#######
# Log infrastructure
logdir=${topdir}/log
qrylog=${logdir}/qry
dbuperr=${logdir}/dbuperr
allriperr=${logdir}/riperr
ripaudit=${logdir}/audit
ripmirlog=${logdir}/mirror
ripsvrlog=${logdir}/svr
sqlog=${logdir}/sql
ripupdlog=${logdir}/ripupd
updlog=${logdir}/upd
acklog=${logdir}/ack
notiflog=${logdir}/notif
forwlog=${logdir}/forw
crosslog=${logdir}/cross

AC_SUBST(topdir)
AC_SUBST(bindir)
AC_SUBST(confdir)
AC_SUBST(scriptdir)
AC_SUBST(utildir)
AC_SUBST(docdir)
AC_SUBST(vardir)
AC_SUBST(tmpdir)
AC_SUBST(lockdir)

AC_SUBST(logdir)
AC_SUBST(qrylog)
AC_SUBST(dbuperr)
AC_SUBST(allriperr)
AC_SUBST(ripaudit)
AC_SUBST(ripmirlog)
AC_SUBST(ripsvrlog)
AC_SUBST(sqlog)
AC_SUBST(ripupdlog)
AC_SUBST(updlog)
AC_SUBST(acklog)
AC_SUBST(notiflog)
AC_SUBST(forwlog)
AC_SUBST(crosslog)

AC_SUBST(docdir)
AC_SUBST(libdir)
AC_SUBST(mandir)



dnl
dnl glib-config
dnl

AC_ARG_WITH(glibconfig, 
	[  --with-glibconfig=FILE     FILE=path of glib-config file[$ac_default_prefix/bin/glib-config]],
	if test [ x$withval != x ] -a [ -x $withval ]; then
	  glibconfig=$withval
	else
	  echo "Please specify a suitable glib-config file: --with-glibconfig=FILE"; exit 1
	fi ,
	glibconfig=$ac_default_prefix/bin/glib-config)

AC_CHECK_FILE($glibconfig, , 
	      echo "The file glib-config does not exist!";
	      echo "Please specify a suitable glib-config file: --with-glibconfig=FILE"; exit 1)

AC_SUBST(glibconfig)


dnl
dnl MySQL distribution directory
dnl

AC_ARG_WITH(mysql, 
	[  --with-mysql=DIR        DIR=path of MySQL distribution dir[$ac_default_prefix/mysql]],
	if test [ x$withval != x ] -a [ -d $withval ]; then
	  mysql=$withval
	else
	  echo "Please specify a suitable MySQL distribution directory: --with-mysql=DIR"; exit 1
	fi ,
	mysql=$ac_default_prefix/mysql)

AC_SUBST(mysql)


mysqlincdir=$mysql/include
mysqllibdir=$mysql/lib
mysqlbindir=$mysql/bin

dnl
dnl MySQL include directory
dnl

AC_ARG_WITH(mysqlinc, 
	[  --with-mysqlinc=DIR        DIR=path of MySQL include dir[$ac_default_prefix/mysql/include]],
	if test [ x$withval != x ] -a [ -d $withval ]; then
	  mysqlincdir=$withval
	else
	  echo "Please specify a suitable MySQL include directory: --with-mysqlinc=DIR"; exit 1
	fi ,
	mysqlincdir=$mysql/include)

AC_CHECK_FILE($mysqlincdir/mysql.h, , 
	      echo "MySQL include directory does not contain mysql.h !";
	      echo "Please specify a suitable MySQL include directory: --with-mysqlinc=DIR"; exit 1)

AC_SUBST(mysqlincdir)

dnl
dnl MySQL library directory
dnl

AC_ARG_WITH(mysqllib, 
	[  --with-mysqllib=DIR        DIR=path of MySQL library dir[$ac_default_prefix/mysql/lib]],
	if test [ x$withval != x ] -a [ -d $withval ]; then
	  mysqllibdir=$withval
	else
	  echo "Please specify a suitable MySQL library directory: --with-mysqllib=DIR"; exit 1
	fi ,
	mysqllibdir=$mysql/lib)

AC_CHECK_FILE($mysqllibdir/libmysqlclient_r.so, , 
	      echo "MySQL library directory does not contain libmysqlclient_r.so !";
	      echo "Please specify a suitable MySQL library directory: --with-mysqllib=DIR"; exit 1)

AC_SUBST(mysqllibdir)

dnl
dnl MySQL binary directory
dnl

AC_ARG_WITH(mysqlbin, 
	[  --with-mysqlbin=DIR        DIR=path of MySQL binary dir[$ac_default_prefix/mysql/bin]],
	if test [ x$withval != x ] -a [ -d $withval ]; then
	  mysqlbindir=$withval
	else
	  echo "Please specify a suitable MySQL binary directory: --with-mysqlbin=DIR"; exit 1
	fi ,
	mysqlbindir=$mysql/bin)

AC_CHECK_FILE($mysqlbindir/mysql, , 
	      echo "MySQL binary directory does not contain mysql !";
	      echo "Please specify a suitable MySQL binary directory: --with-mysqlbin=DIR"; exit 1)

AC_SUBST(mysqlbindir)

dnl
dnl xslt-config
dnl

AC_ARG_WITH(xsltconfig, 
	[  --with-xsltconfig=FILE     FILE=path of xsltconfig file[$ac_default_prefix/bin/xslt-config]],
	if test [ x$withval != x ] -a [ -x $withval ]; then
	  xsltconfig=$withval
	else
	  echo "Please specify a suitable xslt-config file: --with-xsltconfig=FILE"; exit 1
	fi ,
	xsltconfig=$ac_default_prefix/bin/xslt-config)

AC_CHECK_FILE($xsltconfig, , 
	      echo "The file xslt-config does not exist!";
	      echo "Please specify a suitable xslt-config file: --with-xsltconfig=FILE"; exit 1)

AC_SUBST(xsltconfig)
XSLTLIBS=`${xsltconfig} --libs`
XSLTCFLAGS=`${xsltconfig} --cflags`
AC_SUBST(XSLTLIBS)
AC_SUBST(XSLTCFLAGS)

dnl
dnl xml-config
dnl

AC_ARG_WITH(xmlconfig, 
	[  --with-xmlconfig=FILE     FILE=path of xml-config file[$ac_default_prefix/bin/xml2-config]],
	if test [ x$withval != x ] -a [ -x $withval ]; then
	  xmlconfig=$withval
	else
	  echo "Please specify a suitable xml-config file: --with-xmlconfig=FILE"; exit 1
	fi ,
	xmlconfig=$ac_default_prefix/bin/xml2-config)

AC_CHECK_FILE($xmlconfig, , 
	      echo "The file xml2-config does not exist!";
	      echo "Please specify a suitable xml2-config file: --with-xmlconfig=FILE"; exit 1)

AC_SUBST(xmlconfig)
XMLLIBS=`${xmlconfig} --libs`
XMLCFLAGS=`${xmlconfig} --cflags`
AC_SUBST(XMLLIBS)
AC_SUBST(XMLCFLAGS)

dnl
dnl c-client include directory (with c-client.a and various header files)
dnl

AC_ARG_WITH(cclientinc, 
	[  --with-cclientinc=DIR      DIR=path of c-client include dir[$ac_default_prefix/build/imap/c-client]],
	if test [ x$withval != x ] -a [ -d $withval ]; then
	  cclientinc=$withval
	else
	  echo "Please specify a suitable c-client include directory: --with-cclientinc=DIR"; exit 1
	fi ,
	cclientinc=$ac_default_prefix/build/imap/c-client)

AC_ARG_WITH(cclientlib, 
	[  --with-cclientlib=DIR      DIR=path of c-client library dir[$ac_default_prefix/build/imap/c-client]],
	if test [ x$withval != x ] -a [ -d $withval ]; then
	  cclientlib=$withval
	else
	  echo "Please specify a suitable c-client library directory: --with-cclientlib=DIR"; exit 1
	fi ,
	cclientlib=$ac_default_prefix/build/imap/c-client)

AC_CHECK_FILE($cclientinc/mail.h, , 
	      echo "c-client include directory does not contain c-client.h !";
	      echo "Please specify a suitable c-client include directory: --with-cclientinc=DIR"; exit 1)

AC_CHECK_FILE($cclientlib/c-client.a, , 
	      echo "c-client lib directory does not contain c-client.a !";
	      echo "Please specify a suitable c-client library directory: --with-cclientlib=DIR"; exit 1)

AC_SUBST(cclientinc)
AC_SUBST(cclientlib)



dnl
dnl GnuPG executable
dnl

AC_ARG_WITH(gpgcmd, 
	[  --with-gpgcmd=CMD      CMD=pathname of gpg executable [/usr/local/bin/gpg]],
	if test [ x$withval != x ] -a [ -x $withval ]; then
	  gpgcmd=$withval
	else
	  echo "Please specify a suitable gpg executable: --with-gpgcmd=CMD"; exit 1
	fi ,
	gpgcmd=/usr/local/bin/gpg)

AC_CHECK_FILE($gpgcmd, , 
	      echo "Cannot find gpg executable";
	      echo "Please specify a suitable gpg executable: --with-gpgcmd=CMD"; exit 1)

AC_SUBST(gpgcmd)

dnl End of local RIP things.

dnl Checks for programs.
AC_PROG_CC
AC_PROG_CXX
AC_PROG_INSTALL
AC_PROG_RANLIB
AC_PROG_LN_S
AC_PROG_MAKE_SET
AC_PATH_PROGS(M4, gm4 gnum4 m4, m4)
AC_PATH_PROGS(PERL5, perl5 perl, /usr/local/bin/perl5)

dnl Checks for header files.
AC_HEADER_STDC
AC_CHECK_HEADERS(unistd.h pthread.h sys/time.h sys/param.h)
AC_CHECK_HEADERS(typeinfo.h)

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

dnl Fancy check for uint32_t and uint64_t, which are defined in <stdint.h> 
dnl or <netinet/in.h> in Linux, but not in a header searched by AC_CHECK_TYPE
AC_TRY_COMPILE([
#include <inttypes.h>
], [
uint32_t xyzzy;
], 
, 
AC_CHECK_TYPE(uint32_t, u_int32_t)
)
AC_TRY_COMPILE([
#include <inttypes.h>
], [
uint64_t xyzzy;
], 
, 
AC_CHECK_TYPE(uint64_t, u_int64_t)
)


dnl Checks for library functions.
AC_TYPE_SIGNAL
AC_LIBRARY_NET
AC_CHECK_FUNCS(strstr strsep strtok_r)

dnl
dnl decipher pthread compilation model
dnl

dnl the old way doesn't work on BSD systems
dnl AC_CHECK_LIB(pthread,pthread_create)

dnl first, try linking a threaded application with "-pthread"
dnl which will work on FreeBSD (and OpenBSD) systems
save_LDFLAG="$LDFLAGS"
LDFLAGS="-pthread $LDFLAGS"
AC_TRY_LINK([
#include <pthread.h>
], [
pthread_self();
],
,
LDFLAGS="$save_LDFLAGS"
)

dnl then, try looking in various libraries, which will work on other systems
AC_SEARCH_LIBS(pthread_create, [ pthread pthreads thread threads ])

dnl check for pthread_attr_getstacksize, which doesn't exist on Linux
AC_SEARCH_LIBS(pthread_attr_getstacksize,[pthread pthreads thread threads])
AC_CHECK_FUNCS(pthread_attr_getstacksize pthread_attr_setstacksize)

dnl Checks for libraries (must occur after Pthread checks).
AC_CHECK_LIB(bind_r, gethostbyname_r)
AC_CHECK_LIB(pam, pam_start)

# some libraries we use
AC_SEARCH_LIBS(crypt, crypt)
AC_SEARCH_LIBS(compress, z)

# authentication support
AC_SEARCH_LIBS(MD5Init, md5)    # Solaris
AC_CHECK_FUNCS(MD5Init MD5Update MD5Final)
AC_SEARCH_LIBS(MD5_Init, ssl)   # Linux, FreeBSD
AC_CHECK_FUNCS(MD5_Init MD5_Update MD5_Final)
AC_CHECK_HEADER(openssl/md5.h, [ CFLAGS="$CFLAGS -I/usr/include/openssl" ])

AC_OUTPUT(Makefile.site Makefile utils/lnsafe utils/remadmin/remadmin.pl bin/load/make_db conf/rip.config.sample conf/rip.config.sample.q utils/make_mysql_user utils/whoisd.server utils/whoisd_start)
AC_OUTPUT_COMMANDS([echo changing file permissions for executables... utils/lnsafe])
AC_OUTPUT_COMMANDS([chmod +x utils/lnsafe])
AC_OUTPUT_COMMANDS([echo changing file permissions for executables... utils/remadmin/remadmin.pl])
AC_OUTPUT_COMMANDS([chmod +x utils/remadmin/remadmin.pl])
AC_OUTPUT_COMMANDS([echo changing file permissions for executables... bin/load/make_db])
AC_OUTPUT_COMMANDS([chmod +x bin/load/make_db])
AC_OUTPUT_COMMANDS([echo changing file permissions for executables... utils/make_mysql_user])
AC_OUTPUT_COMMANDS([chmod +x utils/make_mysql_user])
AC_OUTPUT_COMMANDS([echo changing file permissions for executables... utils/whoisd.server])
AC_OUTPUT_COMMANDS([chmod +x utils/whoisd.server])
AC_OUTPUT_COMMANDS([echo changing file permissions for executables... utils/whoisd_start])
AC_OUTPUT_COMMANDS([chmod +x utils/whoisd_start])

