# C linkage is default
AC_C_BIGENDIAN
AC_PROG_YACC
AM_PROG_LEX
dnl AC_OUTPUT(kst/kst.spec)


# PIOLIB Test
ac_piolib_includes=""
ac_piolib_libraries=""
AC_ARG_WITH(piolib,
    AC_HELP_STRING([--with-piolib=DIR],[where the root of PIOLib is installed ]),
    [  ac_piolib_includes="-I$withval"
       ac_piolib_libraries="-L$withval"
    ])

all_includes_save="$all_includes"
all_includes="$all_includes $ac_piolib_includes"
KDE_CHECK_HEADER(HL2_PIOLIB/PIOLib.h, AC_DEFINE(HAVE_PIOLIB_H, 1, [If we have piolib headers]) have_piolib_h=yes PIOINCLUDES="$ac_piolib_includes", , )
all_includes="$all_includes_save"

ldflags_save="$LDFLAGS"
LDFLAGS="$LDFLAGS $ac_piolib_libraries"
KDE_CHECK_LIB(piolib, PIOGetObjectList, PIOLIBS="$ac_piolib_libraries -lpiolib $PIOLIBS", , -lm -lpthread)
LDFLAGS="$ldflags_save"

if test $ac_cv_lib_piolib_PIOGetObjectList = yes; then
  if test $have_piolib_h = yes; then
    AC_SUBST(PIOLIBS)
    AC_SUBST(PIOINCLUDES)
    AC_DEFINE(KST_HAVE_PLANCK, 1, [If we have the PLANCK I/O libraries.])
  fi
fi

AM_CONDITIONAL(include_piolib, test -n "$PIOLIBS")


# CDF Test
ac_cdf_includes=""
ac_cdf_libraries=""
AC_ARG_WITH(cdf,
    AC_HELP_STRING([--with-cdf=DIR],[where the root of CDF is installed ]),
    [  ac_cdf_includes="-I$withval/include"
       ac_cdf_libraries="-L$withval/lib"
    ])

all_includes_save="$all_includes"
all_includes="$all_includes $ac_cdf_includes"
KDE_CHECK_HEADER(cdf.h, AC_DEFINE(HAVE_CDF_H, 1, [If we have CDF headers]) have_cdf_h=yes CDFINCLUDES="$ac_cdf_includes", , )
all_includes="$all_includes_save"

ldflags_save="$LDFLAGS"
LDFLAGS="$LDFLAGS $ac_cdf_libraries"
KDE_CHECK_LIB(cdf, CDFlib, CDFLIBS="$ac_cdf_libraries -lcdf $CDFLIBS", , )
LDFLAGS="$ldflags_save"

if test $ac_cv_lib_cdf_CDFlib = yes; then
  if test $have_cdf_h = yes; then
    AC_SUBST(CDFLIBS)
    AC_SUBST(CDFINCLUDES)
    AC_DEFINE(KST_HAVE_CDF, 1, [If we have the CDF libraries.])
  fi
fi

AM_CONDITIONAL(include_cdf, test -n "$CDFLIBS")


