dnl Process this file with autoconf to produce a configure script.

dnl check for one of the source files
AC_INIT(refdb, 1.0.2)
AC_CONFIG_SRCDIR(src/backend-ris.c)

dnl Find out host type
AC_CONFIG_AUX_DIR(./conf)
AC_CANONICAL_HOST

dnl Use automake (shut off warnings about GNU make)
AM_INIT_AUTOMAKE([-Wno-portability])

dnl switch on GNU extensions
AC_DEFINE(_GNU_SOURCE)

dnl see which compiler is available
AC_PROG_CC
AC_PROG_INSTALL
AC_PROG_CPP

dnl set some compile options (just a stub right now)
if test -n "$GCC"; then
   case $host in
   *)
	CFLAGS=$CFLAGS" -O2";;
   esac
fi

dnl see where the libdbi lib is
AC_ARG_WITH(libdbi-lib,
	[  --with-libdbi-lib=DIR         Find libdbi lib in DIR],
	[CFLAGS=$CFLAGS" -L$withval"])

dnl see where expat lib is
AC_ARG_WITH(expat-lib,
	[  --with-expat-lib=DIR          Find expat lib in DIR],
	[CFLAGS=$CFLAGS" -L$withval"])

dnl see where btparse lib is
AC_ARG_WITH(btparse-lib,
	[  --with-btparse-lib=DIR        Find btparse lib in DIR],
	[CFLAGS=$CFLAGS" -L$withval"])

dnl need a sgml declaration
AC_ARG_WITH(sgml-declaration,
	[  --with-sgml-declaration=PATH  Use this file as SGML declaration],
	[sgml_decl=$withval],
	[sgml_decl="\$(pkgdatadir)/declarations/docbook.dcl"])

dnl need a xml declaration
AC_ARG_WITH(xml-declaration,
	[  --with-xml-declaration=PATH   Use this file as XML declaration],
	[xml_decl=$withval],
	[xml_decl="\$(pkgdatadir)/declarations/xml.dcl"])

dnl need the path to the docbook xsl stylesheets
AC_ARG_WITH(docbook-xsl,
	[  --with-docbook-xsl=PATH       Path to the DocBook XSL stylesheet root],
	[docbk_xsl=$withval],
	[docbk_xsl="http://docbook.sourceforge.net/release/xsl/current"])

dnl need the path to the docbook namespaced xsl stylesheets
AC_ARG_WITH(docbook-xsl-ns,
	[  --with-docbook-xsl-ns=PATH     Path to the DocBook XSL NS stylesheet root],
	[docbk_xsl_ns=$withval],
	[docbk_xsl_ns="http://docbook.sourceforge.net/release/xsl-ns/current"])

dnl need the path to the tei P4 xsl stylesheets
AC_ARG_WITH(tei-xsl,
	[  --with-tei-xsl=PATH           Path to the TEI XSL stylesheet root],
	[tei_xsl=$withval],
	[tei_xsl="http://www.tei-c.org/Stylesheets"])

dnl need the path to the tei P5 xsl stylesheets
AC_ARG_WITH(tei-xsl-ns,
	[  --with-tei-xsl-ns=PATH        Path to the TEI XSL NS stylesheet root],
	[tei_xsl_ns=$withval],
	[tei_xsl_ns="http://www.tei-c.org/Stylesheets"])

dnl need the path to the jars
AC_ARG_WITH(classpath_root,
	[  --with-classpath-root=PATH    Path to a Java class repository for XSL processors],
	[classpath_root=$withval],
	[classpath_root=""])

dnl need a path to write a pid file
AC_ARG_WITH(var_dir,
	[  --with-var-dir=PATH           Path of a directory for PID files],
	[var_dir=$withval],
	[var_dir="\$(localstatedir)/run"])

dnl need a path to write log files
AC_ARG_WITH(log_dir,
	[  --with-log-dir=PATH           Path of a directory for log files],
	[log_dir=$withval],
	[log_dir="\$(localstatedir)/log"])

