dnl ****** -*- autoconf -*-
dnl OCamlSdl autoconf input
dnl ******
dnl
dnl $Id: configure.in,v 1.37 2005/01/30 22:31:44 oliv__a Exp $

dnl begin configure.in 
AC_INIT(src/sdl.ml)

AC_CONFIG_AUX_DIR(./support)
AC_CONFIG_HEADER(src/config.h)

# Check for OCaml programs 
AC_PROG_OCAML()
AC_PROG_FINDLIB()

# OCaml platform
AC_MSG_CHECKING(platform)
echo "print_endline Sys.os_type ;;" > conftest.ml
PLATFORM=$(ocaml conftest.ml)
AC_MSG_RESULT($PLATFORM)

# Check for programs
if test -z "$CFLAGS" ; then
   CFLAGS="-g -O"
fi
AC_PROG_CC
AC_OBJEXT
AC_PROG_RANLIB
AC_FUNC_ALLOCA
AC_C_INLINE
if test "$ac_cv_c_inline" != no ; then
  AC_DEFINE(HAVE_INLINE)
fi

AC_PROG_INSTALL

# Checking for lablGL
AC_ARG_WITH(lablgldir,[  --with-lablgldir=DIR	  specify location of lablgl],LABLGLDIR="$withval")
AC_CHECK_OCAML_MODULE(lablGL,LABLGLDIR,Gl,+lablGL)

# Where to install the library ?
AC_ARG_WITH(installdir,[  --with-installdir=DIR	  specify installation directory [defaults to +sdl]],OCAMLSDLDIR="$withval", OCAMLSDLDIR="$OCAMLLIB/sdl")
AC_SUBST(OCAMLSDLDIR)

# Check for libraries

# Check for SDL libraries 
SDL_VERSION=1.2.0
AM_PATH_SDL($SDL_VERSION, :,
    AC_MSG_ERROR([*** SDL version $SDL_VERSION not found!])
)

case $target in
 *-apple-darwin*) PLATFORM=Apple ;;
esac

LIBS="$LIBS $SDL_LIBS"
CPPFLAGS="$CPPFLAGS $SDL_CFLAGS"

# cmdline options 
AC_ARG_ENABLE(sdl_ttf,
 	      [  --disable-sdl-ttf       without freetype support  [default is enabled]],
	      sdl_ttf=$enableval, sdl_ttf=yes)
AC_ARG_ENABLE(sdl_image,
 	      [  --disable-sdl-image     without sdl image support [default is enabled]],
	      sdl_image=$enableval, sdl_image=yes)
AC_ARG_ENABLE(sdl_mixer,
 	      [  --disable-sdl-mixer     without sdl mixer support [default is enabled]],
	      sdl_mixer=$enableval, sdl_mixer=yes)
AC_SUBST(PARTS)

# Checking for lib SDL_ttf
if test "$sdl_ttf" = yes ; then
  AC_CHECK_HEADERS(SDL_ttf.h, : , sdl_ttf=no)
  if test "$sdl_ttf" = yes ; then 
    AC_CHECK_LIB(SDL_ttf, TTF_OpenFontRW, AC_DEFINE(SDL_TTF_VERSION,206),[
      AC_CHECK_LIB(SDL_ttf, TTF_OpenFontIndex, AC_DEFINE(SDL_TTF_VERSION,204),[
        AC_CHECK_LIB(SDL_ttf, TTF_OpenFont, AC_DEFINE(SDL_TTF_VERSION,200),
        AC_MSG_ERROR(Cannot find SDL_tff library.))
      ])
    ])
    PARTS="$PARTS ttf"
  fi
fi

# Checking for lib SDL_image
if test "$sdl_image" = yes ; then
  AC_CHECK_HEADERS(SDL_image.h, : , sdl_image=no)
  if test "$sdl_image" = yes ; then 
    AC_CHECK_LIB(SDL_image, IMG_ReadXPMFromArray, AC_DEFINE(SDL_IMAGE_VERSION,121),[
      AC_CHECK_LIB(SDL_image, IMG_Load_RW, AC_DEFINE(SDL_IMAGE_VERSION,120),
        AC_MSG_ERROR(Cannot find SDL_image library.))	
    ])
    PARTS="$PARTS loader"
  fi
fi

# Checking for lib SDL_mixer
if test "$sdl_mixer" = yes ; then
  AC_CHECK_HEADERS(SDL_mixer.h, : , sdl_mixer=no)
  if test "$sdl_mixer" = yes ; then 
    AC_CHECK_LIB(SDL_mixer, Mix_OpenAudio, :,
      AC_MSG_ERROR(Cannot find SDL_mixer library.))
    PARTS="$PARTS mixer"
  fi
fi

AC_OUTPUT(makefile.config.gcc)

AC_MSG_NOTICE([updating makefile.platform])
sed -i -e "s/^PLATFORM.*/PLATFORM = $PLATFORM/" makefile.platform

# Output the configuration summary
echo
echo "Configure summary:"
if test -n "$LABLGLDIR" ; then lablgl_support=yes ; else lablgl_support=no ; fi
echo "  LablGL	" $lablgl_support
echo "  SDL_image	" $sdl_image
echo "  SDL_mixer	" $sdl_mixer
echo "  SDL_tff	" $sdl_ttf
echo
echo "Configure finished, type 'make' to build."