# GSL Tests
GSL_LIBS="-lgsl -lgslcblas -lm"
AC_SUBST(GSL_LIBS)
GSL_NOISE_SUBDIR="gsl"
GSL_FITS_SUBDIR="gsl"
GSL_FITSNONLINEAR_SUBDIR="gsl"
GSL_INTERPOLATIONS_SUBDIR="gsl"
GSL_PASS_FILTERS_SUBDIR="gsl"
GSL_CONVOLUTION_SUBDIR="gsl"
GSL_CORRELATION_SUBDIR="gsl"
KDE_CHECK_HEADER(gsl/gsl_rng.h, ,[GSL_NOISE_SUBDIR=""] )
KDE_CHECK_LIB(gsl, gsl_rng_alloc, ,[GSL_NOISE_SUBDIR=""], -lgslcblas -lm )
KDE_CHECK_HEADER(gsl/gsl_fit.h, ,[GSL_FITS_SUBDIR=""] )
KDE_CHECK_LIB(gsl, gsl_fit_linear, ,[GSL_FITS_SUBDIR=""], -lgslcblas -lm )
KDE_CHECK_HEADER(gsl/gsl_multifit_nlin.h, ,[GSL_FITSNONLINEAR_SUBDIR=""] )
KDE_CHECK_LIB(gsl, gsl_multifit_fdfsolver_alloc, ,[GSL_FITSNONLINEAR_SUBDIR=""], -lgslcblas -lm )
KDE_CHECK_HEADER(gsl/gsl_spline.h, ,[GSL_INTERPOLATIONS_SUBDIR=""] )
KDE_CHECK_LIB(gsl, gsl_interp_init, ,[GSL_INTERPOLATIONS_SUBDIR=""], -lgslcblas -lm )
KDE_CHECK_HEADER(gsl/gsl_fft_real.h, ,[GSL_PASS_FILTERS_SUBDIR=""] )
KDE_CHECK_LIB(gsl, gsl_fft_real_wavetable_alloc, ,[GSL_PASS_FILTERS_SUBDIR=""], -lgslcblas -lm )
KDE_CHECK_HEADER(gsl/gsl_fft_real.h, ,[GSL_CONVOLUTION_SUBDIR=""] )
KDE_CHECK_LIB(gsl, gsl_fft_real_wavetable_alloc, ,[GSL_CONVOLUTION_SUBDIR=""], -lgslcblas -lm )
KDE_CHECK_HEADER(gsl/gsl_fft_real.h, ,[GSL_CORRELATION_SUBDIR=""] )
KDE_CHECK_LIB(gsl, gsl_fft_real_wavetable_alloc, ,[GSL_CORRELATION_SUBDIR=""], -lgslcblas -lm )
AM_CONDITIONAL(include_noise_plugins, test -n "$GSL_NOISE_SUBDIR")
AM_CONDITIONAL(include_fits_plugins, test -n "$GSL_FITS_SUBDIR")
AM_CONDITIONAL(include_fitsnonlinear_plugins, test -n "$GSL_FITSNONLINEAR_SUBDIR")
AM_CONDITIONAL(include_interpolations_plugins, test -n "$GSL_INTERPOLATIONS_SUBDIR")
AM_CONDITIONAL(include_pass_filters_plugins, test -n "$GSL_PASS_FILTERS_SUBDIR")
AM_CONDITIONAL(include_convolution_plugins, test -n "$GSL_CONVOLUTION_SUBDIR")
AM_CONDITIONAL(include_correlation_plugins, test -n "$GSL_CORRELATION_SUBDIR")

FITSIO_LIBS="-lcfitsio -lm"
AC_SUBST(FITSIO_LIBS)
FITSIO_SUBDIR="fitsio"
KDE_CHECK_HEADER(fitsio.h, ,[FITSIO_SUBDIR=""] )
KDE_CHECK_LIB(cfitsio, fftopn, ,[FITSIO_SUBDIR=""], -lm )
AM_CONDITIONAL(include_fitsio_datasource, test -n "$FITSIO_SUBDIR")

AC_DEFUN([KST_CHECK_MDI],
[
  AC_MSG_CHECKING(whether to use kmdi lib from kdelibs)
  AC_CACHE_VAL(ac_cv_mdi_setup,
  [
    AC_LANG_SAVE
    AC_LANG_CPLUSPLUS
    save_CXXFLAGS="$CXXFLAGS"
    CXXFLAGS="$KDE_INCLUDES $QT_INCLUDES"

    AC_TRY_LINK([
      #include <kdeversion.h>
    ],
    [
      #if KDE_VERSION < ((3<<16) | (2<<8) | (0))
         KDE_choke me
      #endif
    ],
      ac_cv_mdi_setup=yes,
      ac_cv_mdi_setup=no
    )
    CXXFLAGS="$save_CXXFLAGS"
    AC_LANG_RESTORE
   ])

  AM_CONDITIONAL(include_libkstkmdi, test "$ac_cv_mdi_setup" = "no")
  if test "$ac_cv_mdi_setup" = "yes"; then
    LIB_KMDI="-lkmdi"
    KMDI_INCLUDES=""
    AC_MSG_RESULT(yes)
  else
    LIB_KMDI='$(top_builddir)/kst/kst/libkstkmdi/libkstkmdi.la'
    KMDI_INCLUDES='-I$(top_srcdir)/kst/kst/libkstkmdi'
    CXXFLAGS="$CXXFLAGS -DCOMPAT_KMDI"
    AC_MSG_RESULT(no)
  fi

  AC_SUBST(LIB_KMDI)
  AC_SUBST(KMDI_INCLUDES)
])

