#MIN_CONFIG(3)

AM_INIT_AUTOMAKE(digikam,0.9.6)

# -----------------------------------------------------------------
#
# enable hidden visibility only if kde >= 3.3.2 and kdelibs has
# been compiled with visibility enabled
#
# -----------------------------------------------------------------

AC_LANG_PUSH(C++)
digikam_save_cppflags=$CPPFLAGS
CPPFLAGS="$CPPFLAGS $all_includes"  
AC_MSG_CHECKING([if hidden visibility should be enabled])
AC_COMPILE_IFELSE(
 [
   #include <kdeversion.h>
   #include <kdemacros.h>
   int other_func( void )
   { 
      #if KDE_IS_VERSION(3,3,2)
      #else
        iam dying;
      #endif
      #ifdef __KDE_HAVE_GCC_VISIBILITY
      #else
        no, iam really dead;
      #endif      
      return 0;
   }
 ], 
 [ AC_MSG_RESULT([yes])
   digikam_enable_hidden_visibility="yes" ],
 [ AC_MSG_RESULT([no]) ] 
)
CPPFLAGS=$digikam_save_cppflags
AC_LANG_POP(C++)

if test "x$digikam_enable_hidden_visibility" = "xyes"; then
   KDE_ENABLE_HIDDEN_VISIBILITY 
fi

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

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

# -----------------------------------------------------------------
#
# sqlite2 type check
#
# -----------------------------------------------------------------

KDE_CHECK_TYPES

# -------------------------------------------------------
#
# Check endianness
#
# -------------------------------------------------------

AC_LANG_SAVE
AC_LANG_C
AC_C_BIGENDIAN
AC_LANG_RESTORE

# -----------------------------------------------------------------
#
# Check for liblcms
#
# -----------------------------------------------------------------

have_lcms_header='no'
KDE_CHECK_HEADER(lcms/lcms.h,have_lcms_header='yes',,)
if test "$have_lcms_header" = 'yes'
then
    AC_DEFINE(LCMS_HEADER, <lcms/lcms.h>, [The correct header])
else
    # Alternative! Debian does it this way...
    KDE_CHECK_HEADER(lcms.h,have_lcms_header='yes',,)
    if test "$have_lcms_header" = 'yes'
    then
        AC_DEFINE(LCMS_HEADER, <lcms.h>, [The correct header])
    fi
fi

LCMS_LIBS=''
have_lcms='no'
if test "$have_lcms_header" = 'yes'
then
    saved_cflags="$CFLAGS"
    saved_ldflags="$LDFLAGS"
    saved_libs=$LIBS
    LIBS="$LIBS -llcms"
    CFLAGS="$CFLAGS $all_includes"
    LDFLAGS="$LDFLAGS $all_libraries"
    AC_TRY_LINK([
#define inline __inline /* gcc is in ansi mode */
#include LCMS_HEADER
#if LCMS_VERSION < 112
choke!
#endif
], [
cmsOpenProfileFromFile("foo", "r");
],
        [LCMS_LIBS='-llcms'; have_lcms='yes'])
    LIBS=$saved_libs
    CFLAGS=$saved_cflags
    LDFLAGS=$saved_ldflags
fi

if test -z "$LCMS_LIBS"; then
    DO_NOT_COMPILE="$DO_NOT_COMPILE digikam"
fi

AC_SUBST(LCMS_LIBS)

#------------------------------------------------------------------
#
# Check for libgphoto2
#
#------------------------------------------------------------------

AC_PATH_PROG(GPHOTO_CONFIG,gphoto2-config)
AC_PATH_PROG(GPHOTO_PORT_CONFIG,gphoto2-port-config)
if test -n "${GPHOTO_CONFIG}"; then
  GPHOTO_CFLAGS="`$GPHOTO_CONFIG --cflags`"
  AC_SUBST(GPHOTO_CFLAGS)
  LIB_GPHOTO="`$GPHOTO_CONFIG --libs` `$GPHOTO_PORT_CONFIG --libs`"
  AC_SUBST(LIB_GPHOTO)