dnl the main database
AC_ARG_WITH(main_db,
	[  --with-main-db=name           The name of the RefDB system database],
	[main_db=$withval],
	[main_db="refdb"])

dnl which directory will hold the databases? This does not only pertain
dnl to SQLite, as the directory also holds the version file
AC_ARG_WITH(db_dir,
	[  --with-db-dir=PATH            Directory for database files],
	[db_dir=$withval],
	[db_dir="\$(localstatedir)/lib/refdb/db"],
])

dnl maintainers may need the trang jar
AC_ARG_WITH(trang_jar,
	[  --with-trang-jar=PATH         specify the full path to trang],
	[trang_cmd="java -jar "$withval],
	[trang_cmd="cp"])

dnl see whether we should build the docs
ac_docs="YES"
AC_ARG_ENABLE(docs,
	[  --disable-docs		  do not build and install the documentation.],
	[  ac_docs="NO" ])

if test "$ac_docs" = "YES"; then
docs_subdirs="doc"
else
docs_subdirs=""
fi

dnl see whether we should build the man pages
ac_manpages="YES"
AC_ARG_ENABLE(manpages,
	[  --disable-manpages		  do not build and install the man pages.],
	[  ac_manpages="NO" ])

if test "$ac_manpages" = "YES"; then
man_subdirs="man"
else
man_subdirs=""
fi

dnl see whether we should build the server
ac_server="YES"
AC_ARG_ENABLE(server,
	[  --disable-server		  Exclude server.],
	[  ac_server="NO" ])

if test "$ac_server" = "YES"; then
server_subdirs="etc scripts src phpweb sru"
server_bins="refdbd\$(EXEEXT)"
server_scripts="refdbctl refdb-init refdbsru refdb-sruserver"
server_etc_objects="refdbdrc.mysql.example refdbdrc.pgsql.example refdbdrc.sqlite.example refdbdrc.sqlite3.example refdbsrurc.example"
server_etc_distobjects="refdbdrc refdbsrurc"
server_mans="refdb.8 refdbd.1 refdb-init.8 refdbctl.1 refdbsru.1 refdb-sruserver.1"
server8_mans="refdb.sh.8"
else
server_subdirs=""
server_bins=""
server_scripts=""
server_etc_objects=""
server_etc_distobjects=""
server_mans=""
server8_mans=""
fi

dnl see whether we should build the clients
ac_client="YES"
AC_ARG_ENABLE(clients,
	[  --disable-clients		  Exclude clients.],
	[  ac_client="NO" ])

