AC_INIT(aclocal.m4)
# kmando?
AM_INIT_AUTOMAKE(mando,1.6)
AM_CONFIG_HEADER(config.h)

# Checks for programs.
AM_PROG_LIBTOOL
AC_LANG_CPLUSPLUS

AC_MSG_CHECKING([wether C++ compiler works])
AC_TRY_LINK([],[],[AC_MSG_RESULT([yes])],[AC_MSG_ERROR([Failed to compile and link C++ program.])])

dnl Allow customised pkg-config path
AC_ARG_WITH(pkg-config-path,
   AC_HELP_STRING([--with-pkg-config-path=<dirs>],[colon-separated list of pkg-config(1) dirs]),
   [export PKG_CONFIG_PATH=${withval}])

dnl Switch for debug or release mode.
AC_ARG_ENABLE(debug,
              [  --enable-debug=DEBUG    compile with debugging assertions (default=no)],
              enable_debug=$enableval, enable_debug=no )

dnl Check for X11
AC_PATH_XTRA
AC_SUBST(X_LIBS)

dnl Check for Xtst
old_LIBS="$LIBS"
LIBS="$LIBS $X_LIBS"
AC_CHECK_LIB(Xtst,XTestFakeMotionEvent,
             [],
             [AC_MSG_ERROR(Check for XTestFakeMotionEvent in -lXtst failed)])
LIBS="$old_LIBS"

