#Process this file with autoconf to produce a configure script.

AC_INIT(configure.in)
AM_INIT_AUTOMAKE(Motiontrack, 0.1.3)
AM_CONFIG_HEADER(config.h)

AC_PREREQ(2.5)
#AC_ISC_POSIX
CFLAGS="$CFLAGS "
#add a space to make these macros not add stupid stuff if it was unset
AC_ISC_POSIX
AC_PROG_CC
AM_PROG_CC_STDC
AC_HEADER_STDC

AC_CHECK_HEADER([math.h])
AC_CHECK_LIB([m],[pow],,[AC_MSG_ERROR([[Math library not found!]])])

AC_ARG_ENABLE([debug],[
AC_HELP_STRING([--enable-debug],
      [enable debug symbols and disable optimization,
         (only for GCC, set per-compiler options via $CFLAGS
         environment variable, when using a different C compiler)
         and activate additional debug output]
   )],[
      if test "x$enableval" = "xno"; then
         USE_DEBUG="no"
      else
         USE_DEBUG="yes"
      fi
   ],[
      USE_DEBUG="undef"
])

AC_ARG_ENABLE([gd],[
AC_HELP_STRING([--enable-gd],
      [use GD for image I/O,
       this is the most optimized code and a default option]
   )],[
      if test "x$enableval" = "xno"; then
         USE_GD="no"
      else
         USE_GD="yes"
      fi
   ],[
      USE_GD="undef"
])

AC_ARG_ENABLE([magick],[
AC_HELP_STRING([--enable-magick],
      [use ImageMagick for image I/O, this might be slower than GD]
   )],[
      if test "x$enableval" = "xno"; then
         USE_MAGICK="no"
      else
         USE_MAGICK="yes"
      fi
   ],[
      USE_MAGICK="undef"
])

AC_ARG_ENABLE([mg-config],[
AC_HELP_STRING([--disable-mg-config],
      [do not use the Magick-config utility to check for ImageMagick header
         and library path, this option might be crucial for
	 cross-compilation when local copy is unusable.]
   )],[
      if test "x$enableval" = "xno"; then
         USE_MG_CONFIG="no"
      else
         USE_MG_CONFIG="yes"
      fi
   ],[
      USE_MG_CONFIG="undef"   
])

AC_ARG_ENABLE([cluster],[
AC_HELP_STRING([--enable-cluster],
      [optimize code to be able to fork into more than one process.
         While creating additional overhead when splitting image-data,
	 this may increase performance on multi-cpu machines or
	 openmosix-like clusters.]
   )],[
      if test "x$enableval" = "xno"; then
         USE_CLUSTER="no"
      else
         USE_CLUSTER="yes"
      fi
   ],[
      USE_CLUSTER="undef"
])

AC_ARG_ENABLE([speedhack],[
AC_HELP_STRING([--disable-speedhack],
      [do not activate the speed enhancement features of the image I/O
         facility. This lets the I/O library (GD or ImageMagick) do all
	 pixel read and write operation and disables use of motiontracks
	 own pixelcache when used with ImageMagick. You can try this, if
	 motiontrack segfaults, but dont forget to give me an error
	 report in that case.]
   )],[
      if test "x$enableval" = "xno"; then
         USE_SPEEDHACK="no"
	 if test "x$USE_CLUSTER" = "xyes"; then
	    USE_CLUSTER="no"
            AC_MSG_WARN([[Cluster code disabled, no speedhack!]])
	 fi
      else
         USE_SPEEDHACK="yes"
      fi
   ],[
      USE_SPEEDHACK="undef"   
])


AC_ARG_WITH([searchdirs],[
AC_HELP_STRING([--with-searchdirs=PATH],
      [add DIR, DIR/lib and dir/include to the include and library searchpath,
         for each DIR in PATH=DIR:DIR:...]
   )],[
      for dir in $( echo ${withval} | sed -e 's/:/\t/g' ); do
         CPPFLAGS="$INCDIRS -I${dir} -I${dir}/include"
	 LDFLAGS="$LIBDIRS -L${dir} -L${dir}/lib"
      done
])