if test "$ac_client" = "YES" && test "$ac_server" = "YES"; then
client_subdirs="declarations dsssl dtd examples styles xsl"
client_bins="refdbib\$(EXEEXT) refdbc\$(EXEEXT) refdba\$(EXEEXT) refdbxp\$(EXEEXT) eenc\$(EXEEXT)"
client_scripts="refdbjade runbib runbib-missing db2ris refdb-bug refdbxml refdbrtf marc2ris med2ris en2ris refdbnd refdb_dos2unix refdb-ms refdb-backup refdb-restore refdb_latex2utf8txt bib2ris-utf8 rtfcitations refdb-pubmed"
client_etc_objects="bib2risrc.example bib2riscgirc.example db2risrc.example refdbarc.example refdbcrc.example refdbcgirc.example refdbibrc.example med2risrc.example med2riscgirc.example marc2risrc.example refdbxmlrc.example refdbrtfrc.example refdbjaderc.example runbibrc.example"
client_etc_distobjects="bib2risrc bib2riscgirc db2risrc refdbarc refdbcrc refdbcgirc refdbibrc med2risrc med2riscgirc marc2risrc refdbxmlrc refdbrtfrc refdbjaderc runbibrc"
client_mans="RefDB.7 refdba.1 refdbc.1 refdbib.1 bib2ris.1 db2ris.1 en2ris.1 marc2ris.1 med2ris.1 eenc.1 refdb-backup.1 refdb-ms.1 refdb-restore.1 refdbjade.1 refdbnd.1 refdbxml.1 refdbrtf.1 runbib.1 runbib-missing.1 refdbxp.1 refdb-bug.1 refdb_dos2unix.1 refdb_latex2utf8txt.1 rtfcitations.1 refdb-pubmed.1"
client1_mans="bib2ris-utf8.1"
elif test "$ac_client" = "YES"; then
client_subdirs="declarations dsssl dtd etc examples scripts src styles xsl"
client_bins="refdbib\$(EXEEXT) refdbc\$(EXEEXT) refdba\$(EXEEXT) refdbxp\$(EXEEXT) eenc\$(EXEEXT)"
client_scripts="refdbjade runbib runbib-missing db2ris refdb-bug refdbxml refdbrtf marc2ris med2ris en2ris refdbnd refdb_dos2unix refdb-ms refdb-backup refdb-restore refdb_latex2utf8txt bib2ris-utf8 rtfcitations refdb-pubmed"
client_etc_objects="bib2risrc.example bib2riscgirc.example db2risrc.example refdbarc.example refdbcrc.example refdbcgirc.example refdbibrc.example med2risrc.example med2riscgirc.example marc2risrc.example refdbxmlrc.example refdbrtfrc.example refdbjaderc.example runbibrc.example"
client_etc_distobjects="bib2risrc bib2riscgirc db2risrc refdbarc refdbcrc refdbcgirc refdbibrc med2risrc med2riscgirc marc2risrc refdbxmlrc refdbrtfrc refdbjaderc runbibrc"
client_mans="RefDB.7 refdba.1 refdbc.1 refdbib.1 bib2ris.1 db2ris.1 en2ris.1 marc2ris.1 med2ris.1 eenc.1 refdb-backup.1 refdb-ms.1 refdb-restore.1 refdbjade.1 refdbnd.1 refdbxml.1 refdbrtf.1 runbib.1 runbib-missing.1 refdbxp.1 refdb-bug.1 refdb_dos2unix.1 refdb_latex2utf8txt.1 rtfcitations.1 refdb-pubmed.1"
client1_mans="bib2ris-utf8.1"
else
client_subdirs=""
client_bins=""
client_scripts=""
client_etc_objects=""
client_etc_distobjects=""
client_mans=""
client1_mans=""
fi

dnl Checks for programs.
AC_PATH_PROGS(myshell, bash sh)

if test "$ac_client" = "YES"; then
AC_PATH_PROGS(myjade, openjade jade)
AC_PATH_PROG(myperl, perl)
AC_PATH_PROG(myxsltproc, xsltproc)
AC_PATH_PROG(myxmlcatalog, xmlcatalog)
fi

if test "$ac_docs" = "YES"; then
AC_PATH_PROG(mydtdparse, dtdparse)
AC_PATH_PROG(mydtdformat, dtdformat)
AC_PATH_PROG(mysgrep, sgrep)
AC_PATH_PROG(mytidy, tidy)
AC_PATH_PROG(mybatik, batik-rasterizer)
fi

dnl see whether subversion is installed
AC_PATH_PROG(svnversioncommand, svnversion)

dnl use svnversion to record the current repository revision only if
dnl subversion is installed and we are in a working copy
if test "X$svnversioncommand" = "X" || test `$svnversioncommand -n '.'` = "exported"; then
mysvnversion="cat svn_dist_version"
else
mysvnversion="svnversion -n '.'"
fi

dnl this is for building docs only, but the conditional must not be defined
dnl inside of an if block
AM_CONDITIONAL(HAVEBATIK, test x$mybatik != "x")

AM_CONDITIONAL(HAVESERVER, test "$ac_server" = "YES")

