AC_INIT(vigra, 1.6.0)

dnl-------------------------------------------------------------------
dnl                   vigra/interactive version
dnl-------------------------------------------------------------------

MAJOR_VERSION=1
MINOR_VERSION=6
MICRO_VERSION=0

SHAREDLIB_VERSION=`echo "$MAJOR_VERSION$MINOR_VERSION$MICRO_VERSION" | sed 's,[[^0-9]],,g'`

echo "configuring VIGRA $MAJOR_VERSION.$MINOR_VERSION.$MICRO_VERSION"

AC_SUBST(MAJOR_VERSION)dnl
AC_SUBST(MINOR_VERSION)dnl
AC_SUBST(MICRO_VERSION)dnl
AC_SUBST(SHAREDLIB_VERSION)dnl

dnl-------------------------------------------------------------------
dnl                   determine directories
dnl-------------------------------------------------------------------

AC_PREFIX_DEFAULT([`pwd`])

vigra_builddir=`pwd`
vigra_srcdir=`expr $0 : "\(.*\)/"`

if test "x$vigra_srcdir" = "x" ; then
    vigra_srcdir=`pwd` ;
else
    cd $vigra_srcdir
    vigra_srcdir=`pwd`
    cd $vigra_builddir ;
fi

# expand prefix and exec_prefix
if test "x$prefix" = "xNONE" ; then
    prefix=$ac_default_prefix
elif test "x$prefix" = "x." ; then
    prefix=`pwd`
fi

AC_CONFIG_AUX_DIR($vigra_srcdir/config)

dnl-------------------------------------------------------------------
dnl              compiler and platform configuration
dnl-------------------------------------------------------------------
AC_CANONICAL_SYSTEM

dnl AC_CYGWIN seems to be part of AC_CANONICAL_SYSTEM
dnl AC_MINGW32 seems to be part of AC_CANONICAL_SYSTEM

CFLAGS="$CFLAGS -O"
CXXFLAGS="$CXXFLAGS -O"

AC_ARG_WITH( cc, AC_HELP_STRING([--with-cc], [your C compiler (default=gcc)]), CC=$with_cc, [if test "x$CC" = "x" ; then CC=gcc ; fi])
AC_ARG_WITH( cxx, AC_HELP_STRING([--with-cxx], [your C++ compiler (default=g++)]), CXX=$with_cxx, [if test "x$CXX" = "x" ; then CXX=g++ ; fi])

AC_PROG_CC
AC_PROG_CXX
AC_PROG_LD
VIGRA_PROG_INSTALL


dnl-------------------------------------------------------------------
dnl                     libtool configuration
dnl-------------------------------------------------------------------
AC_ENABLE_SHARED(yes)
AC_ENABLE_STATIC(yes)
AC_LIBTOOL_SETUP dnl tell autoconf about libtools prerequisites
AC_PROG_LIBTOOL
AC_LIBTOOL_CXX
LIBTOOL=`echo $LIBTOOL | sed 's,$(top_builddir),$(vigra_builddir),'`

dnl-------------------------------------------------------------------
dnl                    package configuration
dnl-------------------------------------------------------------------

# policy: for every package, we define 6 variables
#     with_package, with_packagelib, with_packageinc,
#     with_package_I, with_package_L, with_package_l
# the first 3 are used for user control in the command line
# the last 3 will contain the results of configuration
# at the end with_package is "yes" on success, "no" on failure

# look for pkg-config
AC_CHECK_PROG(PKGCONFIG, pkg-config, pkg-config)
if test -n "$PKGCONFIG"; then
    PKG_CONFIG_PATH="$PKG_CONFIG_PATH${PKG_CONFIG_PATH:+:}/usr/local/lib/pkgconfig"
fi

# look for extra include directories in $CPPFLAGS
INCLUDESEARCHPATH=""
VIGRA_FIND_PATH(CPPFLAGS, INCLUDESEARCHPATH, [-I\(.*\)])
CPPFLAGS=""
LIBCPPFLAGS=""

# look for extra library directories in $LDFLAGS
LIBSEARCHPATH=""
VIGRA_FIND_PATH(LDFLAGS, LIBSEARCHPATH, [-L\(.*\)])
LDFLAGS=""

# declare supported external packages
VIGRA_DECLARE_PACKAGE(tiff, [support import/export of tiff images], yes)
VIGRA_DECLARE_PACKAGE(jpeg, [support import/export of jpeg images], yes)
VIGRA_DECLARE_PACKAGE(png,  [support import/export of png images], yes)
VIGRA_DECLARE_PACKAGE(zlib, [zlib compression library (required for libpng)], yes)
VIGRA_DECLARE_PACKAGE(fftw, [support fast Fourier transforms (fftw3 required!)], yes)

