AC_PREREQ([2.58])
AC_INIT([ffms2],[2.13.1])
AC_CONFIG_SRCDIR([src/core/ffms.cpp])
AC_CONFIG_MACRO_DIR([m4])
AM_INIT_AUTOMAKE([subdir-objects])

if test -z "$CFLAGS"; then
    CFLAGS="-O3"
fi

if test -z "$CXXFLAGS"; then
    CXXFLAGS="-O3"
fi

AM_CONFIG_HEADER([src/config/config.h])
AC_PROG_CC
AC_PROG_CXX
LT_INIT([static disable-shared])

if echo "$host" | $GREP "cygwin" >/dev/null 2>&1 && test "$enable_shared" = "yes"; then
    AC_MSG_ERROR([Shared build is broken on cygwin.
                  Please remove --enable-shared from your configure options or build with another --host.])
fi

AC_FUNC_ALLOCA
if ! test "x$ac_cv_working_alloca_h" = "xyes" && ! test "x$ac_cv_func_alloca_works" = "xyes"; then
    AC_MSG_FAILURE([ffms2 requires alloca to compile])
fi

dnl Workaround for a bug in libtool
dnl The windows libtool uses a file magic checking method that only accepts
dnl dynamic libraries. Change it for libtool's alternative checking method.
if test "$lt_cv_file_magic_cmd" = "func_win32_libid" ; then
    deplibs_check_method='file_magic file format pei*-(i386|x86-64)|(.*architecture: i386)?'
    file_magic_cmd='$OBJDUMP -f'
fi

CHECK_ZLIB

AC_MSG_CHECKING([for CoUninitialize in -lole32])
_LIBS="$LIBS"
LIBS="-lole32 $LIBS"
AC_LINK_IFELSE([
               #include <objbase.h>
               int main() {
                   CoUninitialize();
                   return 0;
               }
               ], [AC_MSG_RESULT([yes])], [LIBS="$_LIBS"; AC_MSG_RESULT([no])])

PKG_PROG_PKG_CONFIG([0.22])
PKG_CHECK_MODULES(FFMPEG, [libavformat >= 52.39.0 libavcodec >= 52.38.0 libswscale >= 0.7.0 libpostproc >= 51.2.0 libavutil >= 50.4.0 ])
CPPFLAGS="$CPPFLAGS -D__STDC_CONSTANT_MACROS"
AC_SUBST([FFMPEG_CFLAGS])
AC_SUBST([FFMPEG_LIBS])

pkgconfigdir="\$(libdir)/pkgconfig"
AC_SUBST(pkgconfigdir)

AC_DEFUN([TEST_FFMPEG], [
        AC_LINK_IFELSE([
            #include <libavcodec/avcodec.h>
            #include <libswscale/swscale.h>
            #include <libpostproc/postprocess.h>
            int main() {
                avcodec_init();
                swscale_version();
                postproc_version();
                return 0;
            }], [eval $1=yes], [eval $1=no])
        ])

AC_MSG_CHECKING([whether ffmpeg works])
_CFLAGS="$CFLAGS"
CFLAGS="$CFLAGS $FFMPEG_CFLAGS"
_LIBS="$LIBS"
LIBS="$FFMPEG_LIBS $LIBS"
TEST_FFMPEG([FFMPEG_WORKS])
AC_MSG_RESULT([$FFMPEG_WORKS])
if test "$FFMPEG_WORKS" = no; then
    AC_MSG_FAILURE([cannot link with ffmpeg])
fi

AC_MSG_CHECKING([whether -Wl,-Bsymbolic is needed])
if test "$enable_shared" = yes; then
    _LDFLAGS="$LDFLAGS"
    LDFLAGS="$LDFLAGS -shared $lt_prog_compiler_pic"
    TEST_FFMPEG([no_bsymbolic])
    if test "$no_bsymbolic" = "no"; then
        LDFLAGS="$LDFLAGS -Wl,-Bsymbolic"
        TEST_FFMPEG([bsymbolic])
        if test "$bsymbolic" = "yes"; then
            LDFLAGS="$_LDFLAGS -Wl,-Bsymbolic"
        else
            AC_MSG_RESULT($bsymbolic)
            AC_MSG_FAILURE([cannot build ffms2 as a shared library])
        fi
    else
        bsymbolic=no
        LDFLAGS="$_LDFLAGS"
    fi
    LDFLAGS="$LDFLAGS -version-info 2"
else
    bsymbolic=no
fi
CFLAGS="$_CFLAGS -std=c99"
CPPFLAGS="$CPPFLAGS -D_FILE_OFFSET_BITS=64"
LIBS="$_LIBS"
AC_MSG_RESULT($bsymbolic)

if echo "$host" | $GREP "mingw" >/dev/null 2>&1; then
    LTUNDEF="-no-undefined"
fi
AC_SUBST([LTUNDEF])

SHAVE_INIT([shave], [enable])
AC_CONFIG_FILES([
shave/shave
shave/shave-libtool
Makefile 
ffms2.pc
])
AC_OUTPUT

