dnl Process this file with autoconf to produce a configure script.
AC_INIT(src)
AC_CANONICAL_SYSTEM

dnl Set the aux dir to .. (the toplevel evolution directory) so ylwrap
dnl is found in the correct directory for automake >= 1.5
AC_CONFIG_AUX_DIR(..)

AM_INIT_AUTOMAKE(libical,0.24) 
AM_CONFIG_HEADER(config.h)

dnl Subst in evolution's private directories
AC_SUBST(privlibdir)
AC_SUBST(privincludedir)
AC_SUBST(privdatadir)

dnl  Initialize maintainer mode
AM_MAINTAINER_MODE

dnl Checks for programs.
AC_PROG_YACC
AC_PROG_CC
AC_PROG_CXX
AM_PROG_LEX
AC_PROG_LN_S
AC_PROG_INSTALL

dnl Initialize libtool
AM_DISABLE_STATIC
AM_PROG_LIBTOOL

AC_SUBST(AR)

dnl *** check for Win32

AC_MSG_CHECKING([for Win32])
case "$host" in
*-mingw*)
    os_win32=yes
    ;;
*)  os_win32=no
    ;;
esac
AC_MSG_RESULT([$os_win32])
AM_CONDITIONAL(OS_WIN32, [test $os_win32 = yes])

dnl *** bdb4 support

AC_MSG_CHECKING(for Berkeley DB4 support)
AC_ARG_WITH(bdb4, [  --with-bdb4             Add support for Berkeley DB 4.0],
[case $with_bdb4 in  
    yes)	AC_MSG_RESULT(yes)
		AC_MSG_RESULT(  Adding Berkeley DB support)
		WITH_BDB4="yes"
		;;
    no)		AC_MSG_RESULT(no)
		;;
    *)		AC_MSG_RESULT(no)
		AC_MSG_RESULT(Ignoring unknown argument to --with-bdb4: $with_bdb4)
		;;
esac],
AC_MSG_RESULT(no))

if test x$WITH_BDB4 = xyes; then
  AC_ARG_WITH(bdb4_dir, [  --with-bdb4-dir=PATH    Use PATH as location of BerkeleyDB 4.0],
     BDB_DIR=$withval,
     BDB_DIR=/usr/local/BerkeleyDB.4.0
  )

  AC_MSG_CHECKING(for Berkeley DB include files)
  if test -f $BDB_DIR/include/db.h; then
   BDB_DIR_INCLUDE=$BDB_DIR/include
   AC_MSG_RESULT($BDB_DIR_INCLUDE)
  else
    if test -f $BDB_DIR/include/db4/db.h; then
     BDB_DIR_INCLUDE=$BDB_DIR/include/db4
     AC_MSG_RESULT($BDB_DIR_INCLUDE)
    else
     WITH_BDB4="no"
     AC_MSG_RESULT(not found!)
    fi
  fi

  AC_MSG_CHECKING(for Berkeley DB libraries)
  if test -f $BDB_DIR/lib/libdb-4.0.la; then
   BDB_DIR_LIB=$BDB_DIR/lib
   BDB_LIB=libdb-4.0.la
   AC_MSG_RESULT($BDB_DIR_LIB $BDB_LIB)
  else
    if test -f $BDB_DIR/lib/libdb4.la; then
     BDB_DIR_LIB=$BDB_DIR/lib
     BDB_LIB=libdb4.la
     AC_MSG_RESULT($BDB_DIR_LIB $BDB_LIB)
    else
     WITH_BDB4="no"
     AC_MSG_RESULT(not found!)
    fi
  fi

  AC_SUBST(BDB_DIR)
  AC_SUBST(BDB_DIR_INCLUDE)
  AC_SUBST(BDB_DIR_LIB)
  AC_SUBST(BDB_LIB)
  dnl 4.0 is required...
  BDB_VERSION="4.0"
  AC_SUBST(BDB_VERSION)
fi
AM_CONDITIONAL(WITH_BDB4, test x$WITH_BDB4 = xyes)

dnl *** Yacc/Bison support

if test "x$USE_MAINTAINER_MODE" == xyes; then
	DEV="yes"
	AC_MSG_RESULT([Setting up for development: -Wall, flex, bison])
fi

AM_CONDITIONAL(DEV, test x$DEV = xyes)

AC_CHECK_PROGS(AR, ar aal, ar)
AC_CHECK_PROGS(PERL, perl5 perl)  