else
  AC_MSG_WARN([gPhoto2 not found.])
  DO_NOT_COMPILE="digikam $DO_NOT_COMPILE"
fi

CXXFLAGS="$CXXFLAGS -DQT_CLEAN_NAMESPACE"

#------------------------------------------------------------------
#
# Check for libkipi 
#
#------------------------------------------------------------------

if test "$PKGCONFIGFOUND" = "yes" ; then
   have_libkipi=no

   KDE_PKG_CHECK_MODULES(LIBKIPI, libkipi >= 0.1.5,
	 	         have_libkipi=yes, have_libkipi=no)

   if test "x$have_libkipi" = "xno"; then
	LIBKIPI_CFLAGS=""
	LIBKIPI_LIBS=""
        AC_MSG_RESULT([not found])
   else
	AC_MSG_RESULT([found])
   fi
else
   LIBKIPI_CFLAGS=""
   LIBKIPI_LIBS=""
   AC_MSG_RESULT([not found])
fi 
AC_SUBST(LIBKIPI_CFLAGS)
AC_SUBST(LIBKIPI_LIBS)

if test "x$have_libkipi" != "xyes"; then
     DO_NOT_COMPILE="$DO_NOT_COMPILE digikam"
fi

# --------------------------------------------------------------------
#
# Check for libkexiv2
#
# --------------------------------------------------------------------

if test "$PKGCONFIGFOUND" = "yes" ; then
   have_libkexiv2=no

   KDE_PKG_CHECK_MODULES(LIBKEXIV2, libkexiv2 >= 0.1.6,
                         have_libkexiv2=yes, have_libkexiv2=no)

   if test "x$have_libkexiv2" = "xno"; then
       LIBKEXIV2_CFLAGS=""
       LIBKEXIV2_LIBS=""
       AC_MSG_RESULT([not found])
   else
       AC_MSG_RESULT([found])
   fi
else
   LIBKEXIV2_CFLAGS=""
   LIBKEXIV2_LIBS=""
   AC_MSG_RESULT([not found])
fi 
AC_SUBST(LIBKEXIV2_CFLAGS)
AC_SUBST(LIBKEXIV2_LIBS)

if test "x$have_libkexiv2" != "xyes"; then
   DO_NOT_COMPILE="$DO_NOT_COMPILE digikam"
fi

# --------------------------------------------------------------------
#
# Check for libkdcraw
#
# --------------------------------------------------------------------

if test "$PKGCONFIGFOUND" = "yes" ; then
   have_libkdcraw=no

   KDE_PKG_CHECK_MODULES(LIBKDCRAW, libkdcraw >= 0.1.5,
                         have_libkdcraw=yes, have_libkdcraw=no)

   if test "x$have_libkdcraw" = "xno"; then
       LIBKDCRAW_CFLAGS=""
       LIBKDCRAW_LIBS=""
       AC_MSG_RESULT([not found])
   else
       AC_MSG_RESULT([found])
   fi
else
   LIBKDCRAW_CFLAGS=""
   LIBKDCRAW_LIBS=""
   AC_MSG_RESULT([not found])
fi 
AC_SUBST(LIBKDCRAW_CFLAGS)
AC_SUBST(LIBKDCRAW_LIBS)

if test "x$have_libkdcraw" != "xyes"; then
   DO_NOT_COMPILE="$DO_NOT_COMPILE digikam"
fi

#------------------------------------------------------------------
#
# Check for libtiff
#
#------------------------------------------------------------------

have_tiff=no
KDE_CHECK_LIB(tiff, TIFFWriteScanline,
  have_tiff=yes,
  AC_MSG_WARN([TIFF library not found]), 
  -ljpeg -lz -lm)

if test "x$have_tiff" = "xyes"; then
  KDE_CHECK_HEADER(tiffio.h, have_tiff=yes, have_tiff=no)
fi

if test "x$have_tiff" != "xyes"; then
  AC_WARN([TIFF library not found, digiKam will not be compiled.])
  DO_NOT_COMPILE="digikam $DO_NOT_COMPILE"