KST_CHECK_MDI

AC_DEFUN([KST_CHECK_CALENDARSYSTEM],
[
  AC_MSG_CHECKING(whether to use kcalendarsystem lib from kdelibs)
  AC_CACHE_VAL(ac_cv_calendarsystem_setup,
  [
    AC_LANG_SAVE
    AC_LANG_CPLUSPLUS
    save_CXXFLAGS="$CXXFLAGS"
    CXXFLAGS="$KDE_INCLUDES $QT_INCLUDES"

    AC_TRY_LINK([
      #include <kdeversion.h>
    ],
    [
      #if KDE_VERSION < ((3<<16) | (2<<8) | (0))
         KDE_choke me
      #endif
    ],
      ac_cv_calendarsystem_setup=yes,
      ac_cv_calendarsystem_setup=no
    )
    CXXFLAGS="$save_CXXFLAGS"
    AC_LANG_RESTORE
   ])

  AM_CONDITIONAL(include_libkstkcalendarsystem, test "$ac_cv_calendarsystem_setup" = "no")
  if test "$ac_cv_calendarsystem_setup" = "yes"; then
    LIB_KCALENDARSYSTEM=""
    KCALENDARSYSTEM_INCLUDES=""
    AC_MSG_RESULT(yes)
  else
    LIB_KCALENDARSYSTEM='$(top_builddir)/kst/kst/libkstkcalendarsystem/libkstkcalendarsystem.la'
    KCALENDARSYSTEM_INCLUDES='-I$(top_srcdir)/kst/kst/libkstkcalendarsystem'
    CXXFLAGS="$CXXFLAGS -DCOMPAT_KCALENDARSYSTEM"
    AC_MSG_RESULT(no)
  fi

  AC_SUBST(LIB_KCALENDARSYSTEM)
  AC_SUBST(KCALENDARSYSTEM_INCLUDES)
])

KST_CHECK_CALENDARSYSTEM

AC_DEFUN([KST_CHECK_JSE],
[
  AC_MSG_CHECKING(whether to compile the Kst JS extension)
  AC_CACHE_VAL(ac_cv_kjs_setup,
  [
    AC_LANG_SAVE
    AC_LANG_CPLUSPLUS
    save_CXXFLAGS="$CXXFLAGS"
    CXXFLAGS="$KDE_INCLUDES $QT_INCLUDES"
    save_LDFLAGS="$LDFLAGS"
    LDFLAGS="$LDFLAGS -L$KDEDIR/lib -lkjsembed"

    AC_TRY_LINK([
      #include <kdeversion.h>
      #include <kjsembed/jssecuritypolicy.h>
    ],
    [
      #if KDE_VERSION < ((3<<16) | (2<<8) | (0))
         KDE_choke me
      #endif
    ],
      ac_cv_kjs_setup=yes,
      ac_cv_kjs_setup=no
    )
    LDFLAGS="$save_LDFLAGS"
    CXXFLAGS="$save_CXXFLAGS"
    AC_LANG_RESTORE
   ])

  AM_CONDITIONAL(include_kjse, test "$ac_cv_kjs_setup" = "yes")
  if test "$ac_cv_kjs_setup" = "yes"; then
    AC_MSG_RESULT(yes)
  else
    AC_MSG_RESULT(no)
  fi
])

KST_CHECK_JSE

KDE_CHECK_HEADER(linux/vt.h, AC_DEFINE(HAVE_LINUX, 1, [If we have a Linux platform]) have_linux=yes, , )
AM_CONDITIONAL(include_linux, test $have_linux = yes)

