dnl Process this file with autoconf to produce a configure script.
AC_INIT(src/dbi_main.c)
AM_MAINTAINER_MODE

AM_INIT_AUTOMAKE(libdbi, 0.8.4)
AM_CONFIG_HEADER(config.h)
AC_CONFIG_MACRO_DIR([m4])

dnl Library versioning
LIB_CURRENT=1
LIB_REVISION=0
LIB_AGE=0
AC_SUBST(LIB_CURRENT)
AC_SUBST(LIB_REVISION)
AC_SUBST(LIB_AGE)

AC_CANONICAL_HOST

driverdir=$libdir/dbd
AC_SUBST(driverdir)

dnl ====================================
dnl Check for programs
dnl ====================================

AC_PROG_CC
AC_PROG_CPP
AC_PROG_LD
AC_PROG_INSTALL
AC_PROG_LIBTOOL
AC_SUBST(LIBTOOL)
dnl ====================================
dnl Set some general compile options
dnl ====================================

if test -z "$GCC"; then
        case $host in
        *-*-irix*)
                if test -z "$CC"; then
                        CC=cc
                fi
                DEBUG="-g -signed"
                CFLAGS="-O2 -w -signed"
                PROFILE="-p -g3 -O2 -signed" ;;
        sparc-sun-solaris*)
                DEBUG="-v -g"
                CFLAGS="-xO4 -fast -w -fsimple -native -xcg92"
                PROFILE="-v -xpg -g -xO4 -fast -native -fsimple -xcg92 -Dsuncc" ;;
        *)
                DEBUG="-g"
                CFLAGS="-O"
                PROFILE="-g -p" ;;
        esac
else

        case $host in
        *-*-linux*)
                DEBUG="-g -Wall -D_REENTRANT -D__NO_MATH_INLINES -fsigned-char -std=gnu99"
                CFLAGS="-O20 -ffast-math -D_REENTRANT -fsigned-char -std=gnu99"
                PROFILE="-pg -g -O20 -ffast-math -D_REENTRANT -fsigned-char -std=gnu99";;
        sparc-sun-*)
                DEBUG="-g -Wall -D__NO_MATH_INLINES -fsigned-char -mv8 -std=gnu99"
                CFLAGS="-O20 -ffast-math -D__NO_MATH_INLINES -fsigned-char -mv8 -std=gnu99"
                PROFILE="-pg -g -O20 -D__NO_MATH_INLINES -fsigned-char -mv8 -std=gnu99" ;;
        *)
                DEBUG="-g -Wall -D__NO_MATH_INLINES -fsigned-char -std=gnu99"
                CFLAGS="-O20 -D__NO_MATH_INLINES -fsigned-char -std=gnu99"
                PROFILE="-O20 -g -pg -D__NO_MATH_INLINES -fsigned-char -std=gnu99" ;;
        esac
fi

AC_SUBST(DEBUG)
AC_SUBST(PROFILE)

case $host in
	*-*-*bsd*)
		dlopen_flag="RTLD_LAZY" ;;
	*)
		dlopen_flag="RTLD_NOW" ;;
esac

AC_DEFINE_UNQUOTED(DLOPEN_FLAG, $dlopen_flag, [ Specifies the mode of dlopen behavior ])

dnl ==============================
dnl Check for libraries
dnl ==============================

dnl functions to load dynamic libraries
use_ltdl=no
AC_ARG_WITH(ltdl,
	[  --with-ltdl	          Use libtool's libltdl to load runtime modules [no]],
	[  use_ltdl=$withval ])
	
if test "x$use_ltdl" = xyes; then
AC_CHECK_HEADERS([ltdl.h], [AC_CHECK_LIB([ltdl],[lt_dlopen],[LIBADD_DL=-lltdl])])
else
AC_CHECK_HEADERS([mach-o/dyld.h],[AC_CHECK_FUNCS([NSLinkModule], [
       LIBADD_DL=""
       dlsym_prefix=_
       ])],
[
AC_CHECK_HEADERS([dlfcn.h])
AC_CHECK_FUNCS([dlopen],[LIBADD_DL=],
		[AC_CHECK_LIB([dl],[dlopen],[LIBADD_DL=-ldl],[])])])
fi
AC_SUBST(LIBADD_DL)
dnl ==============================
dnl Check for functions
dnl ==============================

case $host in
	*-*-win32*)
		shlib_ext=.dll ;;
	*-*-cygwin*)
		shlib_ext=.dll ;;
	*-*-mingw32*)
		shlib_ext=.dll ;;
	*-*-darwin*)
		shlib_ext=.so ;;
	*)
		shlib_ext=.so ;;
esac

AC_ARG_WITH(shlib-ext,
	[  --with-shlib-ext	  Specifies the filename extension of loadable modules (usually not necessary).],
	[  shlib_ext=$withval ])
AC_ARG_WITH(dlsym-prefix,
	[  --with-dlsym-prefix	  Specifies a required prefix for symbol names of dynamically loaded modules (usually not necessary).],
	[  dlsym_prefix=$withval ])

AC_DEFINE_UNQUOTED(DRIVER_EXT, "$shlib_ext", [ Specifies the filename extension of loadable modules ])
AC_DEFINE_UNQUOTED(DLSYM_PREFIX, "$dlsym_prefix", [ Specifies a required prefix for symbol names of dynamically loaded modules ])


AC_CHECK_FUNCS(strtoll)
AC_REPLACE_FUNCS(atoll timegm)
AC_CHECK_FUNCS(vasprintf)
AC_REPLACE_FUNCS(asprintf)

dnl ==============================
dnl Checks for header files
dnl ==============================

AC_CHECK_HEADERS(string.h strings.h)

dnl ==============================
dnl See whether to build the docs
dnl ==============================

ac_docs="YES"
AC_ARG_ENABLE(docs,
	[  --disable-docs	  do not build and install the documentation.],
	[  if test "$enable_docs" = "no"; then
	      ac_docs="NO"
	   fi ])

if test "$ac_docs" = "YES"; then
dnl   AC_PATH_PROGS(myjade, openjade jade)
dnl   if test "x$myjade" != "x"; then
      docs_subdirs="doc"
dnl   else
dnl      docs_subdirs=""
dnl   fi
else
   docs_subdirs=""
fi

AC_SUBST(docs_subdirs)

dnl ==============================
dnl Write output files
dnl ==============================

AC_CONFIG_FILES([include/dbi/dbi.h])

AC_OUTPUT([
	libdbi.spec
	Makefile
	src/Makefile
	doc/Makefile
	include/Makefile
	include/dbi/Makefile
	])

