# $Id$

AC_INIT(libpipi, 0.0)
AC_PREREQ(2.50)
AC_CONFIG_AUX_DIR(.auto)
AC_CANONICAL_SYSTEM
AM_INIT_AUTOMAKE([no-define tar-ustar])
dnl AM_MAINTAINER_MODE

AM_CONFIG_HEADER(config.h)

LT_MAJOR="0"
LT_MINOR="0"
LT_MICRO="0"
AC_SUBST(LT_MAJOR)
AC_SUBST(LT_MINOR)
AC_SUBST(LT_MICRO)
LT_VERSION="$LT_MAJOR:$LT_MINOR:$LT_MICRO"
AC_SUBST(LT_VERSION)

AM_PROG_CC_C_O
AC_PROG_CPP
AC_PROG_OBJC
_AM_DEPENDENCIES([OBJC])
OBJC="${CC}"
AC_SUBST(OBJC)
AC_SUBST(OBJCFLAGS)

AC_LIBTOOL_WIN32_DLL
AM_PROG_LIBTOOL
AC_LIBTOOL_CXX

AC_C_CONST
AC_C_INLINE

dnl AC_PROG_EGREP only exists in autoconf 2.54+, so we use AC_EGREP_CPP right
dnl now otherwise it might be set in an obscure if statement. Same thing for
dnl PKG_PROG_PKG_CONFIG which needs to be called first.
AC_EGREP_CPP(yes, foo)
PKG_PROG_PKG_CONFIG()
m4_pattern_allow([^PKG_CONFIG_LIBDIR$])
if test "${build}" != "${host}" -a "${PKG_CONFIG_LIBDIR}" = ""; then
  export PKG_CONFIG_LIBDIR=/dev/null
fi

AC_MSG_CHECKING(for library suffix)
LT_SUFFIX="`./libtool -n --mode=link gcc -o libTEST.la -no-undefined -version-number 123:666:999 -rpath / | xargs -n 1 | sed -ne 's/.*libTEST\(@<:@^, ()0-9@:>@*\)123\(@<:@^, ()0-9@:>@*\).*/\1'$LT_MAJOR'\2/p' | grep -v '\.$' | head -n 1`"
if test "$LT_SUFFIX" = ""; then
  LT_SUFFIX=".so.$LT_MAJOR"
  AC_MSG_RESULT([unknown, defaulting to $LT_SUFFIX])
else
  AC_MSG_RESULT($LT_SUFFIX)
fi
AC_SUBST(LT_SUFFIX)

AC_CHECK_HEADERS(stdio.h stdarg.h inttypes.h endian.h stdint.h getopt.h)

ac_cv_have_getopt_long="no"
AC_CHECK_FUNCS(getopt_long,
 [ac_cv_have_getopt_long="yes"],
 [AC_CHECK_LIB(gnugetopt, getopt_long,
   [ac_cv_have_getopt_long="yes"
    GETOPT_LIBS="${GETOPT_LIBS} -lgnugetopt"])])
