#MIN_CONFIG(3.3)

KDE_ENABLE_HIDDEN_VISIBILITY

AM_INIT_AUTOMAKE(tellico,1.3.6)

dnl AM_KDE_MIN_VERSION(MIN-VERSION-MAJOR, MIN-VERSION-MINOR, MIN-VERSION-MICRO)
dnl (taken from KMyMoney2)
dnl
AC_DEFUN([AM_KDE_MIN_VERSION],
[
    AC_MSG_CHECKING([for KDE version $1.$2.$3 or later])
    AC_LANG_SAVE
    AC_LANG_CPLUSPLUS
    save_CXXFLAGS=$CXXFLAGS
    CXXFLAGS="$CXXFLAGS -I$srcdir $all_includes"
    AC_TRY_COMPILE([
        #include "tdeversion.h"
        #if !( KDE_IS_VERSION( $1, $2, $3 ) )
        #error KDE version does not meet minimum requirement
        #endif
    ], [], AC_MSG_RESULT(yes), AC_MSG_ERROR(no))
    CXXFLAGS=$save_CXXFLAGS
    AC_LANG_RESTORE
])
AM_KDE_MIN_VERSION(3,3,1)

dnl CXXFLAGS="$NOOPT_CXXFLAGS" dnl __tdevelop[noopt]__
dnl CFLAGS="$NOOPT_CFLAGS" dnl __tdevelop[noopt]__
dnl CXXFLAGS="$CXXFLAGS $USE_EXCEPTIONS -DQT_NO_ASCII_CAST -DQT_NO_TRANSLATION" dnl __tdevelop[exc]__

dnl KDE_NEED_FLEX dnl __tdevelop__
dnl AC_PROG_YACC dnl __tdevelop__

AC_MSG_CHECKING(for ushort in <sys/types.h>)
AC_CACHE_VAL(bt_cv_type_ushort, [
AC_TRY_COMPILE([
#include <sys/types.h>
],[
ushort i;
], bt_cv_type_ushort=yes, bt_cv_type_ushort=no)])
AC_MSG_RESULT($bt_cv_type_ushort)
if test "$bt_cv_type_ushort" = "yes" ; then
  AC_DEFINE(HAVE_USHORT, 1, [Define if ushort is defined in <sys/types.h>])
fi

AC_MSG_CHECKING(for boolean in <sys/types.h>)
AC_CACHE_VAL(bt_cv_type_boolean, [
AC_TRY_COMPILE([
#include <sys/types.h>
],[
boolean f;
], bt_cv_type_boolean=yes, bt_cv_type_boolean=no)])
AC_MSG_RESULT($bt_cv_type_boolean)
if test "$bt_cv_type_boolean" = "yes" ; then
  AC_DEFINE(HAVE_BOOLEAN, 1, [Define if boolean is defined in <sys/types.h>])
fi