dnl this is used to make sure we create the file svn_dist_version only if 
dnl we have svnversion and if we are in a svn working copy. In all other
dnl cases the distributed copy of svn_dist_version should be used
AM_CONDITIONAL(HAVESVNWC, test "X$svnversioncommand" != "X" && test `$svnversioncommand -n '.'` != "exported")

dnl
dnl Checks for libraries.
dnl

if test "$ac_client" = "YES"; then
dnl libtermcap; libreadline needs either libtermcap or lib(n)curses, so
dnl we have to check this first
AC_CHECK_LIB(termcap, tputs, [
rltestlib="-ltermcap"], 
[AC_CHECK_LIB(ncurses, tputs, [
rltestlib="-lncurses"], 
[AC_CHECK_LIB(curses, tputs, [
rltestlib="-lcurses"], 
[echo "Cannot build refdb without either libtermcap, libncurses, or libcurses"
exit 1])])])

dnl libreadline
AC_CHECK_LIB(readline, readline, [
refdba_LDADD=$refdba_LDADD" -lreadline "$rltestlib
refdbc_LDADD=$refdbc_LDADD" -lreadline "$rltestlib], [
echo "Cannot build refdb without libreadline"
exit 1], $rltestlib)

AC_MSG_CHECKING(for libreadline version)

AC_TRY_RUN(
#include <stdio.h>
#include <readline/readline.h>
char* rl_readline_name;
int main(void) {
exit(0);
}
, ac_cv_lib_readline=old, ac_cv_lib_readline=new, ac_cv_lib_readline=old)

AC_MSG_RESULT($ac_cv_lib_readline)
if test "$ac_cv_lib_readline" = "old" ; then
  AC_DEFINE(READLINE41)
else
  AC_DEFINE(READLINE42)
fi
fi dnl end if build clients

if test "$ac_server" = "YES"; then

dnl see how to get iconv support
AM_ICONV_LINK

dnl refdbd uses log() which requires a math library on some systems
AC_SEARCH_LIBS_VAR(log, m,,,,refdbd_LDADD)

dnl linking with libdl is necessary on some systems
AC_SEARCH_LIBS_VAR(dlopen, dl,,,,refdbd_LDADD)

dnl linking with libltdl may be required by libdbi
AC_SEARCH_LIBS_VAR(lt_dlopen, ltdl,,,,refdbd_LDADD)

dnl libdbi - depends on libdl on some systems
AC_CHECK_LIB(dbi, dbi_initialize, [
refdbd_LDADD=$refdbd_LDADD" -ldbi"], [
echo "Cannot build refdb without libdbi"
exit 1])

dnl compress is in glibc, but not in Cygwin newlib
AC_SEARCH_LIBS_VAR(compress, z, , , , refdbd_LDADD)

fi dnl end if build server

dnl linking with libsocket may be necessary (Solaris?)
AC_SEARCH_LIBS(setsockopt, socket,,)

dnl in some cases Solaris seems to need libnsl too
AC_SEARCH_LIBS(gethostbyname, nsl,,)

dnl libexpat
if test "$ac_server" = "YES"; then
AC_CHECK_LIB(expat, XML_ParserCreate, [
refdbd_LDADD=$refdbd_LDADD" -lexpat"], [
echo "Cannot build refdb without libexpat"
exit 1])
fi

dnl btparse
if test "$ac_client" = "YES"; then
AC_CHECK_LIB(btparse, bt_initialize, [
bib2ris_LDADD=$bib2ris_LDADD" -lbtparse"
BIB2RIS="bib2ris\$(EXEEXT)"], [
echo "libbtparse not found; bib2ris import filter will not be built"])
fi

dnl check types that aren't really portable
AC_CHECK_TYPES(socklen_t)

