dnl Process this file with autoconf to produce a configure script.
AC_INIT(camstream.cc)

dnl AC_CONFIG_HEADER(config.h)

dnl Checks for programs.
AC_PROG_CC
AC_PROG_CPP
AC_PROG_CXX
AC_PROG_CXXCPP
AC_PROG_GCC_TRADITIONAL

dnl Check for Qt preprocessor 'moc'
AC_PATH_PROG(MOC, moc, /usr/local/bin/moc, $QTDIR/bin:$PATH:/usr/bin:/usr/X11R6/bin:/usr/lib/qt/bin:/usr/lib/qt2/bin:/usr/lib/qt3/bin:/usr/local/qt/bin)

dnl Check for Qt GUI compiler 'uic'
AC_PATH_PROG(UIC, uic, /usr/local/bin/uic, $QTDIR/bin:$PATH:/usr/bin:/usr/X11R6/bin:/usr/lib/qt/bin:/usr/lib/qt2/bin:/usr/lib/qt3/bin:/usr/local/qt/bin)

dnl Look in various places for Qt header files and libraries
AC_CACHE_CHECK(location of Qt header files,
  ac_cv_qt_inc, [
  if test -f $QTDIR/include/qglobal.h; then
    ac_cv_qt_inc=$QTDIR/include
  elif test -f /usr/lib/qt2/include/qglobal.h; then
    ac_cv_qt_inc=/usr/lib/qt2/include
  elif test -f /usr/lib/qt3/include/qglobal.h; then
    ac_cv_qt_inc=/usr/lib/qt3/include
  elif test -f /usr/local/include/qt/qglobal.h; then
    ac_cv_qt_inc=/usr/local/include/qt
  elif test -f /usr/include/qt/qglobal.h; then
    ac_cv_qt_inc=/usr/include/qt
  fi
]);

if test "$ac_cv_qt_inc" = ""; then
  echo "not found. Giving up."
  exit
fi

dnl Look for Qt library files
AC_CACHE_CHECK(location of Qt library,
  ac_cv_qt_lib, [
  for T in $QTDIR/lib $QTDIR /usr/lib/qt2 /usr/lib/qt2/lib /usr/lib/qt3 /usr/lib/qt3/lib /usr/local/lib/qt; do
     if test "x$ac_cv_qt_lib" = "x"; then
       if test -f $T/libqt.so -o -f $T/libqt.a; then
         ac_cv_qt_lib=$T;
         qt_mt=no;
         qt_libname="qt";
       elif test -f $T/libqt3.a; then
         ac_cv_qt_lib=$T;
         qt_mt=no;
         qt_libname="qt3";
       elif test -f $T/libqt2.a; then
         ac_cv_qt_lib=$T;
         qt_mt=no;
         qt_libname="qt2";
       elif test -f $T/libqt-mt.so -o -f $T/libqt-mt.a; then
         ac_cv_qt_lib=$T;
         qt_mt=yes;
         qt_libname="qt-mt";
       elif test -f $T/libqt-mt3.a; then
         ac_cv_qt_lib=$T;
         qt_mt=yes;
         qt_libname="qt-mt3";
       elif test -f $T/libqt-mt2.a; then
         ac_cv_qt_lib=$T;
         qt_mt=yes;
         qt_libname="qt-mt2";
       fi
     fi
  done
]);

if test "x$ac_cv_qt_lib" = "x"; then
  echo "not found. Giving up."
  exit
fi

AC_CACHE_VAL(ac_cv_qt_libname,
   ac_cv_qt_libname="$qt_libname"
)

AC_CACHE_CHECK(if Qt is multi-threaded,
  ac_cv_qt_mt,
  ac_cv_qt_mt=$qt_mt;
)

if test "$ac_cv_qt_mt" = "yes"; then
  AC_DEFINE(_REENTRANT)
  AC_DEFINE(QT_THREAD_SUPPORT)
fi

CFLAGS="$CFLAGS -I$ac_cv_qt_inc -g -Wall"
CPPFLAGS="$CPPFLAGS -I$ac_cv_qt_inc"
LIBS="-L$ac_cv_qt_lib -l$ac_cv_qt_libname -L/usr/local/lib -L/usr/X11R6/lib"