AC_CHECK_HEADER([png.h])
AC_CHECK_LIB([png],[png_start_read_image],,[
   AC_MSG_NOTICE([[testing libpng with dynamic zlib]])
   AC_CHECK_LIB([z],[deflate])
   AC_CHECK_LIB([png],[png_write_IDAT])
])

AC_CHECK_HEADER([jpeglib.h])
AC_CHECK_LIB([jpeg],[jpeg_start_decompress],,[
   AC_MSG_NOTICE([[testing jpeg with dynamic zlib...]])
   AC_CHECK_LIB([z],[deflate])
   AC_CHECK_LIB([jpeg],[jpeg_set_defaults])
])

AC_DEFUN([CHECK_FOR_GD],[
   GD_HEADER="yes"
   AC_CHECK_HEADER([gd.h],,[
      AC_MSG_WARN([[GD header not found!]])
      GD_HEADER="no"
      
   ])
   AC_CHECK_LIB([gd],[gdImageCreate],,[
      AC_MSG_WARN([[GD library not found!]])
   ])
   FORMAT_COUNT=0
   RFORMATS=""
   WFORMATS=""
   AC_CHECK_LIB([gd],[gdImageCreateFromPng],[
      AC_MSG_NOTICE([[:-)]])
      PNG_R_WORKS="yes"
      true $(( FORMAT_COUNT++ ));
      RFORMATS="PNG $RFORMATS"
   ],[
      AC_MSG_NOTICE([[GD doesnt support png read!]])
      PNG_R_WORKS="no"
   ])
   AC_CHECK_LIB([gd],[gdImageCreateFromJpeg],[
      AC_MSG_NOTICE([[:-)]])
      JPG_R_WORKS="yes"
      true $(( FORMAT_COUNT++ ));
      RFORMATS="JPG $RFORMATS"
   ],[
      AC_MSG_NOTICE([[GD doesnt support jpg read!]])
      JPG_R_WORKS="no"
   ])
   AC_CHECK_LIB([gd],[gdImageCreateFromWBMP],[
      AC_MSG_NOTICE([[:-)]])
      WBMP_R_WORKS="yes"
      true $(( FORMAT_COUNT++ ));
      RFORMATS="WBMP $RFORMATS"
   ],[
      AC_MSG_NOTICE([[GD doesnt support wmbp read!]])
      WBMP_R_WORKS="no"
   ])
   AC_CHECK_LIB([gd],[gdImageCreateFromGif],[
      AC_MSG_NOTICE([[:-)]])
      GIF_R_WORKS="yes"
      true $(( FORMAT_COUNT++ ));
      RFORMATS="GIF $RFORMATS"
   ],[
      AC_MSG_NOTICE([[GD doesnt support gif read!]])
      GIF_R_WORKS="no"
   ])
   AC_CHECK_LIB([gd],[gdImageCreateFromGd],[
      AC_MSG_NOTICE([[:-)]])
      DGD_R_WORKS="yes"
      true $(( FORMAT_COUNT++ ));
      RFORMATS="GD $RFORMATS"
   ],[
      AC_MSG_NOTICE([[GD doesnt support Gd read!]])
      DGD_R_WORKS="no"
   ])
   AC_CHECK_LIB([gd],[gdImageCreateFromGd2],[
      AC_MSG_NOTICE([[:-)]])
      DGD2_R_WORKS="yes"
      true $(( FORMAT_COUNT++ ));
      RFORMATS="GD2 $RFORMATS"
   ],[
      AC_MSG_NOTICE([[GD doesnt support gd2 read!]])
      DGD2_R_WORKS="no"
   ])
   AC_CHECK_LIB([gd],[gdImageCreateFromXpm],[
      AC_MSG_NOTICE([[:-)]])
      XPM_R_WORKS="yes"
      true $(( FORMAT_COUNT++ ));
      RFORMATS="XPM $RFORMATS"
   ],[
      AC_MSG_NOTICE([[GD doesnt support xpm read!]])
      XPM_R_WORKS="no"
   ])
   AC_CHECK_LIB([gd],[gdImageCreateFromXbm],[
      AC_MSG_NOTICE([[:-)]])
      XBM_R_WORKS="yes"
      true $(( FORMAT_COUNT++ ));
      RFORMATS="XBM $RFORMATS"
   ],[
      AC_MSG_NOTICE([[GD doesnt support xbm read!]])
      XBM_R_WORKS="no"
   ])
   AC_CHECK_LIB([gd],[gdImagePng],[
      AC_MSG_NOTICE([[:-)]])
      PNG_W_WORKS="yes"
      true $(( FORMAT_COUNT++ ));
      WFORMATS="PNG $WFORMATS"
   ],[
      AC_MSG_NOTICE([[GD doesnt support png write!]])
      PNG_W_WORKS="no"
   ])
   AC_CHECK_LIB([gd],[gdImageJpeg],[
      AC_MSG_NOTICE([[:-)]])
      JPG_W_WORKS="yes"
      true $(( FORMAT_COUNT++ ));
      WFORMATS="JPG $WFORMATS"
   ],[
      AC_MSG_NOTICE([[GD doesnt support jpg write!]])
      JPG_W_WORKS="no"
   ])
   AC_CHECK_LIB([gd],[gdImageWBMP],[
      AC_MSG_NOTICE([[:-)]])
      WBMP_W_WORKS="yes"
      true $(( FORMAT_COUNT++ ));
      WFORMATS="WBMP $WFORMATS"
   ],[
      AC_MSG_NOTICE([[GD doesnt support wmbp write!]])
      WBMP_W_WORKS="no"
   ])
   AC_CHECK_LIB([gd],[gdImageGif],[
      AC_MSG_NOTICE([[:-)]])
      GIF_W_WORKS="yes"
      true $(( FORMAT_COUNT++ ));
      WFORMATS="GIF $WFORMATS"
   ],[
      AC_MSG_NOTICE([[GD doesnt support gif write!]])
      GIF_W_WORKS="no"
   ])
   AC_CHECK_LIB([gd],[gdImageGd],[
      AC_MSG_NOTICE([[:-)]])
      DGD_W_WORKS="yes"
      true $(( FORMAT_COUNT++ ));
      WFORMATS="GD $WFORMATS"
   ],[
      AC_MSG_NOTICE([[GD doesnt support Gd write!]])
      DGD_W_WORKS="no"
   ])
   AC_CHECK_LIB([gd],[gdImageGd2],[
      AC_MSG_NOTICE([[:-)]])
      DGD2_W_WORKS="yes"
      true $(( FORMAT_COUNT++ ));
      WFORMATS="GD2 $WFORMATS"
   ],[
      AC_MSG_NOTICE([[GD doesnt support gd2 write!]])
      DGD2_W_WORKS="no"
   ])
   if test "x$RFORMAT_COUNT" = "x0" -o "x$WFORMAT_COUNT" = "x0"; then
      GD_WORKS="no"
   else
      GD_WORKS="yes"
   fi
   if test "x$GD_HEADER" = "xno"; then
      GD_WORKS="no"
   fi
])

