# 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)

AC_CANONICAL_SYSTEM 
AC_ARG_PROGRAM

dnl Automake doc recommends to do this only here. (Janos)
AM_INIT_AUTOMAKE(kdegraphics, "2.1.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

AC_PATH_KDE

dnl Perform program name transformation
AC_ARG_PROGRAM

#MIN_CONFIG
DO_NOT_COMPILE="$DO_NOT_COMPILE"
dnl Image readers/writers
KDE_CHECK_KIMGIO

dnl Checks for header files.
AC_HEADER_DIRENT
AC_HEADER_STDC
AC_CHECK_HEADERS(fcntl.h sys/time.h unistd.h stdlib.h paths.h)
AC_CHECK_FUNCS(usleep setenv unsetenv)

dnl Checks for typedefs, structures, and compiler characteristics.
AC_HEADER_TIME

CXXFLAGS="$CXXFLAGS -DQT_CLEAN_NAMESPACE -DQT_NO_COMPAT -DQT_NO_ASCII_CAST"
dnl KDE_FIND_GPHOTO2 - Find gphoto2 libraries and include files
dnl
dnl Adapted from kdebase/nsplugins/configure.in.in

AC_DEFUN(KDE_FIND_GPHOTO2,
[



# Clear working variables
gphoto2_includes=
gphoto2_libraries=
gpio_includes=
gpio_libraries=



# Process user input to configure
AC_ARG_WITH(kamera,
[  --without-kamera         do not build kamera (gphoto2 required) ],
[if test "$withval" = "no" ; then
  gphoto2_includes=none
  gphoto2_libraries=none
  gpio_includes=none
  gpio_libraries=none
fi])dnl

AC_ARG_WITH(gphoto2-includes,
[  --with-gphoto2-includes=DIR    gphoto2 include files are in DIR],
gphoto2_includes="$withval")

AC_ARG_WITH(gphoto2-libraries,
[  --with-gphoto2-libraries=DIR   gphoto2 libraries are in DIR],
gphoto2_libraries="$withval")

AC_ARG_WITH(gpio-includes,
[  --with-gpio-includes=DIR    gpio include files are in DIR],
gpio_includes="$withval")

AC_ARG_WITH(gpio-libraries,
[  --with-gpio-libraries=DIR   gpio libraries are in DIR],
gpio_libraries="$withval")

AC_MSG_CHECKING(for gPhoto2)


#
# Search for gphoto2 include files.
#
if test "$gphoto2_includes" = ""; then
AC_CACHE_VAL(ac_cv_gphoto2_includes,
[
ac_gphoto2_save_LIBS="$LIBS"
#
LIBS="-lgphoto2 $LIBS"
#
ac_cv_gphoto2_includes="none"
AC_TRY_COMPILE([#include <gphoto2.h>
],[int a;],
[
# gphoto2.h is in the standard search path.
ac_cv_gphoto2_includes=
],
[
# gphoto2.h is not in the standard search path.
# Locate it and put its directory in `gphoto2_includes'
#
for dir in /usr/include /usr/local/include \
           /usr/include/gphoto2 /usr/local/include/gphoto2; do
if test -f "$dir/gphoto2.h"; then
ac_cv_gphoto2_includes="$dir"
break
fi
done
])
#
LIBS="$ac_gphoto2_save_LIBS"
])
gphoto2_includes="$ac_cv_gphoto2_includes"
fi



#
# Search for gpio include files.
#
if test "$gpio_includes" = ""; then
AC_CACHE_VAL(ac_cv_gpio_includes,
[
ac_gpio_save_LIBS="$LIBS"
#
LIBS="-lgpio $LIBS"
#
ac_cv_gpio_includes="none"
AC_TRY_COMPILE([#include <gpio.h>
],[int a;],
[
# gpio.h is in the standard search path.
ac_cv_gpio_includes=
],
[
# gpio.h is not in the standard search path.
# Locate it and put its directory in `gpio_includes'
#
for dir in /usr/include /usr/local/include \
           /usr/include/gpio /usr/local/include/gpio; do
if test -f "$dir/gpio.h"; then
ac_cv_gpio_includes="$dir"
break
fi
done
])
#
LIBS="$ac_gpio_save_LIBS"
])
gpio_includes="$ac_cv_gpio_includes"
fi                                                                              



#
# Search for libgphoto2
#
if test "$gphoto2_libraries" = ""; then
AC_CACHE_VAL(ac_cv_gphoto2_libraries,
[
ac_gphoto2_save_LIBS="$LIBS"
#
LIBS="-lgphoto2 $LIBS"
#
ac_cv_gphoto2_libraries="none"
AC_TRY_LINK([#include <gphoto2.h>],[gp_init(0);],
[
# libgphoto2 is in the standard search path.
ac_cv_gphoto2_libraries=
],
[
# libgphoto2 is not in the standard search path.
# Locate it and put its directory in `gphoto2_libraries'

for dir in /usr/lib /usr/local/lib; do
if test -d "$dir" && test "`ls $dir/libgphoto2.* 2> /dev/null`" != ""; then
ac_cv_gphoto2_libraries="$dir"
break
fi
done
])
#
LIBS="$ac_gphoto2_save_LIBS"
])
#
gphoto2_libraries="$ac_cv_gphoto2_libraries"
fi


#
# Search for libgpio
#
if test "$gpio_libraries" = ""; then
AC_CACHE_VAL(ac_cv_gpio_libraries,
[
ac_gpio_save_LIBS="$LIBS"
#
LIBS="-lgpio $LIBS"
#
ac_cv_gpio_libraries="none"
AC_TRY_LINK([#include <gpio.h>],[gpio_init(0);],
[
# libgpio is in the standard search path.
ac_cv_gpio_libraries=
],
[
# libgpio is not in the standard search path.
# Locate it and put its directory in `gpio_libraries'
 
for dir in /usr/lib /usr/local/lib; do
if test -d "$dir" && test "`ls $dir/libgpio.* 2> /dev/null`" != ""; then
ac_cv_gpio_libraries="$dir"
break
fi
done
])
#
LIBS="$ac_gpio_save_LIBS"
])
#
gpio_libraries="$ac_cv_gpio_libraries"
fi                                                                              



# Intialise compiler and linker flag variables for export
if test "$gphoto2_includes" = "none" -o "$gphoto2_libraries" = "none" -o \
        "$gpio_includes" = "none" -o "$gpio_libraries" = "none"; then
        with_kamera="no"
else
        with_kamera="yes"

        if test "$gpio_libraries" = ""; then
                GPIO_LIBS="-lgpio"
        else
                GPIO_LIBS="-L$gpio_libraries -lgpio"
        fi

        if test "$gphoto2_libraries" = ""; then
                GPHOTO2_LIBS="-lgphoto2"
        else
                GPHOTO2_LIBS="-L$gphoto2_libraries -lgphoto2"
        fi


        if test "$gphoto2_includes" != ""; then
                GPHOTO2_INCS="-I$gphoto2_includes"
        fi

        if test "$gpio_includes" != ""; then
                GPIO_INCS="-I$gpio_includes"
        fi

	AC_DEFINE(HAVE_GPHOTO2,1,[Define if you have gPhoto2 installed])
fi



# Export compiler and linker flags for replacement in Makefile
AC_SUBST(GPHOTO2_INCS)
AC_SUBST(GPHOTO2_LIBS)
AC_SUBST(GPIO_INCS)
AC_SUBST(GPIO_LIBS)



# Display results of configuration
gphoto2_libraries_result="$gphoto2_libraries"
gphoto2_includes_result="$gphoto2_includes"
gpio_libraries_result="$gpio_libraries"
gpio_includes_result="$gpio_includes"

test "$gphoto2_libraries" = "" && gphoto2_libraries_result="in default path"
test "$gphoto2_includes" = "" && gphoto2_includes_result="in default path"
test "$gpio_libraries" = "" && gpio_libraries_result="in default path"
test "$gpio_includes" = "" && gpio_includes_result="in default path"

test "$gphoto2_libraries" = "none" && gphoto2_libraries_result="(none)"
test "$gphoto2_includes" = "none" && gphoto2_includes_result="(none)"
test "$gpio_libraries" = "none" && gpio_libraries_result="(none)"
test "$gpio_includes" = "none" && gpio_includes_result="(none)"

AC_MSG_RESULT(
  [gphoto2 libraries $gphoto2_libraries_result, gphoto2 headers $gphoto2_includes_result, gpio libraries $gpio_libraries_result, gpio headers $gpio_includes_result])

]) dnl end of KDE_FIND_GPHOTO2 definition

KDE_FIND_GPHOTO2
if test "$with_kamera" = "no"; then
  DO_NOT_COMPILE="$DO_NOT_COMPILE kamera"
fi

dnl the following is just to fool the toplevel configure.in
LTLIBOBJS=
AC_SUBST(LTLIBOBJS)

AC_ARG_WITH(system-kpathsea,
    [  --with-system-kpathsea  use system kpathsea library],
    [  use_system_kpathsea=$withval
    ], use_system_kpathsea=no)

# the following is repeated here, so it shows up in the top level configure --help
AC_ARG_WITH(tex-datadir,
    [  --with-tex-datadir=DIR  where the root of texmf/web2c/texmf.cnf is installed ],
    [],
    [])

compile_kdvi=yes
for j in $DO_NOT_COMPILE; do
   if test "kdvi" = $j; then
       compile_kdvi=no
   fi
done

dnl AC_CONFIG_SUBDIRS has to be done before KDE_CREATE_SUBDIRSLIST
if test "$compile_kdvi" = "yes"; then

   KDE_FIND_PATH(kpsewhich, KPSEWHICH, [/usr/bin /bin /usr/sbin /opt/teTeX/bin /opt/local/bin /opt/bin], [ ])

   have_kpsewhich=no
   test_kpsewhich="`${KPSEWHICH-kpsewhich} -show-path cnf 2>/dev/null`"
   test -n "${test_kpsewhich}" && have_kpsewhich=yes

   texmf_dir=
   texmf_font_path=
   if test x"${have_kpsewhich}" = xyes
   then
      texmf_cnf_path="`${KPSEWHICH} -show-path cnf 2>/dev/null`"
      texmf_font_path="`${KPSEWHICH} -show-path pk`"
   else
      texmf_cnf_path="/var/lib/texmf/web2c:/usr/share/texmf/web2c:/opt/teTeX/share/texmf/web2c"
      # whatever the default should be
      texmf_font_path="/var/lib/texmf/fonts/pk//"
  fi

  AC_DEFINE_UNQUOTED(KDVI_DEFAULT_FONTPATH, "$texmf_font_path", [Where kdvi should look for fonts])

  texmf_cnf_path="$texmf_cnf_path:/usr/lib/texmf/web2c:/var/lib/texmf/web2c:/usr/share/texmf/web2c:/usr/share/teTeX/texmf/web2c:/opt/local/teTeX/share/texmf/web2c:/opt/teTeX/share/texmf/web2c:/var/texmf/web2c:/usr/local/share/texmf/web2c:/opt/local/share/texmf/web2c:/etc/texmf"

   # this gets the LAST directory in the path, which is assumed to be the
   # base installation directory. This also allows us to overwrite it with
   # some standard paths. So it rather finds the /etc version than the
   # /var/share one
   AC_MSG_CHECKING([for texmf.cnf])
   kde_safe_IFS=$IFS
   IFS=:
   texmf_dir=
   for d in ${texmf_cnf_path}
   do
      if test -r "${d}/texmf.cnf" && test -r "$d/../../texmf/web2c/texmf.cnf"
      then
          texmf_dir="${d}"
      fi
   done
   IFS=$kde_safe_IFS

   if test -n "${texmf_dir}"
   then
    # texmf_dir="`echo "${texmf_dir}" | sed 's,/web2c,,'`"
     AC_MSG_RESULT([$texmf_dir])
   else
     AC_MSG_RESULT("not found")
   fi

   if test ! x"$use_system_kpathsea" = xno; then
      	KDVI_KPATHSEA_DIR=
	if test -f "$use_system_kpathsea"; then
	   KDVI_KPATHSEA_LIB=$use_system_kpathsea
	else
           KDVI_KPATHSEA_LIB=-lkpathsea
 	fi
        KDVI_KPATHSEA_INC=
   else
        KDVI_KPATHSEA_DIR=kpathsea
        KDVI_KPATHSEA_INC=-Ikpathsea
        KDVI_KPATHSEA_LIB=kpathsea/libkpathsea.la

        if test -f $srcdir/kdvi/kpathsea/configure; then
           if test -n "$texmf_dir"; then
             ac_configure_args="$ac_configure_args --with-tex-datadir=$texmf_dir/../.."
             AC_CONFIG_SUBDIRS([kdvi/kpathsea])
           else
              if test $compile_kdvi = "yes"; then
                 DO_NOT_COMPILE="$DO_NOT_COMPILE kdvi"
		 ac_configure_args="$ac_configure_args --with-system-kpathsea=yes" 
              fi
  	   fi
        fi
    fi
fi

AC_SUBST(KDVI_KPATHSEA_DIR)
AC_SUBST(KDVI_KPATHSEA_INC)
AC_SUBST(KDVI_KPATHSEA_LIB)

AC_CHECK_HEADERS(sys/types.h sys/params.h limits.h)

AC_FUNC_MMAP
AC_CHECK_HEADERS(pthread.h)
KDE_CREATE_SUBDIRSLIST
AC_OUTPUT( \
./Makefile \
doc/Makefile \
doc/kdvi/Makefile \
doc/kfax/Makefile \
doc/kfract/Makefile \
doc/kghostview/Makefile \
doc/kiconedit/Makefile \
doc/kpaint/Makefile \
doc/kruler/Makefile \
doc/ksnapshot/Makefile \
doc/kview/Makefile \
doc/pixie/Makefile \
kamera/Makefile \
kamera/kcontrol/Makefile \
kamera/kioslave/Makefile \
kdvi/Makefile \
kdvi/pix/Makefile \
kfax/Makefile \
kfax/libtiffax/Makefile \
kfaxview/Makefile \
kfract/Makefile \
kghostview/Makefile \
kiconedit/Makefile \
kiconedit/pics/Makefile \
kiconedit/pics/icons/Makefile \
kiconedit/pics/toolbar/Makefile \
kpaint/Makefile \
kpaint/images/Makefile \
kpaint/toolpics/Makefile \
kpixmap2bitmap/Makefile \
kruler/Makefile \
kruler/pics/Makefile \
ksnapshot/Makefile \
kview/Makefile \
kview/pics/Makefile \
kviewshell/Makefile \
libminimagick/Makefile \
libminimagick/mini-magick/Makefile \
pixie/Makefile \
pixie/color/Makefile \
pixie/libifplugin/Makefile \
pixie/pics/Makefile \
pixie/plugins/Makefile \
pixie/plugins/kde/Makefile \
pixie/plugins/magick/Makefile \
pixie/thumbnails/Makefile \
)