dnl Check for X libraries.
AC_LANG_C
AC_CHECK_LIB(X11, main)
AC_CHECK_LIB(Xext, main)
AC_CHECK_LIB(Xt, main)
AC_CHECK_LIB(ICE, main)
AC_CHECK_LIB(SM, main)

dnl Find various libraries, like zlib, png & jpeg

AC_CHECK_LIB(z, zlibVersion)
AC_CHECK_LIB(png, main)
AC_CHECK_LIB(jpeg, jpeg_start_compress)

AC_HEADER_STDC
AC_CHECK_HEADERS(fcntl.h sys/ioctl.h unistd.h linux/videodev.h)

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

dnl Checks for library functions.
AC_CHECK_FUNCS(select)

# Look where mkstemp is
AC_CACHE_CHECK(for mkstemp() in unistd.h, 
  ac_cv_mkstemp_in_unistd, [
  AC_EGREP_HEADER(mkstemp, 
	unistd.h, 
	ac_cv_mkstemp_in_unistd=yes,
	ac_cv_mkstemp_in_unistd=no)
]);

if test $ac_cv_mkstemp_in_unistd = yes; then
  AC_DEFINE(INCLUDE_UNISTD_H)
fi

AC_CACHE_CHECK(for mkstemp() in stdlib.h, 
  ac_cv_mkstemp_in_stdlib, [
  AC_EGREP_HEADER(mkstemp, 
	stdlib.h, 
	ac_cv_mkstemp_in_stdlib=yes,
	ac_cv_mkstemp_in_stdlib=no)
]);

if test $ac_cv_mkstemp_in_stdlib = yes; then
  AC_DEFINE(INCLUDE_STDLIB_H)
fi

dnl Tests for Qt library
AC_LANG_CPLUSPLUS

AC_CACHE_CHECK(for Qt library version >= 2.2.0,
  ac_cv_qt_lib_version, [
  AC_TRY_LINK([#include "qglobal.h"], 
    [
#if QT_VERSION < 220
#error Qt library 2.2.0 or greater required.
#endif
    ], 
    ac_cv_qt_lib_version=yes,
    [ echo "not okay; Qt 2.2.0 or greater is required"
      exit ])
  ]
);

AC_CACHE_CHECK(if Qt supports JPEG,
  ac_cv_qt_has_jpeg, [
  AC_TRY_RUN([
#include <stdio.h>
#include <string.h>

#include <qstrlist.h>
#include <qimage.h>

int main()
{
   QStrList list;
   char *frm;
   int jpeg;
   
   jpeg = 1;
   list = QImage::outputFormats();
   for (frm = list.first(); frm != 0; frm = list.next()) {
      if (!strcmp(frm, "JPEG"))
        jpeg = 0;
   }
   return jpeg;
}
    ], 
    ac_cv_qt_has_jpeg=yes,
    ac_cv_qt_has_jpeg=no,
    ac_cv_qt_has_jpeg=no
  )]
);

if test $ac_cv_qt_has_jpeg = no; then
  echo "Warning! Qt was not compiled with JPEG support, or could not determine this."
  echo "Please consider rebuilding the Qt library with -system-jpeg.";
fi




dnl Check for Intel architecture
AC_CACHE_CHECK(for Intel processor(s), ac_cv_flag_intelcpu, [
if [ uname -m | grep -q "i.86" ]; then
  ac_cv_flag_intelcpu=yes
else
  ac_cv_flag_intelcpu=no
fi
])

if test $ac_cv_flag_intelcpu = yes; then
  AC_DEFINE(HAVE_INTELCPU)

  dnl Check for mmx processor
  AC_CACHE_CHECK(for MMX on processor(s), ac_cv_flag_mmx, [
  if grep "^flags.* mmx" /proc/cpuinfo > /dev/null; then
    ac_cv_flag_mmx=yes
  else
    ac_cv_flag_mmx=no
  fi
  ])
  if test $ac_cv_flag_mmx = yes; then
    AC_DEFINE(HAVE_MMX)
  fi
fi


AC_OUTPUT(Makefile codec/Makefile devices/Makefile gui/Makefile video/Makefile lib/Makefile icons/Makefile)

echo ""
echo "Now type 'make', followed by 'make install' as root."
echo ""
echo "If you want to develop or bugfix, run 'make dep' first! You can ignore"
echo "the warnings."
echo ""