AC_DEFUN([CHECK_FOR_MAGICK],[
   if test "x$USE_MG_CONFIG" != "xno"; then
      AC_PATH_TOOL([MG_CONFIG],[Magick-config])
      if test -n "$MG_CONFIG"; then
         AC_MSG_CHECKING([[\"$MG_CONFIG --cppflags\"]])
         CPPFLAGS="$CPPFLAGS $( $MG_CONFIG --cppflags )"
	 AC_MSG_RESULT([[$( $MG_CONFIG --cppflags )]])
         AC_MSG_CHECKING([[\"$MG_CONFIG --ldflags\"]])
	 LDFLAGS="$LDFLAGS $( $MG_CONFIG --ldflags )"
	 AC_MSG_RESULT([[$( $MG_CONFIG --ldflags )]])
         AC_MSG_CHECKING([[\"$MG_CONFIG --libs\"]])
	 LIBS="$LIBS $( $MG_CONFIG --libs )"
	 # "
	 AC_MSG_RESULT([[$( $MG_CONFIG --libs )]])
      else
         AC_MSG_WARN([Magick-Config not found!])
      fi
   fi
   MAGICK_HEADER="yes"
   AC_CHECK_HEADER([magick/api.h],,[
	MAGICK_HEADER="no"
	AC_MSG_WARN([[ImageMagick header not found!]])
   ])
   MAGICK_WORKS="yes"
   AC_CHECK_LIB([Magick],[ReadImage],,[
      AC_MSG_WARN([[ImageMagick library not found!]])
      MAGICK_WORKS="no"
   ])
  if test "x$MAGICK_HEADER" = "xno"; then
      MAGICK_WORKS="no"
   fi
   RFORMATS="N/A"
   WFORMATS="N/A
   
! Image format support is handled internally by ImageMagick    !
! and determined on runtime, there is no impact on compilation !"

   AC_CHECK_LIB([Magick],[ExportImagePixels],,[
      AC_MSG_WARN([[ImageMagick doesnt provide direct pixel data access!]])
      USE_SPEEDHACK="no"
      USE_CLUSTER="no"
      WFORMATS="$WFORMATS

!!!WARNING!!!   !!!WARNING!!!   !!!WARNING!!!
---------------------------------------------
!!! Your ImageMagick version is too old   !!!
---------------------------------------------
ImageMagick prior to 5.5.7 doesnt provide
the ExportImagePixels() function, which is
needed for fast pixel data access.
--speedhack has been disabled, but without
motiontrack will run up to 10 times slower
than it should.
Multi thread code is disabled, too.
---------------------------------------------
I advice to upgrade your ImageMagick version
to 5.5.7 or higher, or use GD for image-IO.
---------------------------------------------"
   ])
   
])

TRY_FIRST="GD"

if test "x$USE_GD" = "xno"; then
   if test "x$USE_MAGICK" = "xno"; then
      AC_MSG_WARN([[I have to use an image I/O library, defaulting to GD!]])
      TRY_FIRST="GD"
   else
      TRY_FIRST="MAGICK"
   fi
fi

if test "x$USE_MAGICK" = "xyes"; then
   if test "x$USE_GD" = "xyes"; then
      AC_MSG_WARN([[I can only use 1 image I/O library, defaulting to GD!]])
      TRY_FIRST="GD"
   else
      TRY_FIRST="MAGICK"
   fi
fi

GD_WORKS="no"
MAGICK_WORKS="no"
if test "x$TRY_FIRST" = "xMAGICK"; then
   CHECK_FOR_MAGICK()
   if test "x$MAGICK_WORKS" = "xno"; then
      AC_MSG_WARN([[ImageMagick library did not work, trying to use GD!]])
      CHECK_FOR_GD()
   fi
else
   CHECK_FOR_GD()
   if test "x$GD_WORKS" = "xno"; then
      AC_MSG_WARN([[GD library did not work, trying to use ImageMagick!]])
      CHECK_FOR_MAGICK()
   fi
fi

if test "x$GD_WORKS" = "xno"; then
   if test "x$MAGICK_WORKS" = "xno"; then
      AC_MSG_ERROR([[No useable image I/O libraries have been found!]])
   else
      AC_DEFINE_UNQUOTED(USE_IMAGEMAGICK,,[compile using ImageMagick])
      IMAGE_LIBRARY="ImageMagick"
   fi
else
   AC_DEFINE_UNQUOTED(USE_GD,,[compile using GD])
   IMAGE_LIBRARY="GD"
fi

if test "x$PNG_R_WORKS" = "xyes"; then
   AC_DEFINE_UNQUOTED(USE_PNG_R,,[compile read support for PNG images])
fi
if test "x$JPG_R_WORKS" = "xyes"; then
   AC_DEFINE_UNQUOTED(USE_JPG_R,,[compile read support for JPG images])
fi
if test "x$WBMP_R_WORKS" = "xyes"; then
   AC_DEFINE_UNQUOTED(USE_WBMP_R,,[compile read support for WBMP images])
fi
if test "x$GIF_R_WORKS" = "xyes"; then
   AC_DEFINE_UNQUOTED(USE_GIF_R,,[compile read support for GIF images])
fi
if test "x$DGD_R_WORKS" = "xyes"; then
   AC_DEFINE_UNQUOTED(USE_DGD_R,,[compile read support for GD images])
fi
if test "x$DGD2_R_WORKS" = "xyes"; then
   AC_DEFINE_UNQUOTED(USE_DGD2_R,,[compile read support for GD2 images])
fi
if test "x$XPM_R_WORKS" = "xyes"; then
   AC_DEFINE_UNQUOTED(USE_XPM_R,,[compile read support for XPM images])
fi
if test "x$XBM_R_WORKS" = "xyes"; then
   AC_DEFINE_UNQUOTED(USE_XBM_R,,[compile read support for XBM images])
fi
if test "x$PNG_W_WORKS" = "xyes"; then
   AC_DEFINE_UNQUOTED(USE_PNG_W,,[compile write support for PNG images])
fi
if test "x$JPG_W_WORKS" = "xyes"; then
   AC_DEFINE_UNQUOTED(USE_JPG_W,,[compile write support for JPG images])
fi
if test "x$WBMP_W_WORKS" = "xyes"; then
   AC_DEFINE_UNQUOTED(USE_WBMP_W,,[compile write support for WBMP images])
fi
if test "x$GIF_W_WORKS" = "xyes"; then
   AC_DEFINE_UNQUOTED(USE_GIF_W,,[compile write support for GIF images])
fi
if test "x$DGD_W_WORKS" = "xyes"; then
   AC_DEFINE_UNQUOTED(USE_DGD_W,,[compile write support for GD images])
fi
if test "x$DGD2_W_WORKS" = "xyes"; then
   AC_DEFINE_UNQUOTED(USE_DGD2_W,,[compile write support for GD2 images])
fi

if test "x$USE_DEBUG" = "xyes"; then
   AC_DEFINE_UNQUOTED(USE_DEBUG,,[compile extra debug output])
fi
if test "x$USE_SPEEDHACK" != "xno"; then
   AC_DEFINE_UNQUOTED(USE_SPEEDHACK,,[compile speed optimisations])
fi

if test "x$USE_CLUSTER" = "xyes"; then
   AC_DEFINE_UNQUOTED(USE_CLUSTER,,[compile multi process support])
fi

#Set PACKAGE_DATA_DIR in config.h.
if test "x${datadir}" = 'x${prefix}/share'; then
  if test "x${prefix}" = "xNONE"; then
    AC_DEFINE_UNQUOTED(PACKAGE_DATA_DIR,
       "${ac_default_prefix}/share/${PACKAGE}",[data directory])
  else
    AC_DEFINE_UNQUOTED(PACKAGE_DATA_DIR, "${prefix}/share/${PACKAGE}",
       [data directory])
  fi
else
  AC_DEFINE_UNQUOTED(PACKAGE_DATA_DIR, "${datadir}/${PACKAGE}",
     [data directory])
fi

#Set PACKAGE_SOURCE_DIR in config.h.
packagesrcdir=`cd $srcdir && pwd`
AC_DEFINE_UNQUOTED(PACKAGE_SOURCE_DIR, "${packagesrcdir}", [source directory])

#Use -Wall if we have gcc.
if test "x$GCC" = "xyes"; then
   if test "x$USE_DEBUG" = "xyes"; then
      CFLAGS="$CFLAGS -O0 -g -Wall"
   else
      CFLAGS="$CFLAGS -O2 -finline-functions -fno-strict-aliasing -Wall";
   fi
fi

AC_OUTPUT([
Makefile
src/Makefile
])
AC_MSG_RESULT([[
-----------------------------------------------------------------------
             compile configuration completed successfully

      Motiontrack will be compiled with the following options:

     Image I/O library: $IMAGE_LIBRARY
    image read support: $RFORMATS
   image write support: $WFORMATS
   speed optimisations: $( if test "x$USE_SPEEDHACK" != "xno"; then echo yes; else echo no; fi )
          debug output: $( if test "x$USE_DEBUG" = "xyes"; then echo yes; else echo no; fi )
       cluster support: $( if test "x$USE_CLUSTER" = "xyes"; then echo yes; else echo no; fi )


         \$CPPFLAGS are: $CPPFLAGS
             \$DEFS are: $DEFS
           \$CFLAGS are: $CFLAGS
          \$LDFLAGS are: $LDFLAGS
	     \$LIBS are: $LIBS

-----------------------------------------------------------------------
]])

