AC_INIT(ming, 0.4.0.beta4)

dnl -- Release version 
dnl To avoid colation problems, the 0.4 release shall be 0.4.1

MAJOR_VERSION=0
MINOR_VERSION=4
MICRO_VERSION=0.beta4

MING_VERSION=${MAJOR_VERSION}.${MINOR_VERSION}.${MICRO_VERSION}

dnl Tell configure to look in the config subdirectory
dnl for config.guess, config.sub, etc
AC_CONFIG_AUX_DIR(config)

AM_INIT_AUTOMAKE(AC_PACKAGE_NAME, AC_PACKAGE_VERSION)

AM_MAINTAINER_MODE

dnl -- Version info for libtool
INTERFACE_CURRENT=4
INTERFACE_REVISION=0
INTERFACE_AGE=4

AC_SUBST(INTERFACE_CURRENT)
AC_SUBST(INTERFACE_REVISION)
AC_SUBST(INTERFACE_AGE)


dnl GNU recommended way to determine host variables (OS, etc)
AC_CANONICAL_HOST

AC_PROG_CC
AC_PROG_CXX
AC_PROG_INSTALL

dnl --------------------------------------------
dnl Check if we should build python extension
dnl --------------------------------------------

AC_PATH_PROG(PYTHON, python)
AM_CONDITIONAL(HAVE_PYTHON, test x"$PYTHON" != x)

AC_ARG_ENABLE(
[python],
[  --enable-python           Enable build of python extension],
[case "${enableval}" in
  yes) python_ext=yes ;;
   no) python_ext=no ;;
    *) AC_MSG_ERROR([bad value ${enableval} for --enable-python-ext option]) ;;
esac],
python_ext=no)


if test x"$python_ext" = xyes; then
	if test x"$PYTHON" = x; then
		AC_MSG_ERROR([Can't build python extension, as python executable could not be found])
	fi
fi

AM_CONDITIONAL(BUILD_PYTHON_EXTENSION, test x"$python_ext" = xyes)


dnl --------------------------------------------
dnl Check if we should build perl extension
dnl --------------------------------------------

AC_PATH_PROG(PERL, perl)
AM_CONDITIONAL(HAVE_PERL, test x"$PERL" != x)

AC_ARG_ENABLE(
[perl],
[  --enable-perl           Enable build of perl extension],
[case "${enableval}" in
  yes) perl_ext=yes ;;
   no) perl_ext=no ;;
    *) AC_MSG_ERROR([bad value ${enableval} for --enable-perl-ext option]) ;;
esac], perl_ext=no)

if test x"$perl_ext" = xyes; then
	if test x"$PERL" = x; then
		AC_MSG_ERROR([Can't build perl extension, as perl executable could not be found])
	fi
fi

AM_CONDITIONAL(BUILD_PERL_EXTENSION, test x"$perl_ext" = xyes)

dnl --------------------------------------------
dnl Check if we should build php extension
dnl --------------------------------------------

AC_PATH_PROG(PHP, php)
AC_PATH_PROG(PHPIZE, phpize)
dnl AM_CONDITIONAL(HAVE_PHP, test x"$PHP" != x)

AC_ARG_ENABLE(
[php],
[  --enable-php           Enable build of php extension],
[case "${enableval}" in
  yes) php_ext=yes ;;
   no) php_ext=no ;;
    *) AC_MSG_ERROR([bad value ${enableval} for --enable-php-ext option]) ;;
esac], php_ext=no)

if test x"$php_ext" = xyes; then
	if test x"$PHP" = x -o x"$PHPIZE" = x ; then
		AC_MSG_ERROR([Can't build php extension, as php or phpize executable could not be found])
	fi
fi

AM_CONDITIONAL(BUILD_PHP_EXTENSION, test x"$php_ext" = xyes)

dnl --------------------------------------------
dnl Check if we should build tcl extension
dnl --------------------------------------------

AC_PATH_PROG(TCL, tclsh)
AM_CONDITIONAL(HAVE_TCL, test x"$TCL" != x)

AC_ARG_ENABLE(
[tcl],
[  --enable-tcl           Enable build of tcl extension],
[case "${enableval}" in
  yes) tcl_ext=yes ;;
   no) tcl_ext=no ;;
    *) AC_MSG_ERROR([bad value ${enableval} for --enable-tcl-ext option]) ;;
esac], tcl_ext=no)