AC_MSG_CHECKING(whether to define _REENTRANT)
AC_ARG_ENABLE(reentrant, [  --enable-reentrant      define _REENTRANT to enable reentrant system calls],
[ case "${enableval}" in
  no)
    AC_MSG_RESULT(no)
    ac_cv_reentrant=no
    ;;
  *) 
    AC_MSG_RESULT(yes)
    AC_DEFINE(ICAL_REENTRANT, ,[Define if we want _REENTRANT]) [CFLAGS="$CFLAGS -D_REENTRANT"]
    ac_cv_reentrant=yes
    ;;
  esac ],
  AC_MSG_RESULT(no)
)

dnl *** libicalcap support (experimental)

AC_MSG_CHECKING(for CAP/BEEP support)
AC_ARG_WITH(libicalcap, [  --with-libicalcap             Add support for CAP/BEEP support],
[case $with_libicalcap in  
    yes)	AC_MSG_RESULT(yes)
		AC_MSG_RESULT(  Adding CAP/BEEP support)
		WITH_CAP="yes"
		;;
    no)		AC_MSG_RESULT(no)
		;;
    *)		AC_MSG_RESULT(no)
		AC_MSG_RESULT(Ignoring unknown argument to --with-libicalcap: $with_libicalcap)
		;;
esac],
AC_MSG_RESULT(no))

if test x$WITH_CAP = xyes; then
  AC_MSG_CHECKING(for CAP/BEEP files)
  PKG_CHECK_MODULES(RRCAP, roadrunner	>= 0.9 \
                         rrtls		>= 0.9 \
                         rrsasl		>= 0.9 \
                         rrcap		>= 0.9)

  AC_SUBST(RRCAP_CFLAGS)
  AC_SUBST(RRCAP_LIBS)
fi
AM_CONDITIONAL(WITH_CAP, test x$WITH_CAP = xyes)

dnl **************************************************
dnl test/regression.c (and maybe others) needs this defined
dnl **************************************************
AC_DEFINE(ICAL_ERRORS_ARE_FATAL,0,[Define to make icalerror_* calls abort instead of internally signalling an error])
AC_DEFINE(NO_WARN_DEPRECATED,1,[Define to 1 if you DO NOT WANT to see deprecated messages])
AC_DEFINE(NO_WARN_ICAL_MALFORMEDDATA_ERROR_HACK,1,[Define to 1 if you DO NO WANT to see the warning messages related to ICAL_MALFORMEDDATA_ERROR and parsing .ics zoneinfo files])

AC_DEFINE(ICAL_UNIX_NEWLINE,1,[Define to terminate lines with "\n" instead of "\r\n"])
dnl AC_DEFINE(ICAL_NO_EMPTY_ARRAY_DECL,1,[Define to 1 if your compile does not like lines like: struct something foo[]])

