dnl    This file is part of the KDE libraries/packages
dnl    Copyright (C) 2001 Stephan Kulow (coolo@kde.org)
 
dnl    This file is free software; you can redistribute it and/or
dnl    modify it under the terms of the GNU Library General Public
dnl    License as published by the Free Software Foundation; either
dnl    version 2 of the License, or (at your option) any later version.
 
dnl    This library is distributed in the hope that it will be useful,
dnl    but WITHOUT ANY WARRANTY; without even the implied warranty of
dnl    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
dnl    Library General Public License for more details.
 
dnl    You should have received a copy of the GNU Library General Public License
dnl    along with this library; see the file COPYING.LIB.  If not, write to
dnl    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
dnl    Boston, MA 02111-1307, USA.

# Original Author was Kalle@kde.org
# I lifted it in some mater. (Stephan Kulow)
# I used much code from Janos Farkas

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

AC_INIT(acinclude.m4) dnl a source file from your sub dir

dnl This is so we can use kde-common
AC_CONFIG_AUX_DIR(admin)

dnl This ksh/zsh feature conflicts with `cd blah ; pwd`
unset CDPATH

dnl Checking host/target/build systems, for make, install etc.
AC_CANONICAL_SYSTEM 
dnl Perform program name transformation
AC_ARG_PROGRAM

dnl Automake doc recommends to do this only here. (Janos)
AM_INIT_AUTOMAKE(kbib, 0.6.1) dnl searches for some needed programs

KDE_SET_PREFIX

dnl generate the config header
AM_CONFIG_HEADER(config.h) dnl at the distribution this done

dnl Checks for programs.
AC_CHECK_COMPILERS
AC_ENABLE_SHARED(yes)
AC_ENABLE_STATIC(no)
KDE_PROG_LIBTOOL

dnl for NLS support. Call them in this order!
dnl WITH_NLS is for the po files
AM_KDE_WITH_NLS

KDE_USE_QT(3)
AC_PATH_KDE
#MIN_CONFIG(3)

dnl PACKAGE set before

dnl CXXFLAGS="$NOOPT_CXXFLAGS" dnl __kdevelop[noopt]__
dnl CFLAGS="$NOOPT_CFLAGS" dnl __kdevelop[noopt]__
dnl CXXFLAGS="$CXXFLAGS $USE_EXCEPTIONS" dnl __kdevelop[exc]__

dnl KDE_NEED_FLEX dnl __kdevelop__
dnl AC_PROG_YACC dnl __kdevelop__


dnl ---------- LIBXML and LIBXSLT CHECK ----------
AC_DEFUN([AC_HAVE_XSLT],
[
    AC_DEFINE(HAVE_XSLT, 1, [Define to 1 if you have the libxml & libxslt libraries])
    LIBXML_CFLAGS="`$XML_CONFIG --cflags`"
    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"`
 
    LIBXSLT_LIBS="`$XSLT_CONFIG --libs` -lexslt"
    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`"
])

AC_DEFUN([AC_NO_XSLT],
[
    echo "-----------------------------------------------------------"
    echo "You don't have libxml >= 2.6.0 and/or libxslt >= 1.0.19. Remote database searching using USMARC/MARC21/UNIMARC/MARC21XML/UNIMARCXML format will not be available"
    echo "-----------------------------------------------------------"
    AC_DEFINE(HAVE_XSLT, 0, [Define to 1 if you have the libxml & libxslt libraries])
    LIBXML_CFLAGS=""
    LIBXML_LIBS=""
    LIBXML_RPATH=""
    LIBXSLT_CFLAGS=""
    LIBXSLT_LIBS=""
    LIBXSLT_RPATH=""
]) 	
 
KDE_FIND_PATH(xml2-config, XML_CONFIG, [${prefix}/bin ${exec_prefix}/bin /usr/bin /usr/local/bin /opt/local/bin])
KDE_FIND_PATH(xslt-config, XSLT_CONFIG, [${prefix}/bin ${exec_prefix}/bin /usr/bin /usr/local/bin /opt/local/bin])


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
  
  	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
			AC_HAVE_XSLT
		else
			AC_NO_XSLT
		fi
	else
		AC_NO_XSLT
	fi
  else
  	AC_NO_XSLT
  fi
else
  AC_NO_XSLT
fi

AC_SUBST(LIBXML_LIBS)
AC_SUBST(LIBXML_CFLAGS)
AC_SUBST(LIBXML_RPATH)  
AC_SUBST(LIBXSLT_LIBS)
AC_SUBST(LIBXSLT_CFLAGS)
AC_SUBST(LIBXSLT_RPATH)
dnl ---------- END LIBXML and LIBXSLT 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"`
    
    YAZPP_CFLAGS=[`$YAZPP_CONFIG --cflags`]
    YAZPP_LIBS=[`$YAZPP_CONFIG --libs`]
    YAZPP_RPATH=
    for args in $YAZPP_LIBS; do
      case $args in
        -L*)
          YAZPP_RPATH="$YAZPP_RPATH $args"
        ;;
      esac
    done
    YAZPP_RPATH=`echo $YAZPP_RPATH | sed -e "s/-L/-R/g"`
])