dnl this is the dusty corner of configure and attempts to tackle the
dnl following issues:
dnl (1) printf uses non-standard conversion specifiers on some platforms
dnl (2) ps uses different arguments on BSD/SysV style systems to list
dnl     all running processes. Some ps'es accept both styles which makes
dnl     the list a little less ugly
case $host in
    *-sun-solaris*)
        ULLSPEC="%llu"
	psarg="-e" ;;
    *)
        ULLSPEC="%llu"
	psarg="ax" ;;
esac

AC_SUBST(refdba_LDADD)
AC_SUBST(refdbc_LDADD)
AC_SUBST(refdbd_LDADD)
AC_SUBST(bib2ris_LDADD)
AC_SUBST(BIB2RIS)
AC_SUBST(myjade)
AC_SUBST(myshell)
AC_SUBST(myxsltproc)
AC_SUBST(mysvnversion)
AC_SUBST(sgml_decl)
AC_SUBST(xml_decl)
AC_SUBST(docbk_xsl)
AC_SUBST(docbk_xsl_ns)
AC_SUBST(tei_xsl)
AC_SUBST(tei_xsl_ns)
AC_SUBST(classpath_root)
AC_SUBST(log_dir)
AC_SUBST(var_dir)
AC_SUBST(main_db)
AC_SUBST(db_dir)
AC_SUBST(ULLSPEC)
AC_SUBST(psarg)
AC_SUBST(trang_cmd)
AC_SUBST(server_subdirs)
AC_SUBST(client_subdirs)
AC_SUBST(docs_subdirs)
AC_SUBST(man_subdirs)
AC_SUBST(server_bins)
AC_SUBST(client_bins)
AC_SUBST(server_scripts)
AC_SUBST(client_scripts)
AC_SUBST(server_etc_objects)
AC_SUBST(client_etc_objects)
AC_SUBST(server_etc_distobjects)
AC_SUBST(client_etc_distobjects)
AC_SUBST(server_mans)
AC_SUBST(server8_mans)
AC_SUBST(client_mans)
AC_SUBST(client1_mans)