if test x"$tcl_ext" = xyes; then
	if test x"$TCL" = x; then
		AC_MSG_ERROR([Can't build tcl extension, as tcl executable could not be found])
	fi

tcllibdirs_default="\
$prefix/lib/itcl \
$prefix/lib \
$HOME/local/lib \
$HOME/lib \
/usr/local/lib \
/usr/lib64 \
/usr/lib"

	for i in $tcllibdirs_default; do
	  for suf in 8.3 8.4 ""; do
	    if test -f $i/libtcl$suf.so || test -f $i/libtcl$suf.a; then
	     TCL_LIB_DIR=$i
	     break
	    fi
	  done
	done

	if test -z "$TCL_LIB_DIR"; then
	  AC_MSG_ERROR(Unable to find a Tcl library.)
	fi

tclincdirs_default="\
/usr/include/tcl-private/generic \
/usr/include/tk-private/generic \
/usr/include/itcl-private/generic \
/usr/include/tcl8.4 \
/usr/include/tcl8.4/tcl-private/generic \
/usr/include/tcl8.4/tk-private/generic \
/usr/include/tcl8.4/itcl-private/generic \
/usr/include/tcl8.4 \
/usr/include/tcl8.3/tcl-private/generic \
/usr/include/tcl8.3/tk-private/generic \
/usr/include/tcl8.3/itcl-private/generic \
/usr/include/tcl8.3 \
/usr/include/tcl8.2/generic \
$prefix/include/itcl \
/usr/include/itcl3.1/generic/ \
$prefix/include \
$HOME/local/include \
$HOME/include \
/usr/local/include \
/usr/include/tcl \
/usr/include"

	for i in $tclincdirs_default; do
	   if test -f $i/tcl.h; then
	     TCL_INC_DIR=$i
	     break
	   fi
	done

	if test -z "$TCL_INC_DIR"; then
	  AC_MSG_ERROR(Unable to find a Tcl header.)
	fi

fi

AM_CONDITIONAL(BUILD_TCL_EXTENSION, test x"$tcl_ext" = xyes)
AC_SUBST(TCL_LIB_DIR)
AC_SUBST(TCL_INC_DIR)

dnl --------------------------------------------
dnl Check for required programs
dnl --------------------------------------------

AC_PROG_YACC
AM_PROG_LEX
AC_PROG_LIBTOOL
if test x"$LIBTOOL" = x; then
	AC_MSG_ERROR([could not detect libtool, bailing out])
fi

dnl Check if the X libraries are installed (needed for libungif on at least Solaris)
AC_CHECK_LIB(X11, XGetImage, XLIB="-lX11", XLIB="")

dnl Check for the freetype library
AC_ARG_WITH(freetype-config, [  --with-freetype-config=PROG   Use FreeType configuration program PROG], freetype_config=$withval, freetype_config=yes)

if test "$freetype_config" = "yes"; then 
        AC_PATH_PROG(ft_config,freetype-config,no)
        if test "$ft_config" = "no"; then
                AC_MSG_ERROR([You must have freetype installed; see http://www.freetype.org/])
        fi
else
        ft_config="$freetype_config"
fi

FREETYPE_CFLAGS="`$ft_config --cflags`"
FREETYPE_LIBS="`$ft_config --libs`"

AC_SUBST(FREETYPE_LIBS)
AC_SUBST(FREETYPE_CFLAGS)

dnl Check for the gif or ungif libraries
AC_CHECK_LIB(gif, PrintGifError, GIFLIB="-lgif", GIFLIB="")
dnl MinGW check for libungif
AC_CHECK_LIB(ungif, DGifOpen, GIFLIB="-lungif")
dnl Solaris needs -lX11 on the linker line for ungif to work
AC_CHECK_LIB(ungif, PrintGifError, GIFLIB="-lungif",, "-lX11")

dnl Check for the png library
dnl Solaris needs -lm on the linker line, and other platforms aren't bothered having it there. :)
AC_CHECK_LIB(png, png_read_image, PNGLIB="-lpng", PNGLIB="", "-lm")

dnl Check for the zlib library
AC_CHECK_LIB(z, compress2,
	ZLIB="-lz",
	dnl Windows has the zlib library, but its named zdll instead
	AC_CHECK_LIB(zdll, compress2, ZLIB="-lzdll", ZLIB="")
)
AC_CHECK_HEADERS([zlib.h])

dnl Check if the vasprintf function exists on this platform
dnl (It's not present on MinGW and Solaris 10 at the time of this coding)
dnl Also check for the presence of the mkstemp function (it's not on MinGW)
AC_CHECK_FUNCS(vasprintf mkstemp)

dnl Check for various getopt functions
AC_CHECK_FUNCS(getopt getopt_long)

AC_CHECK_FUNC(sin, MATHLIB="", [
        AC_CHECK_LIB(m, sin, MATHLIB="-lm", [
		AC_ERROR([I can't find sin() function !!!])]
	)]
)

AC_DEFINE(TRACK_ALLOCS, 1, [Define this if you want Ming to track all objects allocations. Ming will mantain a doubly linked list of allocated objects, call Ming_collectGarbage() to get rid of them all])

AM_CONDITIONAL(USE_CXX, [test x${CXX} != x])
if test -n "${CXX}"; then
	AC_DEFINE([USE_CXX], [1], [Use c++])
fi

AM_CONDITIONAL(USE_ZLIB, test x${ZLIB} != x)
if test -n "${ZLIB}"; then
	AC_DEFINE([USE_ZLIB], [1], [Use zlib])
fi

AM_CONDITIONAL(USE_FREETYPE, test x${ft_config} != x)
if test -n "${ft_config}"; then
	AC_DEFINE(USE_FREETYPE, [1], [Use freetype library])
fi

AM_CONDITIONAL(USE_GIF, test x${GIFLIB} != x)
if test -n "${GIFLIB}"; then
	AC_DEFINE(USE_GIF, [1], [Use gif library])
fi

AM_CONDITIONAL(USE_PNG, test x${PNGLIB} != x)
if test -n "${PNGLIB}"; then
 	AC_DEFINE(USE_PNG, [1], [Use png library])
fi

CFLAGS="$CFLAGS -Wall"
MACHINE=`uname -m`
if test "$MACHINE" = "x86_64"; then
	CFLAGS="$CFLAGS -fPIC"
fi

AC_SUBST(SHCFLAGS)
AC_SUBST(GIFLIB)
AC_SUBST(PNGLIB)
AC_SUBST(MATHLIB)
AC_SUBST(XLIB)
AC_SUBST(ZLIB)
AC_SUBST(MAJOR_VERSION)
AC_SUBST(MINOR_VERSION)
AC_SUBST(MICRO_VERSION)
AC_SUBST(MING_VERSION)

AM_CONFIG_HEADER(src/ming_config.h)

dnl AC_OUTPUT(Makefile.config src/ming.h util/ming-config)
AC_OUTPUT([
	docs/Makefile
	docs/man/Makefile
	Makefile 
	src/actioncompiler/Makefile
	src/blocks/Makefile
	src/Makefile
	src/ming.h
	src/libming.pc
	perl_ext/Makefile
	php_ext/Makefile
	py_ext/Makefile
	py_ext/setup.py
	tcl_ext/Makefile
	test/Makefile
	test/Movie/Makefile
	test/Movie/Background/Makefile
	test/Movie/Dimension/Makefile
	test/Movie/add/Makefile
	test/Movie/new/Makefile
	test/Movie/nextFrame/Makefile
	test/Movie/NumFrames/Makefile
	test/Movie/FrameLabel/Makefile
	test/Movie/Protect/Makefile
	test/Movie/Rate/Makefile
	test/Filter/Makefile
	test/actionscript/Makefile
	macros/Makefile
	util/Makefile
	util/ming-config
	ming.spec
])

echo "Config summary: "

if test x"$CXX" != x; then
	echo "  C++ enabled"
else
	echo "  C++ disabled"
fi
if test x"$perl_ext" = "xyes"; then
	echo "  Perl extension enabled"
else
	echo "  Perl extension disabled"
fi

if test x"$php_ext" = "xyes"; then
	echo "  PHP extension enabled"
else
	echo "  PHP extension disabled"
fi

if test x"$python_ext" = "xyes"; then
	echo "  Python extension enabled"
else
	echo "  Python extension disabled"
fi

if test x"$tcl_ext" = "xyes"; then
	echo "  Tcl extension enabled"
else
	echo "  Tcl extension disabled"
fi

if test x"$ZLIB" != "x"; then
	echo "  ZLIB enabled ($ZLIB)"
else
	echo "  ZLIB disabled"
fi

if test x"$ft_config" = "x"; then
	echo "  Freetype library disabled"
else
	echo "  Freetype library enabled ($ft_config)"
fi

if test x"$GIFLIB" = "x"; then
	echo "  GIF library disabled"
else
	echo "  GIF library enabled ($GIFLIB)"
fi

if test x"$PNGLIB" = "x"; then
	echo "  PNG library disabled"
else
	echo "  PNG library enabled ($PNGLIB)"
fi