# AC_ARG_WITH(python, [
# --with-python:  Build the VIGRA Python bindings (default: no).
#                 The python executable must be in the path or given by $PYTHON], ,[with_python="no"])
# AC_ARG_WITH(pythoninstall, [--with-pythoninstall=dir:  install the VIGRA Python modules in 'dir' instead of the
#                            default Python module directory (requires adding 'dir'
#                            to $PYTHONPATH)], ,)
with_python="no"

VIGRA_DECLARE_PACKAGE(boostpython, [boost_python glue code generator (required for VIGRA Python bindings)], [$with_python])
AC_ARG_WITH(boostpythonname, [        --with-boostpythonname=name : the name of the boost python library (default: boost_python)], ,)

dnl-------------------------------------------------------------------
dnl           tiff package configurations
dnl-------------------------------------------------------------------

LIBS="-lm"  # needed by libtiff
VIGRA_FIND_PACKAGE(tiff, tiff, tiff.h)
LIBS=""
if test "$with_tiff" = "yes"; then
    TIFF_LIB="$with_tiff_L $with_tiff_l"
    IMPEX_DEPLIBS="$TIFF_LIB $IMPEX_DEPLIBS"
    LIBCPPFLAGS="-DHasTIFF $with_tiff_I $LIBCPPFLAGS"
else
    AC_MSG_WARN(  Configuring without tiff support)
fi

dnl-------------------------------------------------------------------
dnl           jpeg package configurations
dnl-------------------------------------------------------------------

VIGRA_FIND_PACKAGE(jpeg, jpeg, jpeglib.h)
if test "$with_jpeg" = "yes"; then
    JPEG_LIB="$with_jpeg_L $with_jpeg_l"
    IMPEX_DEPLIBS="$JPEG_LIB $IMPEX_DEPLIBS"
    LIBCPPFLAGS="-DHasJPEG $with_jpeg_I $LIBCPPFLAGS"
else
    AC_MSG_WARN(  Configuring without jpeg support)
fi

dnl-------------------------------------------------------------------
dnl           png and zlib package configurations
dnl-------------------------------------------------------------------

# search using pkg-config
if test "$with_png" = "search"; then
    VIGRA_FIND_PACKAGE_PKGCONFIG(png, libpng, png.h)
fi
# search using libpng-config
if test "$with_png" = "search"; then
    AC_CHECK_PROG(LIBPNGCONFIG, libpng-config, libpng-config)
    if test "x$LIBPNGCONFIG" != "x"; then
        AC_LANG_SAVE
        AC_LANG_CPLUSPLUS

        CPPFLAGS=`$LIBPNGCONFIG --I_opts`
        LDFLAGS=`$LIBPNGCONFIG --L_opts`
        LIBS="-lpng -lz"
        AC_TRY_LINK(
                [#include <png.h>
], [],
                [with_png="yes"
                 with_png_I="$CPPFLAGS"
                 with_png_L="$LDFLAGS"
                 with_png_l="$LIBS"
                ],
                [with_png_I=""
                 with_png_L=""
                 with_png_l=""
                ])
        CPPFLAGS=""
        LDFLAGS=""
        LIBS=""
        AC_LANG_RESTORE
    fi
fi
# search using explicitly given paths (either via the --with_png... switches
# or LDFLAGS, CPPFLAGS or in /usr/local
if test  "$with_png" != "no" -a "$with_png" != "yes"; then
    if test "$with_zlib" != "no"; then
        VIGRA_FIND_PACKAGE(zlib, z, zlib.h)
    fi
    if test "$with_zlib" = "no"; then
        AC_MSG_WARN(  [Configuring without png support -- missing zlib] )
        with_png="no"
    else
        CPPFLAGS="$with_zlib_I"
        LDFLAGS="$with_zlib_L"
        LIBS="$with_zlib_l"
        VIGRA_FIND_PACKAGE(png, png, png.h)
        CPPFLAGS=""
        LDFLAGS=""
        LIBS=""
    fi
fi
if test $with_png = "yes"; then
    PNG_LIB="$with_png_L $with_png_l"
    IMPEX_DEPLIBS="$PNG_LIB $IMPEX_DEPLIBS"
    LIBCPPFLAGS="-DHasPNG $with_png_I $LIBCPPFLAGS"
else
    AC_MSG_WARN(  Configuring without png support)
fi

dnl-------------------------------------------------------------------
dnl           fftw package configurations
dnl-------------------------------------------------------------------

if test "$with_fftw" = "search"; then
    VIGRA_FIND_PACKAGE_PKGCONFIG(fftw, fftw3, fftw3.h)
fi
if test  "$with_fftw" != "no" -a "$with_fftw" != "yes"; then
    VIGRA_FIND_PACKAGE(fftw, fftw3, fftw3.h)