AC_DEFUN([AC_NO_YAZ],
[
    echo "-----------------------------------------------------------"
    echo "You don't have YAZ and/or YAZ++. Remote database searching to Z9530 servers will not be available."
    echo "-----------------------------------------------------------"
    AC_DEFINE(HAVE_YAZ, 0, [Define to 1 if you have the yaz library])
    YAZ_CFLAGS=""
    YAZ_LIBS=""
    YAZ_RPATH=""
    YAZPP_CFLAGS=""
    YAZPP_LIBS=""
    YAZPP_RPATH=""
])


KDE_FIND_PATH(yazpp-config, YAZPP_CONFIG, [${prefix}/bin ${exec_prefix}/bin /usr/bin /usr/local/bin /opt/local/bin])
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" && test -n "$YAZPP_CONFIG"; then
	vers=`$YAZ_CONFIG --version 2>/dev/null | awk 'BEGIN { FS = "."; } { printf "%d", ($1 * 1000 + $2) * 1000 + $3;}'`
	if test -n "$vers" && test "$vers" -ge 3000000
  	then
		echo "Yaz version >= 3.0.0"
		AC_DEFINE(YAZ_VERSION_3_0_0, 1, [Define to 1 if you have the yaz>=3.0.0])
	else
		echo "Yaz version < 3.0.0"
		AC_DEFINE(YAZ_VERSION_3_0_0, 0, [Define to 1 if you have the yaz>=3.0.0])
	fi
	
	
	AC_HAVE_YAZ

else
    AC_NO_YAZ
fi


AC_SUBST(YAZ_CFLAGS)
AC_SUBST(YAZ_LIBS)
AC_SUBST(YAZ_RPATH)
AC_SUBST(YAZPP_CFLAGS)
AC_SUBST(YAZPP_LIBS)
AC_SUBST(YAZPP_RPATH)

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

# -----------------------------------------------------------------
# pkg config check
# -----------------------------------------------------------------

AC_ARG_VAR(PKGCONFIGFOUND, [Path to pkg-config])
AC_CHECK_PROG(PKGCONFIGFOUND, pkg-config,[yes])

dnl ---------- POPPLER CHECK ----------
POPPLER_LIBS=''
POPPLER_CFLAGS=''
if test x$PKGCONFIGFOUND = xyes; then
   KDE_PKG_CHECK_MODULES(POPPLER, poppler >= 0.5.0, have_poppler=yes,have_poppler=no)
   KDE_PKG_CHECK_MODULES(POPPLER_QT, poppler-qt >= 0.5.0, have_poppler_qt=yes,have_poppler_qt=no)
   	
   if test x$have_poppler = xyes && test x$have_poppler_qt = xyes; then
   	POPPLER_LIBS=[`pkg-config --libs poppler-qt`]   
   	POPPLER_CFLAGS=[`pkg-config --cflags poppler-qt`]
   else
      	AC_MSG_ERROR([[Could not find poppler and/or poppler-qt. If you are using pre-packaged packages, be sure to have the development packages installed as well, which may be called poppler-devel and poppler-qt-devel or something similar.]])
   fi
else
	AC_MSG_ERROR([[Could not find pkg-config ]])
fi
AC_SUBST(POPPLER_CFLAGS)
AC_SUBST(POPPLER_LIBS)

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

KDE_CREATE_SUBDIRSLIST
AC_CONFIG_FILES([ Makefile ])
AC_CONFIG_FILES([ doc/Makefile ])
AC_CONFIG_FILES([ doc/en/Makefile ])
AC_CONFIG_FILES([ kbib/Makefile ])
AC_CONFIG_FILES([ po/Makefile ])
AC_OUTPUT
if test "$all_tests" = "bad"; then
  if test ! "$cache_file" = "/dev/null"; then
    echo ""    
    echo "Please remove the file $cache_file after changing your setup"
    echo "so that configure will find the changes next time."
    echo ""
  fi
else
  echo ""
  echo "Good - your configure finished. Start make now"
  echo ""
fi
