# Copyright (C) 2001 Leni Mayo <serel@moniker.net>
#  
# This file is free software.  The author gives unlimited permission
# to copy and/or distribute it, with or without modifications, as long
# as this notice is preserved.
# 
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY, to the extent permitted by law; without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#

# TODO:
# - Lots more checking, eg:
#	- the ac_func_vsnprintf.m4 from http://www.gnu.org/software/ac-archive/
# - building serel requires good installations of glib and redland.
#	The dependencies upon these libraries is not very well expressed here,
#	basically because the time I've spent nutting it out hasn't been sufficient
#	to get it right.
#	Instead, src/Makefile.am makes assumptions about the health and
#	location of these two packages.
#	Help cleaning this is up is welcome!
#	leni - serel@fastboot.org - Tue Apr 23 13:02:03 EST 2002
#

AC_INIT(reconf)
AM_CONFIG_HEADER(config.h)
AM_INIT_AUTOMAKE(serel,0.3.4)
AM_MAINTAINER_MODE # enables the --enable-maintainermode and the @MAINT@ formulation
AC_ARG_PROGRAM
AC_PROG_CC
AC_PROG_INSTALL
AC_PROG_LN_S
AC_HEADER_STDC
AC_PREFIX_DEFAULT(/usr)
# AC_CHECK_HEADERS(redland.h glib.h)

AC_CHECK_LIB(glib, g_hash_table_new)
AC_ARG_ENABLE(packaging, [  --enable-packaging         Turn on options that configure serel for rpm/deb packaging ], packaging="yes", packaging="no")

if test $packaging = yes; then 
	staticredland="yes"
	nodebug=yes
	TARGET_RDF2DOT=#
else
	staticredland="yes"
	nodebug=yes
	TARGET_RDF2DOT=#
fi

# try and figure out where the glib includes files are installed
# glib_INCLUDES ought to be eg. sometimes /usr/include/glib-1.2 needs to be added

glib_INCLUDES="-I/usr/lib/glib/include"

saveCPPFLAGS="$CPPFLAGS"
CPPFLAGS="$CPPFLAGS $glib_INCLUDES"

AC_TRY_COMPILE([#include "glib.h"], [GSList *pList = NULL; pList++;], compiled="yes", compiled="no")

if test $compiled = no; then 
	glib_INCLUDES="$glib_INCLUDES -I/usr/include/glib-1.2"
	CPPFLAGS="$saveCPPFLAGS $glib_INCLUDES"

	AC_TRY_COMPILE([#include "glib.h"], [GSList *pList = NULL; pList++;], compiled="yes", compiled="no")

	if test $compiled = no; then
			echo "Error: Unable to find glib include files - make sure that glib (aka libglib) is installed."
			exit
	fi
fi
CPPFLAGS="$saveCPPFLAGS"


# try and figure out whether librdf contains expat or not
#
saveLIBS="$LIBS"
LIBS="$LIBS -lrdf -ldb -lexpat"
rdf_contains_expat="unknown"
AC_TRY_RUN([main() {XML_ParserCreateNS(); return(0);}], rdf_contains_expat="no", rdf_contains_expat="unknown", rdf_contains_expat="unknown")
if test $rdf_contains_expat = unknown; then 
	LIBS="$LIBS -lrdf -ldb"
	AC_TRY_RUN([main() {XML_ParserCreateNS(); return(0);}], rdf_contains_expat="yes", rdf_contains_expat="unknown", rdf_contains_expat="unknown")
fi
LIBS="$saveLIBS"

libexpat=
if test $rdf_contains_expat = no; then
	libexpat="-lexpat"
fi

AC_CHECK_LIB(rdf, librdf_new_world, rdf_LIBS="yes", rdf_LIBS="no", -ldb $libexpat)

if test $rdf_LIBS = yes; then
	# We don't use --static here 'cos this option affects linking of the entire
	# entire executable, and we only want the redland libraries static,
	# assuming that shared libraries for (libc, glib etc) are widely installed
	#
	# This code is fragile in a number of different ways, eg, it may not work
	# with non-gcc compilers and linkers.  If you strike a problem, please let
	# us know - serel@fastboot.org
	#
	if test $staticredland = yes; then

		saveLIBS="$LIBS"
		static_lib_db="unknown"
		LIBS="$LIBS -Xlinker -Bstatic -lrdf -ldb $libexpat -Xlinker -Bdynamic"
		AC_TRY_RUN([main() {XML_ParserCreateNS(); return(0);}], static_lib_db="-ldb", static_lib_db="unknown", static_lib_db="unknown")

		if test $static_lib_db = unknown; then 
			LIBS="$saveLIBS"
			LIBS="$LIBS -Xlinker -Bstatic -lrdf -ldb-3.1 $libexpat -Xlinker -Bdynamic"
			AC_TRY_RUN([main() {XML_ParserCreateNS(); return(0);}], static_lib_db="-ldb-3.1", static_lib_db="unknown", static_lib_db="unknown")
		fi

		if test $static_lib_db = unknown; then 
			echo Error: Unable to statically link redland.  Try configure without the --enable-staticredland option
			exit
		else
			rdf_LIBS="-Xlinker -Bstatic -lrdf $static_lib_db $libexpat -Xlinker -Bdynamic"
		fi

		LIBS="$saveLIBS"

	else
		rdf_LIBS="-lrdf -ldb $libexpat"
	fi
else
	echo Error: serel requires the redland library in order to build
	echo See: http://www.redland.opensource.ac.uk/
	exit
fi

# deal with the nodebug option
#
if test "$ac_test_CFLAGS" != set; then
	if test $nodebug = yes -a "$GCC" = yes; then
    	CFLAGS="-O2"
	elif test $ac_cv_prog_cc_g = yes; then
    	CFLAGS="-g"
	fi
fi

PATCHFOR_REDHAT=#
PATCHFOR_DEBIAN=#
patchfor_msg=

AC_ARG_ENABLE(patchfor, [  --enable-patchfor=PLATFORM  Only distribute patch files for PLATFORM [ redhat debian ]], patchfor="$enableval")
if test "$patchfor" = redhat; then
	PATCHFOR_REDHAT=
	patchfor_msg="only distributing patch files for redhat"
elif test "$patchfor" = debian; then
	PATCHFOR_DEBIAN=
	patchfor_msg="only distributing patch files for debian"
else
	PATCHFOR_REDHAT=
	PATCHFOR_DEBIAN=
	patchfor_msg="distributing patch files for all platforms"
fi

AC_MSG_RESULT($patchfor_msg)

AC_SUBST(TARGET_RDF2DOT)

AC_SUBST(PATCHFOR_REDHAT)
AC_SUBST(PATCHFOR_DEBIAN)

AC_SUBST(rdf_LIBS)
AC_SUBST(glib_INCLUDES)
AC_PATH_PROGS(BASH, bash)
AC_PATH_PROGS(SH, sh)
# The following progs are used by serel-rdfgen (only)
#
AC_PATH_PROGS(RM, rm)
AC_PATH_PROGS(CAT, cat)
AC_PATH_PROGS(RPM, rpm)
AC_PATH_PROGS(DPKG, dpkg)
AC_PATH_PROGS(GREP, grep)
AC_PATH_PROGS(TAR, tar)
AC_OUTPUT(Makefile data/Makefile doc/Makefile doc/docbook/Makefile doc/man/Makefile doc/html/Makefile scripts/Makefile src/Makefile test/Makefile packages/Makefile)