AC_MSG_CHECKING(for strdup declaration in <string.h>)
AC_EGREP_HEADER([strdup *\(], string.h, bt_cv_decl_strdup=yes, bt_cv_decl_strdup=no)
AC_MSG_RESULT($bt_cv_decl_strdup)
if test "$bt_cv_decl_strdup" = "yes" ; then
  AC_DEFINE(HAVE_STRDUP_DECL, 1, [Define if strdup is declared in <string.h>])
fi

dnl Checks for library functions.
AC_FUNC_ALLOCA
AC_FUNC_VPRINTF
AC_CHECK_FUNCS(strlwr strupr)

AC_CHECK_SIZEOF(unsigned int, 4)
AC_CHECK_SIZEOF(unsigned short, 2)
AC_CHECK_SIZEOF(unsigned char, 1)
AC_CHECK_SIZEOF(char, 1)

AC_CACHE_CHECK([for std::vector::at(std::size_t)],
        ac_cv_cxx_have_vector_at, [
        AC_LANG_SAVE
        AC_LANG_CPLUSPLUS
        AC_TRY_COMPILE([
                #include <vector>
        ],[
                std::vector<int> v(1);
                v.at(0);
                return 0;
        ],ac_cv_cxx_have_vector_at=yes, ac_cv_cxx_have_vector_at=no)
        AC_LANG_RESTORE
])
if test "$ac_cv_cxx_have_vector_at" = "yes"; then
        AC_DEFINE(HAVE_VECTOR_AT,1,[Define if you have the 'std::vector::at(std::size_t)' function])
fi

KDE_FIND_PATH(xml2-config, XML_CONFIG, [${prefix}/bin ${exec_prefix}/bin /usr/bin /usr/local/bin /opt/local/bin], [
  AC_MSG_ERROR([[Could not find xml2-config anywhere, check ftp://xmlsoft.org/ for libxml >= 2.6.0. If you are using pre-packaged libraries, be sure to have the development files installed as well, which may be called libxml2-devel or something similar.]])
])

if test -n "$XML_CONFIG"; then
  vers=`$XML_CONFIG --version 2>/dev/null | sed -e 's/libxml //' | awk 'BEGIN { FS = "."; } { printf "%d", ($1 * 1000 + $2) * 1000 + $3;}'`
  if test -n "$vers" && test "$vers" -ge 2006000
  then
    LIBXML_LIBS="`$XML_CONFIG --libs`"
    LIBXML_RPATH=
    for args in $LIBXML_LIBS; do
      case $args in
        -L*)
          LIBXML_RPATH="$LIBXML_RPATH $args"
        ;;
      esac
    done
    LIBXML_RPATH=`echo $LIBXML_RPATH | sed -e "s/-L/-R/g"`
    LIBXML_CFLAGS="`$XML_CONFIG --cflags`"
  else
    AC_MSG_ERROR([[You need at least libxml 2.6.0.]])
  fi
fi

AC_SUBST(LIBXML_LIBS)
AC_SUBST(LIBXML_CFLAGS)
AC_SUBST(LIBXML_RPATH)

AC_ARG_WITH([xml-catalog],
  AC_HELP_STRING([--with-xml-catalog=CATALOG],
                 [path to XML catalog, default=without-xml-catalog]),,
                 [with_xml_catalog=no])

if test x"$with_xml_catalog" != xno; then
  KDE_FIND_PATH(xmlcatalog, XMLCATALOG, [${prefix}/bin ${exec_prefix}/bin /usr/bin /usr/local/bin /opt/local/bin], [
    AC_MSG_ERROR([[Could not find xmlcatalog anywhere, check ftp://xmlsoft.org/ for libxml2 >= 2.4.23. If you are using pre-packaged libraries, be sure to have the utility files installed as well, which may be called libxml2-utils or something similar.]])
 ])

  AC_SUBST(XMLCATALOG)

  if test x"$with_xml_catalog" = xyes; then
    XML_CATALOG_FILE="/etc/xml/catalog"
  else
    XML_CATALOG_FILE="$with_xml_catalog"
  fi

  AC_MSG_CHECKING([for XML catalog])
  if test -f "$XML_CATALOG_FILE"; then
    AC_MSG_RESULT([$XML_CATALOG_FILE])
  else
    AC_MSG_ERROR([$XML_CATALOG_FILE not found])
  fi

  AC_SUBST(XML_CATALOG_FILE)
fi

KDE_FIND_PATH(xslt-config, XSLT_CONFIG, [${prefix}/bin ${exec_prefix}/bin /usr/bin /usr/local/bin /opt/local/bin], [
  AC_MSG_ERROR([[Could not find xslt-config anywhere, check ftp://xmlsoft.org/ for libxslt >= 1.0.19. If you are using pre-packaged libraries, be sure to have the development files installed as well, which may be called libxslt-devel or something similar.]])
])

if test -n "$XSLT_CONFIG"; then
  vers=`$XSLT_CONFIG --version 2>/dev/null | awk 'BEGIN { FS = "."; } { printf "%d", ($1 * 1000 + $2) * 1000 + $3;}'`
  if test -n "$vers" && test "$vers" -ge 1000019; then
    LIBXSLT_LIBS="`$XSLT_CONFIG --libs`"
    LIBXSLT_RPATH=
    for args in $LIBXSLT_LIBS; do
      case $args in
        -L*)
          LIBXSLT_RPATH="$LIBXSLT_RPATH $args"
        ;;
      esac
    done
    LIBXSLT_RPATH=`echo $LIBXSLT_RPATH | sed -e "s/-L/-R/g"`
    LIBXSLT_CFLAGS="`$XSLT_CONFIG --cflags`"
  else
    AC_MSG_ERROR([[You need at least libxslt 1.0.19.]])
  fi
fi

AC_SUBST(LIBXSLT_LIBS)
AC_SUBST(LIBXSLT_CFLAGS)
AC_SUBST(LIBXSLT_RPATH)

dnl ---------- TAGLIB CHECK ----------

AC_DEFUN([AC_HAVE_TAGLIB],
[
    AC_DEFINE(HAVE_TAGLIB, 1, [Define to 1 if you have the TagLib library])
    TAGLIB_CFLAGS=[`$TAGLIB_CONFIG --cflags`]
    TAGLIB_LIBS=[`$TAGLIB_CONFIG --libs`]
    TAGLIB_RPATH=
    for args in $TAGLIB_LIBS; do
      case $args in
        -L*)
          TAGLIB_RPATH="$TAGLIB_RPATH $args"
        ;;
      esac
    done
    TAGLIB_RPATH=`echo $TAGLIB_RPATH | sed -e "s/-L/-R/g"`
])

AC_DEFUN([AC_NO_TAGLIB],
[
    TAGLIB_CFLAGS=""
    TAGLIB_LIBS=""
    TAGLIB_RPATH=""
])

KDE_FIND_PATH(taglib-config, TAGLIB_CONFIG, [${prefix}/bin ${exec_prefix}/bin /usr/bin /usr/local/bin /opt/local/bin])
if test -n "$TAGLIB_CONFIG"; then
    AC_HAVE_TAGLIB
else
    AC_NO_TAGLIB
fi

AC_SUBST(TAGLIB_CFLAGS)
AC_SUBST(TAGLIB_LIBS)
AC_SUBST(TAGLIB_RPATH)

dnl ---------- END TAGLIB CHECK ----------

dnl ---------- YAZ CHECK ----------

AC_DEFUN([AC_HAVE_YAZ],
[
    AC_DEFINE(HAVE_YAZ, 1, [Define to 1 if you have the yaz library])
    YAZ_CFLAGS=[`$YAZ_CONFIG --cflags`]
    YAZ_LIBS=[`$YAZ_CONFIG --libs`]
    YAZ_RPATH=
    for args in $YAZ_LIBS; do
      case $args in
        -L*)
          YAZ_RPATH="$YAZ_RPATH $args"
        ;;
      esac
    done
    YAZ_RPATH=`echo $YAZ_RPATH | sed -e "s/-L/-R/g"`
])

AC_DEFUN([AC_NO_YAZ],
[
    YAZ_CFLAGS=""
    YAZ_LIBS=""
    YAZ_RPATH=""
])

KDE_FIND_PATH(yaz-config, YAZ_CONFIG, [${prefix}/bin ${exec_prefix}/bin /usr/bin /usr/local/bin /opt/local/bin])
if test -n "$YAZ_CONFIG"; then
    AC_HAVE_YAZ
else
    AC_NO_YAZ
fi

AC_SUBST(YAZ_CFLAGS)
AC_SUBST(YAZ_LIBS)
AC_SUBST(YAZ_RPATH)

dnl ---------- END YAZ CHECK ----------

dnl ---------- KCDDB CHECK ----------

AC_ARG_ENABLE(libkcddb,
    AC_HELP_STRING([--disable-libkcddb], [disable libkcddb, default=enable if found]),
    [enable_libkcddb=$enableval],
    [enable_libkcddb=yes]
)

if test x$enable_libkcddb = xyes; then

AC_LANG_SAVE
AC_LANG_CPLUSPLUS

ac_save_libs="$LIBS"
ac_save_cxxflags="$CXXFLAGS"

LIBS="$LIBS $all_libraries -lkcddb"
CXXFLAGS="$CXXFLAGS $all_includes"

AC_MSG_CHECKING(for libkcddb library and headers)
AC_TRY_LINK(
    [
#ifdef QT_NO_CAST_ASCII
#undef QT_NO_CAST_ASCII
#endif
#include <libkcddb/cddb.h>
        ],
    [KCDDB::CDDB c],
    [kcddb=yes],
    [kcddb=no]
)
AC_MSG_RESULT($kcddb)

LIBS="$ac_save_libs"
CXXFLAGS="$ac_save_cxxflags"
AC_LANG_RESTORE

if test x$kcddb = xyes; then
    AC_DEFINE(HAVE_KCDDB, 1, [Define to 1 if you have libkcddb])
    KCDDB_LIBS="-lkcddb"
else
    KCDDB_LIBS=
fi

dnl -- kcddb was disabled ---
else
    KCDDB_LIBS=
fi

AC_SUBST(KCDDB_LIBS)

dnl ---------- END KCDDB CHECK ----------

dnl ---------- KCAL CHECK ----------

AC_ARG_ENABLE(libkcal,
    AC_HELP_STRING([--disable-libkcal], [disable libkcal, default=enable if found]),
    [enable_libkcal=$enableval],
    [enable_libkcal=yes]
)

if test x$enable_libkcal = xyes; then

AC_LANG_SAVE
AC_LANG_CPLUSPLUS

ac_save_libs="$LIBS"
ac_save_cxxflags="$CXXFLAGS"

LIBS="$LIBS $all_libraries -lkcal"
CXXFLAGS="$CXXFLAGS $all_includes"

AC_MSG_CHECKING(for libkcal library and headers)
AC_TRY_LINK(
    [
#ifdef QT_NO_CAST_ASCII
#undef QT_NO_CAST_ASCII
#endif
#include <libkcal/todo.h>
        ],
    [KCal::Todo t],
    [kcal=yes],
    [kcal=no]
)
AC_MSG_RESULT($kcal)

LIBS="$ac_save_libs"
CXXFLAGS="$ac_save_cxxflags"
AC_LANG_RESTORE

if test x$kcal = xyes; then
    AC_DEFINE(HAVE_KCAL, 1, [Define to 1 if you have libkcal])
    KCAL_LIBS="-lkcal -ltderesources"
else
    KCAL_LIBS=
fi

dnl -- libkcal was disabled ---
else
    KCAL_LIBS=
fi

AC_SUBST(KCAL_LIBS)

dnl ---------- END KCAL CHECK ----------

AC_ARG_WITH([btparse],
            AC_HELP_STRING([--with-btparse], [Link against libbtparse
            as a shared lib (shared) or build it statically (static), default=static]),
            [case "${withval}" in
                 shared) btparse=shared ;;
                 static) btparse=static ;;
                    yes) btparse=shared ;;
                      *) AC_MSG_ERROR(bad value ${withval} for --with-btparse) ;;
             esac],
            [btparse=static])

AM_CONDITIONAL(USE_LIBBTPARSE, test x$btparse = xshared)

if test x$btparse = xshared; then
  KDE_CHECK_LIB(btparse, bt_initialize, [], [AC_MSG_ERROR(could not find libbtparse)])
fi

dnl ---------- AMAZON CHECK ----------

AC_ARG_ENABLE(amazon,
    AC_HELP_STRING([--disable-amazon], [disable Amazon.com searching, default=enable]),
    [enable_amazon=$enableval],
    [enable_amazon=yes]
)

if test x$enable_amazon = xyes; then
    AC_DEFINE(AMAZON_SUPPORT, 1, [Define to 1 if Amazon.com searching should be enabled])
fi

dnl ---------- END AMAZON CHECK ----------

dnl ---------- IMDB CHECK ----------

AC_ARG_ENABLE(imdb,
    AC_HELP_STRING([--disable-imdb], [disable IMDb searching, default=enable]),
    [enable_imdb=$enableval],
    [enable_imdb=yes]
)

if test x$enable_imdb = xyes; then
    AC_DEFINE(IMDB_SUPPORT, 1, [Define to 1 if IMDb searching should be enabled])
fi

dnl ---------- END IMDB CHECK ----------

dnl ---------- CDTEXT CHECK ----------
dnl Not sure if my code for CDText works on all platforms, allow disabling

AC_ARG_ENABLE(cdtext,
    AC_HELP_STRING([--disable-cdtext], [disable cdtext, default=enable (Linux only)]),
    [enable_cdtext=$enableval],
    [enable_cdtext=yes]
)

if test x$enable_cdtext = xyes; then
    AC_DEFINE(USE_CDTEXT, 1, [Define to 1 if you want to compile CD-Text support])
fi

dnl ---------- OpenOffice.org CHECK ----------

AC_ARG_ENABLE(ooo,
    AC_HELP_STRING([--enable-ooo], [enable support for OpenOffice.org, default=disable]),
    [enable_ooo=$enableval],
    [enable_ooo=no]
)

AM_CONDITIONAL(WITH_OOO, test x$enable_ooo = xyes)

dnl Options: OpenOffice.org installation
AC_ARG_WITH([ooo-dir],
	    AC_HELP_STRING([--with-ooo-dir=DIR],
			   [where the OpenOffice.org program files are installed]),
	    [ac_ooo_libdir="${withval}/program"
	     ac_ooo_datadir="${withval}/program"
             ac_ooo_basisdir="${withval}/basis3.0"])
AC_ARG_WITH([ooo-libdir],
	    AC_HELP_STRING([--with-ooo-libdir=DIR],
			   [where the OpenOffice.org program libraries are installed (<ooo-dir>/program)]),
	    [ac_ooo_libdir="${withval}"])
AC_ARG_WITH([ooo-datadir],
	    AC_HELP_STRING([--with-ooo-datadir=DIR],
			   [where the OpenOffice.org data files are installed (<ooo-dir>/program)]),
	    [ac_ooo_datadir="${withval}"])

dnl Options: OpenOffice.org SDK
AC_ARG_WITH([ooo-sdk-dir],
	    AC_HELP_STRING([--with-ooo-sdk-dir=DIR],
			   [where the OpenOffice.org SDK is installed]),
	    [ac_ooo_sdk_bindir="${withval}/linux/bin"
	     ac_ooo_sdk_includes="${withval}/include"])
AC_ARG_WITH([ooo-sdk-bindir],
	    AC_HELP_STRING([--with-ooo-sdk-bindir=DIR],
			   [where the OpenOffice.org SDK binaries are installed (<ooo-sdk-dir>/linux/bin)]),
	    [ac_ooo_sdk_bindir="${withval}"])
AC_ARG_WITH([ooo-sdk-includes],
	    AC_HELP_STRING([--with-ooo-sdk-includes=DIR],
			   [where the OpenOffice.org SDK includes are installed (<ooo-sdk-dir>/include)]),
	    [ac_ooo_sdk_includes="${withval}"])

if test x$enable_ooo = xyes; then

dnl Check for UNO cpp binding
KDE_FIND_PATH(cppumaker, CPPUMAKER, [$ac_ooo_sdk_bindir ${prefix}/bin ${exec_prefix}/bin /usr/bin /usr/local/bin /opt/local/bin], [
  AC_MSG_ERROR([[cppumaker not found, try '--with-ooo-sdk-dir=location_of_ODK']])
])

dnl Check for ODK includes
ac_cppflags_safe="$CPPFLAGS"
CPPFLAGS="-I$ac_ooo_sdk_includes"
KDE_CHECK_HEADERS([sal/config.h],
		 [OFFICE_SDK_includes="$CPPFLAGS"],
		 AC_MSG_ERROR([[OpenOffice.org SDK include files not found, try '--with-ooo-sdk-dir=location_of_ODK']]))
CPPFLAGS="$ac_cppflags_safe"

dnl OpenOffice.org libraries
if test -n "$ac_ooo_libdir" ; then
  OFFICE_libadd="-L$ac_ooo_libdir"
  OFFICE_libdir="$ac_ooo_libdir"
fi

dnl OpenOffice.org types.rdb
AC_CHECK_FILE([$ac_ooo_datadir/types.rdb],
	      [OFFICE_registry="$ac_ooo_datadir"/types.rdb],
	      AC_MSG_ERROR([[types.rdb not found, try '--with-ooo-dir=OpenOffice.org_installation_path']]))

AC_FIND_FILE([offapi.rdb],
             [$ac_ooo_basisdir/program $ac_ooo_libdir $ac_ooo_datadir],
             offapi_dir)
if test "$offapi_dir" != "NO"; then
  OFFICE_registry="$OFFICE_registry $offapi_dir/offapi.rdb"
fi

dnl Substitute the values
AC_SUBST(OFFICE_SDK_includes)
AC_SUBST(OFFICE_libadd)
AC_SUBST(OFFICE_libdir)
AC_SUBST(OFFICE_registry)
AC_SUBST(CPPUMAKER)

fi
CFLAGS="$CFLAGS -I\$(top_srcdir)/src/core"
CXXFLAGS="$CXXFLAGS -I\$(top_srcdir)/src/core"

dnl ---------- EXEMPI CHECK ----------

AC_ARG_ENABLE(exempi,
    AC_HELP_STRING([--disable-exempi], [disable exempi, default=enable if found]),
    [enable_libexempi=$enableval],
    [enable_libexempi=yes]
)

if test x$enable_libexempi = xyes; then
   KDE_PKG_CHECK_MODULES(EXEMPI,
                                 [exempi-2.0 >= 1.99.0],
                                 [have_exempi=yes],
                                 [have_exempi=no])
   AC_SUBST(EXEMPI_LIBS)
   AC_SUBST(EXEMPI_CFLAGS)
else
   have_exempi="no (disabled)"
fi

test "$have_exempi" = "yes" && AC_DEFINE(HAVE_EXEMPI, 1, [Define to 1 if exempi is available])


dnl ---------- END EXEMPI CHECK ----------

dnl ---------- POPPLER CHECK ----------

AC_ARG_ENABLE(poppler,
    AC_HELP_STRING([--disable-poppler], [disable poppler, default=enable if found]),
    [enable_libpoppler=$enableval],
    [enable_libpoppler=yes]
)

if test x$enable_libpoppler = xyes; then
   KDE_PKG_CHECK_MODULES(POPPLER,
                                 [poppler-tqt >= 0.5],
                                 [have_poppler=yes],
                                 [have_poppler=no])
   AC_SUBST(POPPLER_LIBS)
   AC_SUBST(POPPLER_CFLAGS)
else
   have_poppler="no (disabled)"
fi

test "$have_poppler" = "yes" && AC_DEFINE(HAVE_POPPLER, 1, [Define to 1 if poppler is available])


dnl ---------- END POPPLER CHECK ----------

dnl ---------- WEBCAM CHECK ----------

AC_ARG_ENABLE(webcam,
    AC_HELP_STRING([--enable-webcam], [enable support for webcams (experimental), default=disable]),
    [enable_webcam=$enableval],
    [enable_webcam=no]
)

if test x$enable_webcam = xyes; then
    KDE_CHECK_HEADERS([linux/videodev.h],
		     [],
		     [enable_webcam="no"])
fi

AM_CONDITIONAL(ENABLE_WEBCAM, test $enable_webcam = yes)
if test x$enable_webcam = xyes; then
    AC_DEFINE(ENABLE_WEBCAM, 1, [Define to 1 if webcam support is enabled])
fi

dnl ---------- END WEBCAM CHECK ----------

dnl ---------- SAX CHECK ----------

AC_ARG_ENABLE(sax,
    AC_HELP_STRING([--enable-sax], [enable SAX document loading (experimental), default=disable]),
    [enable_sax=$enableval],
    [enable_sax=no]
)

if test x$enable_sax = xyes; then
    AC_DEFINE(SAX_SUPPORT, 1, [Define to 1 if the XML loader should use SAX])
fi

dnl ---------- END SAX CHECK ----------

dnl -------- fix TQTDCOPIDLNG ---------
if test "$kde_cv_tqt" = "no"; then
    if test -z "$TQTDCOPIDLNG"; then
        KDE_FIND_PATH(dcopidlng, TQTDCOPIDLNG, [$kde_default_bindirs], [KDE_MISSING_PROG_ERROR(dcopidlng)])
    fi
fi