if test "$ac_cv_have_getopt_long" != "no"; then
  AC_DEFINE(HAVE_GETOPT_LONG, 1, Define to 1 if you have the `getopt_long' function.)
fi
AM_CONDITIONAL(NEED_GETOPT_LONG, test "$ac_cv_have_getopt_long" = "no")
AC_SUBST(GETOPT_LIBS)

# How to get the C99 types. See pipi/pipi_types.h.in for details about
# the PIPI_TYPES variable
if test "${ac_cv_my_have_vga}" = "yes"; then
  PIPI_TYPES=0
else
  AC_CHECK_HEADERS(stdint.h,
    [PIPI_TYPES=1],
    [AC_CHECK_HEADERS(inttypes.h,
      [PIPI_TYPES=2],
      [AC_CHECK_HEADERS(windows.h,
        [PIPI_TYPES=3],
        [PIPI_TYPES=0])])])
fi
AC_SUBST(PIPI_TYPES)

# Optimizations
CFLAGS="${CFLAGS} -g -O2 -fno-strength-reduce -fomit-frame-pointer"
# Code qui fait des warnings == code de porc == deux baffes dans ta gueule
CFLAGS="${CFLAGS} -Wall -Wpointer-arith -Wcast-align -Wcast-qual -Wstrict-prototypes -Wshadow -Waggregate-return -Wmissing-prototypes -Wnested-externs -Wsign-compare"

# Use DevIL?
#AC_CHECK_HEADERS(IL/il.h,
# [ac_cv_my_have_il="yes"],
# [ac_cv_my_have_il="no"])
#CPPFLAGS="${save_CPPFLAGS}"
#AM_CONDITIONAL(USE_IL, test "${ac_cv_my_have_il}" = "yes")

# Use Windows?
#AC_CHECK_HEADERS(olectl.h,
# [ac_cv_my_have_ole="yes"],
# [ac_cv_my_have_ole="no"])
#CPPFLAGS="${save_CPPFLAGS}"
#AM_CONDITIONAL(USE_OLE, test "${ac_cv_my_have_ole}" = "yes")

# Use SDL?
ac_cv_my_have_sdl="no"
save_CPPFLAGS="${CPPFLAGS}"
AC_PATH_PROG(SDL_CONFIG, sdl-config, no)
if test "${SDL_CONFIG}" != "no"; then
  CPPFLAGS="${CPPFLAGS} `sdl-config --cflags`"
fi
AC_CHECK_HEADERS(SDL_image.h,
 [ac_cv_my_have_sdl="yes"],
 [ac_cv_my_have_sdl="no"])
CPPFLAGS="${save_CPPFLAGS}"
if test "${ac_cv_my_have_sdl}" != "no"; then
  AC_DEFINE(USE_SDL, 1, Define to 1 to use SDL_image)
fi
AM_CONDITIONAL(USE_SDL, test "${ac_cv_my_have_sdl}" = "yes")

# Use OpenCV?
ac_cv_my_have_opencv="no"
OPENCV="no"
PKG_CHECK_MODULES(OPENCV, opencv, [ac_cv_my_have_opencv="yes"], [:])
if test "${ac_cv_my_have_opencv}" != "no"; then
  AC_DEFINE(USE_OPENCV, 1, Define to 1 to use OpenCV)
fi
AM_CONDITIONAL(USE_OPENCV, test "${ac_cv_my_have_opencv}" = "yes")

# Use Imlib2?
ac_cv_my_have_imlib2="no"
IMLIB2="no"
PKG_CHECK_MODULES(IMLIB2, imlib2, [ac_cv_my_have_imlib2="yes"], [:])
if test "${ac_cv_my_have_imlib2}" != "no"; then
  AC_DEFINE(USE_IMLIB2, 1, Define to 1 to use Imlib2)
fi
AM_CONDITIONAL(USE_IMLIB2, test "${ac_cv_my_have_imlib2}" = "yes")

# Use Windows GDI?
ac_cv_my_have_gdi="no"
GDI="no"
AC_CHECK_HEADERS(windows.h,
 [ac_cv_my_have_gdi="yes"],
 [ac_cv_my_have_gdi="no"])
if test "${ac_cv_my_have_gdi}" != "no"; then
  AC_DEFINE(USE_GDI, 1, Define to 1 to use GDI)
fi
AM_CONDITIONAL(USE_GDI, test "${ac_cv_my_have_gdi}" = "yes")

# Use Cocoa? 
ac_cv_my_have_cocoa="no"
AC_CHECK_HEADERS(/System/Library/Frameworks/Cocoa.framework/Headers/Cocoa.h,
[
 MACOSX_SDK_FRAMEWORKS="-F${MACOSX_SDK}/System/Library/Frameworks"
 CPPFLAGS="${CPPFLAGS} ${ARCH} ${MACOSX_SDK_FRAMEWORKS} -I/Developer//SDKs/MacOSX10.5.sdk/System/Library/Frameworks/QuartzCore.framework/Versions/A/Headers/"
 CFLAGS="${CFLAGS} ${MACOSX_SDK_CFLAGS} -I/Developer//SDKs/MacOSX10.5.sdk/System/Library/Frameworks/QuartzCore.framework/Versions/A/Headers/"
 CXXFLAGS="${CXXFLAGS} ${MACOSX_SDK_CXXFLAGS}"
 OBJCFLAGS="${OBJCFLAGS} ${MACOSX_SDK_CFLAGS}"
 LDFLAGS="${ARCH} ${MACOSX_SDK_LDFLAGS} ${LDFLAGS}"
 AC_DEFINE(USE_COCOA, 1, Define to 1 to activate the Cocoa backend driver)
#    CACA_LIBS="${CACA_LIBS} -Wl,-syslibroot,${MACOSX_SDK},-framework,Cocoa"
#    CACA_DRIVERS="${CACA_DRIVERS} cocoa"
 ac_cv_my_have_cocoa="yes"],
 [ac_cv_my_have_cocoa="no"]
 )
AM_CONDITIONAL(USE_COCOA, test "${ac_cv_my_have_cocoa}" = "yes")


if test "${ac_cv_my_have_il}" = "no" -a "${ac_cv_my_have_ole}" = "no" -a "${ac_cv_my_have_sdl}" = "no" -a "${ac_cv_my_have_imlib2}" = "no" -a "${ac_cv_my_have_opencv}" = "no" -a "${ac_cv_my_have_cocoa}" = "no"; then
  AC_MSG_ERROR([[cannot find DevIL, OLE, SDL_Image, Imlib2, Cocoa, or OpenCV, please install one of them]])
fi

# Build pipi-sharp?
AC_PATH_PROG(GMCS, gmcs, no)
AM_CONDITIONAL(USE_CSHARP, test "${gmcs}" != "no")

# Build The Pimp?
ac_cv_my_have_gtksharp=yes
PKG_CHECK_MODULES([GLADE_SHARP_20], [glade-sharp-2.0], [:], [ac_cv_my_have_gtksharp=no])
PKG_CHECK_MODULES([GLIB_SHARP_20], [glib-sharp-2.0], [:], [ac_cv_my_have_gtksharp=no])
PKG_CHECK_MODULES([GTK_SHARP_20], [gtk-sharp-2.0], [:], [ac_cv_my_have_gtksharp=no])
AM_CONDITIONAL(USE_GTKSHARP, test "${ac_cv_my_have_gtksharp}" != no)

AC_CONFIG_FILES([
  Makefile
  pipi/Makefile
  src/Makefile
  genethumb/Makefile
  examples/Makefile
  test/Makefile
  pipi-sharp/Makefile
  ThePimp/Makefile
  win32/Makefile
])
AC_CONFIG_FILES([
  pipi/pipi_types.h
  pipi/pipi.pc
  pipi-sharp/pipi-sharp.pc
  pipi-sharp/pipi-sharp.dll.config
])

AC_OUTPUT