fi
if test $with_fftw = "yes"; then
    FFTW_LIB="$with_fftw_L $with_fftw_l"
    FFTW_INCLUDES="$with_fftw_I"
else
    AC_MSG_WARN(  Configuring without fftw support)
fi

dnl-------------------------------------------------------------------
dnl           python package configurations
dnl-------------------------------------------------------------------

if test "$with_python" = "yes" -a ! -d "$vigra_srcdir/src/pythonbindings"; then
    AC_MSG_WARN([VIGRA Python sources missing, configuring without Python support])
    with_python="no"
fi
if test "$with_python" = "yes"; then
    AC_CHECK_PROG(PYTHON, python, python)
    if test "x$PYTHON" != "x"; then
        AC_LANG_SAVE
        AC_LANG_CPLUSPLUS

        changequote(<<, >>)dnl
        CPPFLAGS=`$PYTHON -c "import sys; v = sys.version[:3]; print '-I'+sys.prefix+'/include/python'+v, '-I'+sys.exec_prefix+'/include/python'+v"`
        LDFLAGS=`$PYTHON -c "import sys; print '-L'+sys.exec_prefix+'/lib/python'+sys.version[:3]+'/config'"`
        LIBS=`$PYTHON -c "import sys; print '-lpython'+sys.version[:3]"`
        changequote([, ])dnl
        AC_MSG_CHECKING([for the python includes and library ])
        AC_TRY_LINK(
                [#include <Python.h>
], [],
                [with_python="yes"
                 with_python_I="$CPPFLAGS"
                 with_python_L="$LDFLAGS"
                 with_python_l="$LIBS"
                 AC_MSG_RESULT($with_python_I $with_python_L $with_python_l)
                ],
                [with_python="no"
                 with_python_I=""
                 with_python_L=""
                 with_python_l=""
                 AC_MSG_RESULT([not found])
                ])
        CPPFLAGS=""
        LDFLAGS=""
        LIBS=""
        AC_LANG_RESTORE
    fi
    if test "$with_python" = "yes"; then
        PYTHON_LIB="$with_python_L $with_python_l"
        PYTHON_INCLUDES="$with_python_I"
        if test -z "$with_pythoninstall"; then
            changequote(<<, >>)dnl
            PYTHON_INSTALLDIR=`$PYTHON -c "import sys; print sys.exec_prefix+'/lib/python'+sys.version[:3]+'/site-packages'"`
            changequote([, ])dnl
        else
            PYTHON_INSTALLDIR="$with_pythoninstall"
        fi
    else
        AC_MSG_WARN(  Configuring without Python support)
    fi
fi

dnl-------------------------------------------------------------------
dnl           boost_python package configurations
dnl-------------------------------------------------------------------

if test "$with_python" = "yes"; then
    if test "$with_boostpython" != "no"; then
        # we only search for a directory containing a suitably named library
        # because portable linking of Python modules requires libtool
        if test "$with_boostpython" = "explicit"; then
            BOOSTSEARCHPATH="$with_boostpythonlib"
        else
            changequote(<<, >>)dnl
            BOOSTSEARCHPATH="$LIBSEARCHPATH "`$PYTHON -c "import sys; print sys.prefix+'/lib/python'+sys.version[:3], sys.prefix+'/lib'"`" /usr/local/lib /usr/lib"
            changequote([, ])dnl
        fi
        # since the lib name may vary, we must find it out as well
        if test -z "$with_boostpythonname"; then
            with_boostpythonname="boost_python"
            changequote(<<, >>)dnl
            BOOSTREGEX="lib\(boost_python[^\.]*[0-9]\)\."
            changequote([, ])dnl
        else
            BOOSTREGEX="lib\($with_boostpythonname\)\."
        fi
        with_boostpython_L=""
        with_boostpython_l=""
        AC_MSG_CHECKING([for the boost python library ])
        # look for both the long (with version number) and short form of the name
        for i in $BOOSTSEARCHPATH; do
           for j in `cd "$i" && ls lib"$with_boostpythonname"* 2> /dev/null`; do
                r=`expr "$j" : "$BOOSTREGEX"`
                rs=`expr "$j" : "lib\($with_boostpythonname\)\."`
                if test -z "$r"; then r="$rs"; fi
                if test -n "$r"; then
                    with_boostpythonname="$r"
                    with_boostpython_L="$with_python_L -L$i"
                    with_boostpython_l="-l$r $with_python_l"
                    break
                fi
            done
            if test -n "$with_boostpython_l"; then break; fi
        done
        if test -z "$with_boostpython_l"; then
            AC_MSG_RESULT([not found])
            with_boostpython="no"
        else
            AC_MSG_RESULT($with_boostpython_L $with_boostpython_l)
            if test "x$GXX" = "xyes"; then
                CPPFLAGS="-ftemplate-depth-96 $with_python_I"
            else
                CPPFLAGS="$with_python_I"
            fi

            # find out the boost version since it may be part of the include path
            changequote(<<, >>)dnl
            BOOST_VERSION=`expr "$with_boostpythonname" : ".*-\([^-]*\)\$"`
            changequote([, ])dnl
            if test -z "$BOOST_VERSION"; then
                BOOST_INCLUDE_POSTFIX=""
            else
                BOOST_INCLUDE_POSTFIX="boost-$BOOST_VERSION"
            fi

            VIGRA_FIND_INCLUDE(boostpython, boost/python/converter/registry.hpp, $BOOST_INCLUDE_POSTFIX )
            if test "$with_boostpython" != "no"; then
                with_boostpython="yes"
            fi
        fi
    fi
    if test "$with_boostpython" = "yes"; then
        BOOST_PYTHON_LIB="$with_boostpython_L $with_boostpython_l"
        BOOST_INCLUDES="$with_boostpython_I"
    else
        # undo the python settings
        with_python="no"
        PYTHON_LIB=""
        PYTHON_INCLUDES=""
        PYTHON_INSTALLDIR=""
        AC_MSG_WARN(  Configuring without Python support)
    fi