dnl Check for boost-library.
AC_MSG_CHECKING([for boost])
AC_TRY_CPP([#include <boost/version.hpp>
            #if BOOST_VERSION < 103200
            #error "Boost version 1.32 or greater required."
            #endif],
             AC_MSG_RESULT([yes]),
             AC_MSG_ERROR([Check for boost library of version >= 1.32 failed.]))

dnl Check for libdc1394 (firewire digital camera library).
AC_MSG_CHECKING([for dc1394])
old_LIBS="$LIBS"
LIBS="$LIBS -ldc1394_control -lraw1394"
AC_TRY_LINK([#include <libdc1394/dc1394_control.h>],
   [dc1394_dma_setup_capture( 0, 0, 0, 0, 0, 0, 0, 0, 0, "", 0 )],
   [AC_MSG_RESULT([yes])]
   [LIBDC1394_OLD="no"]
   [LIBS_DC1394="-ldc1394_control -lraw1394"],
   [AC_MSG_RESULT([no (older than version 1.1?)])]
   [LIBS_DC1394=""])
if test "x$LIBS_DC1394" == "x"; then
   AC_MSG_CHECKING([for old version of dc1394])
   AC_TRY_LINK([#include <libdc1394/dc1394_control.h>],
      [dc1394_dma_setup_capture( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "", 0 )],
      [AC_MSG_RESULT([yes])]
      [LIBDC1394_OLD="yes"]
      [AC_DEFINE(LIBDC1394_OLD,yes,Old version of libdc1394)]
      [LIBS_DC1394="-ldc1394_control -lraw1394"],
      [AC_MSG_ERROR([Failed to link with dc1394 library (only version 1.x supported)])])
fi
LIBS="$old_LIBS"
AC_SUBST(LIBS_DC1394)
if test "x$LIBDC1394_OLD" == "xyes"; then
info="$info
libdc1394-library                    : older than version 1.1"
else
info="$info
libdc1394-library                    : version 1.1 or newer"
fi
AC_DEFINE(HAVE_LIBDC1394,yes,libdc1394 is present)

dnl Check for opengl
MDL_HAVE_OPENGL
HAVE_OPENGL="yes"
AC_MSG_CHECKING([for GL])
if test "x$have_GL" != "xyes"; then
   AC_MSG_RESULT([no])
   HAVE_OPENGL="no"
else    
   AC_MSG_RESULT([yes])
fi
AC_MSG_CHECKING([for GLU])
if test "x$have_GLU" != "xyes"; then
   AC_MSG_RESULT([no])
   HAVE_OPENGL="no"
else    
   AC_MSG_RESULT([yes])
fi
AC_MSG_CHECKING([for glut])
if test "x$have_glut" != "xyes"; then
   AC_MSG_RESULT([no])
   HAVE_OPENGL="no"
else    
   AC_MSG_RESULT([yes])
fi
if test "x$HAVE_OPENGL" != "xyes"; then
   AC_MSG_ERROR([Check for OpenGL failed.])
fi

# Checks for libraries.
dnl Check for Qt library.
PKG_CHECK_MODULES(QT, QtOpenGL >= 4.0.1, HAVE_QT="yes", HAVE_QT="no")
dnl PKG_CHECK_MODULES(QT, QtOpenGL_debug >= 4.0.1, HAVE_QT="yes", HAVE_QT="no")
if test "x$HAVE_QT" = "xyes" ; then
        AC_MSG_RESULT([yes])
        QT_PREFIX="`$PKG_CONFIG --variable=prefix QtCore`/bin"
        AC_CHECK_FILE("$QT_PREFIX/moc-qt4",
                [ QT_MOC=$QT_PREFIX/moc-qt4 ],
                [AC_CHECK_FILE("$QT_PREFIX/moc",
                        [ QT_MOC=$QT_PREFIX/moc ],
                        [ AC_MSG_ERROR([Could not find Qt4 moc.]) ] ) ] )
        AC_SUBST(QT_MOC)
        AC_CHECK_FILE("$QT_PREFIX/uic-qt4",
                [ QT_UIC=$QT_PREFIX/uic-qt4 ],
                [AC_CHECK_FILE("$QT_PREFIX/uic",
                        [ QT_UIC=$QT_PREFIX/uic ],
                        [ AC_MSG_ERROR([Could not find Qt4 uic.]) ] ) ] )
        AC_SUBST(QT_UIC)
        AC_CHECK_FILE("$QT_PREFIX/rcc-qt4",
                [ QT_RCC=$QT_PREFIX/rcc-qt4 ],
                [AC_CHECK_FILE("$QT_PREFIX/rcc",
                        [ QT_RCC=$QT_PREFIX/rcc ],
                        [ AC_MSG_ERROR([Could not find Qt4 rcc.]) ] ) ] )
        AC_SUBST(QT_RCC)
else
   AC_MSG_ERROR([Check for Qt4 failed.])
fi
AC_SUBST(QT_LIBS)
AC_SUBST(QT_CFLAGS)

dnl The blas-test called by the lapack-test will exit the script, if no
dnl g77 or f77 is found.
AC_MSG_CHECKING([for f2c.h])
AC_TRY_CPP([#include <f2c.h>],
           [AC_MSG_RESULT([yes])
            HAVE_F2C="yes"],
           [AC_MSG_RESULT([no])
            HAVE_F2C="no"])
if test "x$HAVE_F2C" == "xyes"; then
        old_LIBS="$LIBS"
dnl For older blas-libraries you may have to use
dnl       ./configure FC=f77
dnl or
dnl       ./configure FC=g77
        AC_PROG_FC
        F77=$FC
        if test "x$F77" != "x"; then
                ACX_LAPACK([AC_DEFINE(HAVE_LIBLAPACK,yes,lapack library)]
                        [HAVE_LIBLAPACK="yes"],
                        [HAVE_LIBLAPACK="no"])
        else
                AC_MSG_WARN([Could not find Fortran compiler. Disabling lapack-support.])
                HAVE_LIBLAPACK="no"
        fi
else
        HAVE_LIBLAPACK="no"
fi
if test "x$HAVE_LIBLAPACK" != "xyes"; then
   AC_MSG_ERROR([Check for Lapack failed.])
fi

dnl Check for fftw3 library (required by fftw.h).
PKG_CHECK_MODULES(FFTW3, fftw3 >= 3.0.0, HAVE_FFTW3="yes", HAVE_FFTW3="no")
PKG_CHECK_MODULES(FFTW3F, fftw3f >= 3.0.0, HAVE_FFTW3F="yes", HAVE_FFTW3F="no")
if test "x$HAVE_FFTW3" == "xyes"; then
   if test "x$HAVE_FFTW3F" == "xyes"; then
        AC_DEFINE(HAVE_LIBFFTW3,yes,fftw library)
   fi
fi
if test "x$HAVE_FFTW3" != "xyes"; then
   AC_MSG_ERROR([Check for fftw3 failed.])
fi
AC_SUBST(FFTW3_LIBS)
AC_SUBST(FFTW3_CFLAGS)
AC_SUBST(FFTW3F_LIBS)
AC_SUBST(FFTW3F_CFLAGS)

# Compiler-flags for debug- or release-mode.
if test "x$enable_debug" = "xyes"; then
        CXXFLAGS='-g $(QT_CFLAGS)'
else
        CXXFLAGS='-O6 -DNDEBUG $(QT_CFLAGS)'
fi

AC_OUTPUT(
        Makefile,
        make clean )