dnl Checks for header files.
AC_HEADER_STDC
AC_CHECK_HEADERS(fcntl.h limits.h sys/file.h sys/time.h sys/ioctl.h syslog.h unistd.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

dnl Checks for library functions.
AC_TYPE_SIGNAL
AC_FUNC_STRFTIME
AC_FUNC_MKFIFO
AC_CHECK_FUNCS(gethostname select socket strcspn strstr strtoll atoll)

dnl these checks are required only for clients
if test "$ac_client" = "YES"; then

dnl Check for XSL stylesheets
AC_CHECK_FILE($docbk_xsl/fo/docbook.xsl,
[echo "DocBook XSL stylesheets found"],
[if test -n "$myxmlcatalog"; then
REFDB_XML_FILE($myxmlcatalog,${docbk_xsl}/fo/docbook.xsl)
else
echo "cannot locate DocBook XSL stylesheets via XML catalog"
fi])

dnl Check for XSL NS stylesheets
AC_CHECK_FILE($docbk_xsl_ns/fo/docbook.xsl,
[echo "DocBook XSL NS stylesheets found"],
[if test -n "$myxmlcatalog"; then
REFDB_XML_FILE($myxmlcatalog,${docbk_xsl_ns}/fo/docbook.xsl)
else
echo "cannot locate DocBook XSL NS stylesheets via XML catalog"
fi])

AC_CHECK_FILE($tei_xsl/p4/fo/tei.xsl,
[echo "TEI XSL stylesheets found"],
[if test -n "$myxmlcatalog"; then
REFDB_XML_FILE($myxmlcatalog,${tei_xsl}/p4/fo/tei.xsl)
else
echo "cannot locate TEI XSL stylesheets via XML catalog"
fi])

AC_CHECK_FILE($tei_xsl_ns/p5/fo/tei.xsl,
[echo "TEI XSL NS stylesheets found"],
[if test -n "$myxmlcatalog"; then
REFDB_XML_FILE($myxmlcatalog,${tei_xsl_ns}/p5/fo/tei.xsl)
else
echo "cannot locate TEI XSL NS stylesheets via XML catalog"
fi])

dnl Check for Perl version and Perl modules
changequote(<<,>>)dnl
refdb_perl_command='print $]'
changequote([,])dnl
REFDB_PERL_VERSION($myperl, 5.006)
REFDB_PERL_MODULE($myperl, Text::Iconv, refdb_cv_iconv)
REFDB_PERL_MODULE($myperl, RefDB::Makestyle, refdb_cv_makestyle)
REFDB_PERL_MODULE($myperl, RefDB::Prefs, refdb_cv_prefs)
REFDB_PERL_MODULE($myperl, RefDB::SRU, refdb_cv_sru)
REFDB_PERL_MODULE($myperl, RefDB::SRUserver, refdb_cv_sruserver)
REFDB_PERL_MODULE($myperl, XML::Parser, refdb_cv_parser)
REFDB_PERL_MODULE($myperl, MARC::Record, refdb_cv_record)
REFDB_PERL_MODULE($myperl, MARC::Charset, refdb_cv_charset)
REFDB_PERL_MODULE($myperl, CGI, refdb_cv_cgi)

fi dnl end if build clients

AC_OUTPUT(src/Makefile scripts/Makefile etc/Makefile doc/Makefile dtd/Makefile declarations/Makefile examples/Makefile styles/Makefile phpweb/Makefile phpweb/css/Makefile phpweb/images/Makefile phpweb/includes/Makefile phpweb/xsl/Makefile sru/Makefile dsssl/Makefile dsssl/refdb/Makefile dsssl/refdb/html/Makefile dsssl/refdb/print/Makefile dsssl/refdb/lib/Makefile xsl/Makefile xsl/refdb/Makefile xsl/refdb/common-refdb/Makefile xsl/refdb/docbk-refdb-xsl/Makefile xsl/refdb/docbk-refdb-xsl/docbk-fo/Makefile xsl/refdb/docbk-refdb-xsl/docbk-html/Makefile xsl/refdb/docbk-refdb-xsl/docbk-xhtml/Makefile xsl/refdb/docbk-refdb-xsl/docbk-lib/Makefile xsl/refdb/tei-refdb-xsl/Makefile xsl/refdb/tei-refdb-xsl/tei-fo/Makefile xsl/refdb/tei-refdb-xsl/tei-html/Makefile xsl/refdb/tei-refdb-xsl/tei-lib/Makefile man/Makefile Makefile)

echo

if test "$ac_client" = "YES"; then
if test -z "$refdb_cv_makestyle" || test -z "$refdb_cv_prefs" || test -z "$refdb_cv_parser" || test -z "$refdb_cv_record" || test -z "$refdb_cv_charset" || test -z "$refdb_cv_iconv" || test -z "$refdb_cv_clui" || test -z "$refdb_cv_sru" || test -z "$refdb_cv_sruserver" || test -z "$refdb_cv_cgi"; then
echo "Your system seems to miss some of the recommended Perl modules. Please"
echo "check the output above for details. It is safe to make and make install"
echo "anyway, but some of the accessory Perl scripts will fail to run if the"
echo " required modules are not installed. Please consult the manual where to"
echo "get the missing modules from."
echo
fi
fi

echo "Configuration summary:"
if test "$ac_client" = "YES"; then
echo "will build clients"
fi
if test "$ac_server" = "YES"; then
echo "will build server"
fi
if test "$ac_docs" = "YES"; then
echo "will build docs"
fi
if test "$ac_manpages" = "YES"; then
echo "will build man pages"
fi

echo ""
echo "What you should do next:"
echo "- run \"make\" to build RefDB."
echo "- run \"make install\" as root to install everything."
echo "- create the main database and the configuration files. Either run the"
echo "  refdb-init shell script as root, or peruse the handbook if you"
echo "  prefer a manual installation."
echo ""