fi

dnl-------------------------------------------------------------------
dnl                        finish & output
dnl-------------------------------------------------------------------

VIGRA_INCPATH="$includedir"
VIGRA_INCLUDES="-I$includedir"
POSTINSTALLCPPFLAGS="$VIGRA_INCLUDES"
CPPFLAGS="-I$vigra_srcdir/include"
LIBCPPFLAGS="$LIBCPPFLAGS $CPPFLAGS"
IMPEX_LIB="-L$libdir -lvigraimpex $IMPEX_DEPLIBS"
LIBS="$IMPEX_LIB"

AC_SUBST(vigra_srcdir)
AC_SUBST(vigra_builddir)

AC_SUBST(TIFF_LIB)
AC_SUBST(JPEG_LIB)
AC_SUBST(PNG_LIB)
AC_SUBST(FFTW_LIB)
AC_SUBST(FFTW_INCLUDES)
AC_SUBST(IMPEX_LIB)
AC_SUBST(IMPEX_DEPLIBS)
AC_SUBST(VIGRA_INCLUDES)
AC_SUBST(VIGRA_INCPATH)
AC_SUBST(POSTINSTALLCPPFLAGS)
AC_SUBST(BOOST_PYTHON_LIB)
AC_SUBST(BOOST_INCLUDES)
AC_SUBST(PYTHON_INSTALLDIR)

AC_SUBST(LIBCPPFLAGS)

VIGRA_OUTPUT="config/Makefile.include config/vigra-config doc/vigra/index.html src/Makefile src/impex/Makefile src/examples/Makefile Makefile"

if test "$with_python" = "yes" -a -d "$vigra_srcdir/src/pythonbindings"; then
    VIGRA_OUTPUT="$VIGRA_OUTPUT $vigra_srcdir/src/pythonbindings/Makefile"
fi

if test -d "$vigra_srcdir/test"; then
    VIGRA_OUTPUT="$VIGRA_OUTPUT test/Makefile.include test/Makefile"
    for i in `ls "$vigra_srcdir/test"`; do
        if test -d "$vigra_srcdir/test/$i"; then
            if test -f "$vigra_srcdir/test/$i/Makefile.in"; then
                 VIGRA_OUTPUT="$VIGRA_OUTPUT test/$i/Makefile"
            fi
        fi
    done
fi

AC_OUTPUT($VIGRA_OUTPUT)
chmod +x config/vigra-config # for src/examples

[echopath()
{
    echo "$1" \
    | sed "s,\$[({]exec_prefix[})],$exec_prefix," \
    | sed "s,\$[({]prefix[})],$prefix,"
}]

echo "configure finished."
echo ""
echo "TIFF   support enabled (--with-tiff): $with_tiff"
echo "JPEG   support enabled (--with-jpeg): $with_jpeg"
echo "PNG    support enabled (--with-png):  $with_png"
echo "FFTW   support enabled (--with-fftw): $with_fftw"
#echo "Python support enabled (--with-python): $with_python"
echo ""
echopath "The vigra sources are taken from $vigra_srcdir."
echopath "The vigraimpex library    will be installed in $libdir."
echopath "The 'vigra-config' script will be installed in $bindir."
echopath "The include files         will be installed in $includedir."
echopath "The documentation         will be installed in $docdir."
if test "$with_python" = "yes"; then
    echopath "The Python modules        will be installed in $PYTHON_INSTALLDIR."
fi