dnl Checks for libraries.
dnl Replace `main' with a function in -lical:
dnl AC_CHECK_LIB(ical, main)

dnl Check if this system can use pthreads
dnl HACK to support FreeBSD 4.x pthread

case "${host}" in 
        *-*-freebsdelf4* )
		AC_MSG_RESULT(checking for pthread_create in -lpthread... -pthread (FreeBSD 4.x style))
		AC_DEFINE(HAVE_PTHREAD, ,[Define if we have pthread.])
		PTHREAD_LIBS=-pthread;;

	*)
	AC_CHECK_LIB(pthread,pthread_create,have_pthread=yes)
	if test "x$have_pthread" = xyes; then
	   AC_DEFINE(HAVE_PTHREAD, ,[Define if we have pthread.])
	   PTHREAD_LIBS=-lpthread
	fi;;
esac

AM_CONDITIONAL(HAVE_PTHREAD, test "$have_pthread" = "yes")
AC_SUBST(PTHREAD_LIBS)

dnl Checks for header files.
AC_HEADER_STDC
AC_CHECK_HEADERS(assert.h ctype.h pthread.h stdint.h time.h sys/types.h unistd.h wctype.h)

dnl Checks for typedefs, structures, and compiler characteristics.
AC_C_CONST
AC_TYPE_SIZE_T
AC_TYPE_MODE_T
AC_CHECK_TYPE(intptr_t)
AC_STRUCT_TM

dnl Checks for library functions.
AC_CHECK_FUNCS(gmtime_r isspace iswspace strdup snprintf unsetenv)

dnl **************************************************
dnl * Check for C++
dnl **************************************************

AC_ARG_ENABLE(cxx, 
[  --enable-cxx           Enable C++ bindings],,enable_cxx="no")
AM_CONDITIONAL(WITH_CXX_BINDINGS, test x$enable_cxx = "xyes")

dnl **************************************************
dnl * Check for Java
dnl **************************************************

AC_ARG_ENABLE(java, 
[  --enable-java          Enable Java bindings],,enable_java="no")
if test "x$enable_java" = "xyes"; then
  AC_PATH_PROG(JAR, jar, /usr/local/bin/jar, /usr/local/bin:$PATH)
  AC_PATH_PROG(JAVA, java, /usr/local/bin/java, /usr/local/bin:$PATH)
  AC_PATH_PROG(JAVAC, javac, /usr/local/bin/javac, /usr/local/bin:$PATH)
  AC_PATH_PROG(JAVAH, javah, /usr/local/bin/javah, /usr/local/bin:$PATH)
  AC_SUBST(JAR)
  AC_SUBST(JAVA)
  AC_SUBST(JAVAC)
  AC_SUBST(JAVAH)

  dnl truly gross

  case "${host}" in
        *-*-solaris*)   JAVA_PLATFORM="solaris" ;;
        *-*-linux*)     JAVA_PLATFORM="linux" ;;
        *-*-freebsd*)   JAVA_PLATFORM="bsd" ;;
        *-*-aix*)       JAVA_PLATFORM="aix" ;;
        *-sequent-*)    JAVA_PLATFORM="ptx" ;;
        *-*-hpux*)      JAVA_PLATFORM="hpux" ;;
        *-*-irix*)      JAVA_PLATFORM="irix" ;;
        *-*-os390*)     JAVA_PLATFORM="os390" ;;
        *-*-os400*)     JAVA_PLATFORM="os400" ;;
        *-apple-darwin*|*-apple-rhapsody*)      JAVA_PLATFORM="darwin" ;;
        *)              JAVA_PLATFORM="unknown" ;;
  esac

  AC_SUBST(JAVA_PLATFORM)
fi
enable_java=no
AM_CONDITIONAL(WITH_JAVA, test x$enable_java = "xyes")

dnl **************************************************
dnl * Check for Python
dnl **************************************************
python_val=false
AC_ARG_ENABLE(python, 
[  --enable-python         Enable python bindings],,enable_python="no")

if test "x$enable_python" = "xyes"; then
	python_val=true
else
	python_val=false
fi

if test $python_val = true; then
        
	AC_CHECK_PROG(python_val, python, true, false)
	AC_CHECK_PROG(python_val, swig, true, false)

	  PY_PREFIX=`python -c 'import sys ; print sys.prefix'`
	  PY_EXEC_PREFIX=`python -c 'import sys ; print sys.exec_prefix'`
	  changequote(<<, >>)dnl
	  PY_VERSION=`python -c 'import sys ; print sys.version[0:3]'`
	  changequote([, ])dnl
	  if test -f $PY_PREFIX/include/python$PY_VERSION/Python.h; then
		  PY_LIBS="python$PY_VERSION"
		  PY_LIB_LOC="-L$PY_EXEC_PREFIX/lib/python$PY_VERSION/config"
		  PY_CFLAGS="-I$PY_PREFIX/include/python$PY_VERSION"
		  PY_MAKEFILE="$PY_EXEC_PREFIX/lib/python$PY_VERSION/config/Makefile"
		  PY_LOCALMODLIBS=`sed -n -e 's/^LOCALMODLIBS=\(.*\)/\1/p' $PY_MAKEFILE`
		  PY_BASEMODLIBS=`sed -n -e 's/^BASEMODLIBS=\(.*\)/\1/p' $PY_MAKEFILE`
		  PY_OTHER_LIBS=`sed -n -e 's/^LIBS=\(.*\)/\1/p' $PY_MAKEFILE`
		  PY_EXTRA_LIBS="$PY_LOCALMODLIBS $PY_BASEMODLIBS $PY_OTHER_LIBS"
		  AC_SUBST(PY_LIBS)
		  AC_SUBST(PY_LIB_LOC)
		  AC_SUBST(PY_CFLAGS)
		  AC_SUBST(PY_EXTRA_LIBS)
	  else
		  python_val=false
	  fi
fi
python_val=false
AM_CONDITIONAL(WITH_PYTHON, test $python_val = true)

AC_OUTPUT([
Makefile
design-data/Makefile
doc/Makefile
examples/Makefile
scripts/Makefile
src/Makefile
src/libical/Makefile
src/libical/icalversion.h
src/libicalcap/Makefile
src/libicalss/Makefile
src/libicalvcal/Makefile
src/test/Makefile
test-data/Makefile
zoneinfo/Makefile
])