else
  LIB_TIFF="-ltiff"
  AC_SUBST(LIB_TIFF)
fi

#------------------------------------------------------------------
#
# Check for libpng (with png_set_add_alpha() function)
#
#------------------------------------------------------------------

have_png=no
KDE_CHECK_LIB(png, png_set_add_alpha,
        have_png=yes,
        AC_MSG_WARN([digiKam requires libpng >= 1.2.7]),
        -lpng -lz -lm)

if test "x$have_png" != "xyes"; then
  AC_WARN([digiKam requires libpng >= 1.2.7; digiKam will not be compiled.])
  DO_NOT_COMPILE="digikam $DO_NOT_COMPILE"
else
  LIB_PNG="-lpng"
  AC_SUBST(LIB_PNG)
fi

#------------------------------------------------------------------
#
# Check for libjasper (JPEG2000)
#
#------------------------------------------------------------------

have_jasper=no
KDE_CHECK_LIB(jasper, jas_init,
              have_jasper=yes,
              AC_MSG_WARN([digiKam requires libjasper >= 1.7.0]),
              -ljasper)

if test "x$have_jasper" != "xyes"; then
  AC_WARN([digiKam requires libjasper >= 1.7.0; digiKam will not be compiled.])
  DO_NOT_COMPILE="digikam $DO_NOT_COMPILE"
else
  LIB_JASPER="-ljasper"
  AC_SUBST(LIB_JASPER)
fi

#------------------------------------------------------------------
#
# Check for local/shared sqlite3
#
#------------------------------------------------------------------

LIB_SQLITE3=""

AC_ARG_WITH(included-sqlite3,
    AC_HELP_STRING([--without-included-sqlite3],[build digiKam using system sqlite3 library]),
    [included_sqlite3=$withval],
    [included_sqlite3=yes]
)

if test x$included_sqlite3 = xno; then
    if test x$PKGCONFIGFOUND = xyes; then
        PKG_CHECK_MODULES(SQLITE, sqlite3 >= 3.5, have_sqlite3=yes,have_sqlite3=no)

        if test x$have_sqlite3 = xyes; then
            ## AC_DEFINE(HAVE_SQLITE3, 1, [have SQLite3 database library])
            LIB_SQLITE3=`pkg-config --libs sqlite3`
        else
            # We don't support not having sqlite3 anymore
            DO_NOT_COMPILE="digikam $DO_NOT_COMPILE"
        fi
    fi
fi

AC_SUBST(LIB_SQLITE3)
AM_CONDITIONAL(with_included_sqlite3, [test x$included_sqlite3 = xyes])

###############################################################################
# END SQLITE CHECK
###############################################################################


#------------------------------------------------------------------
#
# NFS is Evil (sqlite makes use of file locking for allowing 
# multiple processes to access the database. but on many 
# nfs implementations, this file locking is horribly broken and
# can end up locking the app or not allowing access to the app. 
# since we use kioslaves which access the db too)
#
#------------------------------------------------------------------

AC_ARG_ENABLE(nfs-hack,
	AC_HELP_STRING([--enable-nfs-hack],
[Enable a hack for album libraries on a nfs mount, 
 which causes the database to be saved in 
 $HOMEDIR/.kde/share/apps/digikam/directoryname.db [default=disable]]),
 [enable_nfs_hack=$enableval],
 [enable_nfs_hack=no]
)

if test "x$enable_nfs_hack" = "xyes"; then
   AC_DEFINE(NFS_HACK, 1, [NFS hack enabled])
   AC_MSG_NOTICE([NFS hack enabled. Make sure you know what you are doing])
fi

#------------------------------------------------------------------
#
# get the gcc version
#
# CImg.h version 1.2.8 do not compile fine with gcc 4.3.x
# See B.K.O #163118: digikam-0.9.4_beta5 compilation hangs with gcc 4.3
# Using -fno-tree-pre is work around this problem.
#
#------------------------------------------------------------------

KDE_CHECK_COMPILER_FLAG(fno-tree-pre,[CXXFLAGS="-fno-tree-pre $CXXFLAGS"])

