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

AC_INIT(configure.in)
AM_INIT_AUTOMAKE(evolution_sync, 0.82)
AM_CONFIG_HEADER(config.h)

AC_ISC_POSIX
AC_PROG_CC
AM_PROG_CC_STDC
AC_HEADER_STDC

EVO_VER=`evolution --version | sed -e 's/.nome .volution //g'`
EVO_MAJVER=${EVO_VER:0:1}
EVO_MINVER=${EVO_VER:2:1}

pkg_modules="gtk+-2.0 glib  libbonobo-2.0"
if (( EVO_MAJVER > 1 || ( EVO_MAJVER==1 && EVO_MINVER >= 4))); then
  pkg_modules="${pkgmodules} evolution-addressbook evolution-calendar libgnomeui-2.0"
else
  AC_MSG_ERROR(This version of the Evolution plugin (and MultiSync) does only work with Evolution >= 1.4.)
fi
PKG_CHECK_MODULES(PACKAGE, [$pkg_modules])
AC_SUBST(PACKAGE_CFLAGS)
AC_SUBST(PACKAGE_LIBS)


dnl Add the languages which your application supports here.
dnl ALL_LINGUAS=""
dnl AM_GLIB_GNU_GETTEXT

AC_PROG_LIBTOOL

CPPFLAGS="${PACKAGE_CFLAGS}"

AC_CHECK_HEADER(pthread.h,,AC_MSG_ERROR(The pthread headers must be installed.))
dnl Check if enable workaround for broken la files in evolution
AC_ARG_ENABLE(broken-evolution-la-workaround,[  --enable-broken-evolution-la-workaround
                          Turn on workaround for broken la files.
                          Try this only if you can't compile this module 
                          because it have some missing la files.])

dnl Find evolution and all libraries 
AC_ARG_WITH(evo-libs,[  --with-evo-libs=<path>  Path where the Evolution libraries reside (optional)], EVO_LIB_PATH="$withval", EVO_LIB_PATH="/usr/lib")
AC_CHECK_PROG(HAVE_EVOLUTION, evolution, yes, no)
if test "x${HAVE_EVOLUTION}" = "xno"; then
  AC_MSG_ERROR(Evolution must be installed.)
fi
AC_DEFINE_UNQUOTED(EVOLUTION_VERSION,"${EVO_VER:0:5}","The compiled Evolution version")
echo Detected evolution ${EVO_VER:0:5}.
EVO_LIBS=
EVO_DFLAGS=
rm -rf liblinks &> /dev/null
mkdir liblinks &> /dev/null
for evolib in ebook camel cal-client cal-util eame eutil ical-evolution\
	wombat ename; do
EVOLIB=`find ${EVO_LIB_PATH} -name "lib${evolib}.so" 2>/dev/null | sed -e 'q'`
if test "x${EVOLIB:0:1}" != "x/"; then
  EVOLIB=`find ${EVO_LIB_PATH} -name "lib${evolib}.so*" 2>/dev/null | sed -e 'q'`
  EVO_CREATE_SO="yes"
else
  EVO_CREATE_SO="no"
fi
if test "x${EVOLIB:0:1}" = "x/"; then
  echo Found ${evolib}.
  EVOLIBDIR=`dirname ${EVOLIB}`
  if test ${EVOLIBDIR} != "/usr/lib"; then
    EVO_LDFLAGS="${EVO_LDFLAGS} -Wl,-rpath=${EVOLIBDIR} -L${EVOLIBDIR}"
  fi
  if test ${EVO_CREATE_SO} = "yes" ;then 
    echo Found lib, but missing .so file, creating link.
    ln -s ${EVOLIB} liblinks/lib${evolib}.so
    EVO_LDFLAGS="-L\$(top_srcdir)/liblinks ${EVO_LDFLAGS}"
  fi
  EVO_LIBS="${EVO_LIBS} -l${evolib}"
  if test "x${enable_broken_evolution_la_workaround}" = "xyes" ; then
    BROKEN_LA_FILENAME=${EVOLIBDIR}/lib${evolib}.la
    if test -f ${BROKEN_LA_FILENAME}; then 
       EVO_LDFLAGS="-L\$(top_srcdir)/liblinks ${EVO_LDFLAGS}"
       cat ${BROKEN_LA_FILENAME} | sed s/^dependency_libs=.*/dependency_libs=/ > liblinks/lib${evolib}.la
   fi;    
  fi;
fi; done
if (echo ${EVO_LIBS} | grep -q -v cal-client); then
  AC_MSG_ERROR(Could not find the Evolution library libcal-client (and possibly other are missing as well). Please run configure with "--with-evo-libs=<path>".)
fi

# Debian bug workaround
# libcamel have a broken libcamel.la file, wish referens
# to /usr/lib/evolution/1.2/libeutil.la
AC_SUBST(EVO_LIBS)
AC_SUBST(EVO_LDFLAGS)

AC_PROG_YACC

dnl Info for the RPM
MULTISYNC_TOP="../.."
AC_SUBST(VERSION)
AC_SUBST(prefix)
MULTISYNC_VERSION=`grep "#define VERSION" ${MULTISYNC_TOP}/config.h | sed -e 's/#define VERSION //g' | sed -e 's/\"//g'`
AC_SUBST(MULTISYNC_VERSION)
AC_SUBST(EVO_RPM_VER)

AC_OUTPUT([
Makefile
src/Makefile
intl/Makefile
po/Makefile.in
${MULTISYNC_TOP}/specs/multisync-evolution.spec
])

