dnl Check if we have SDL (sdl-config, header and library) version >= 1.2.0
dnl Extra options: --with-sdl-prefix=PATH and --with-sdl={sdl12,sdl2}
dnl Output:
dnl SDL_CFLAGS and SDL_LIBS are set and AC_SUBST-ed
dnl HAVE_SDL_H is AC_DEFINE-d

AC_DEFUN([EXULT_CHECK_SDL],[
  exult_backupcppflags="$CPPFLAGS"
  exult_backupldflags="$LDFLAGS"
  exult_backuplibs="$LIBS"

  exult_sdlok=yes

  AC_ARG_WITH(sdl-prefix,[  --with-sdl-prefix=PFX   Prefix where SDL is installed (optional)], sdl_prefix="$withval", sdl_prefix="")
  AC_ARG_WITH(sdl-exec-prefix,[  --with-sdl-exec-prefix=PFX Exec prefix where SDL is installed (optional)], sdl_exec_prefix="$withval", sdl_exec_prefix="")
  AC_ARG_WITH(sdl,       [  --with-sdl=sdl12,sdl2   Select a specific version of SDL to use (optional)], sdl_ver="$withval", sdl_ver="")

  dnl First: find sdl-config or sdl2-config
  exult_extra_path=$prefix/bin:$prefix/usr/bin
  sdl_args=""
  if test x$sdl_exec_prefix != x ; then
     sdl_args="$sdl_args --exec-prefix=$sdl_exec_prefix"
     exult_extra_path=$sdl_exec_prefix/bin
  fi
  if test x$sdl_prefix != x ; then
     sdl_args="$sdl_args --prefix=$sdl_prefix"
     exult_extra_path=$sdl_prefix/bin
  fi
  if test x"$sdl_ver" = xsdl12 ; then
    exult_sdl_progs=sdl-config
  elif test x"$sdl_ver" = xsdl2 ; then
    exult_sdl_progs=sdl2-config
  else
    dnl NB: This line implies we prefer SDL 2.0 to SDL 1.2
    exult_sdl_progs="sdl2-config sdl-config"
  fi
  AC_PATH_PROGS(SDL_CONFIG, $exult_sdl_progs, no, [$exult_extra_path:$PATH])
  if test "$SDL_CONFIG" = "no" ; then
    exult_sdlok=no
  else
    SDL_CFLAGS=`$SDL_CONFIG $sdl_args --cflags`
    SDL_LIBS=`$SDL_CONFIG $sdl_args --libs`

    SDL_MAJOR_VERSION=`$SDL_CONFIG $sdl_args --version | \
           sed 's/\([[0-9]]*\).\([[0-9]]*\).\([[0-9]]*\)/\1/'`
    sdl_minor_version=`$SDL_CONFIG $sdl_args --version | \
           sed 's/\([[0-9]]*\).\([[0-9]]*\).\([[0-9]]*\)/\2/'`
    sdl_patchlevel=`$SDL_CONFIG $sdl_args --version | \
           sed 's/\([[0-9]]*\).\([[0-9]]*\).\([[0-9]]*\)/\3/'`
    if test $SDL_MAJOR_VERSION -eq 1 ; then
      sdl_ver=sdl12
    else
      sdl_ver=sdl2
    fi
  fi

  if test x"$sdl_ver" = xsdl2 ; then
    REQ_MAJOR=2
    REQ_MINOR=0
    REQ_PATCHLEVEL=0
  else
    REQ_MAJOR=1
    REQ_MINOR=2
    REQ_PATCHLEVEL=0
  fi
  REQ_VERSION=$REQ_MAJOR.$REQ_MINOR.$REQ_PATCHLEVEL

  AC_MSG_CHECKING([for SDL - version >= $REQ_VERSION])


  dnl Second: include "SDL.h"

  if test x$exult_sdlok = xyes ; then
    CPPFLAGS="$CPPFLAGS $SDL_CFLAGS"
    AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
    #include "SDL.h"
    ]],)],sdlh_found=yes,sdlh_found=no)

    if test x$sdlh_found = xno; then
      exult_sdlok=no
    else
      AC_DEFINE(HAVE_SDL_H, 1, [Define to 1 if you have the "SDL.h" header file])
    fi
  fi

  dnl Next: version check (cross-compile-friendly idea borrowed from autoconf)
  dnl (First check version reported by sdl-config, then confirm
  dnl  the version in SDL.h matches it)

  if test x$exult_sdlok = xyes ; then

    if test ! \( \( $SDL_MAJOR_VERSION -gt $REQ_MAJOR \) -o \( \( $SDL_MAJOR_VERSION -eq $REQ_MAJOR \) -a \( \( $sdl_minor_version -gt $REQ_MINOR \) -o \( \( $sdl_minor_version -eq $REQ_MINOR \) -a \( $sdl_patchlevel -gt $REQ_PATCHLEVEL \) \) \) \) \); then
      exult_sdlok="no, version < $REQ_VERSION found"
    else
      AC_COMPILE_IFELSE([AC_LANG_SOURCE([[
      #include "SDL.h"

      int main()
      {
        static int test_array[1-2*!(SDL_MAJOR_VERSION==$SDL_MAJOR_VERSION&&SDL_MINOR_VERSION==$sdl_minor_version&&SDL_PATCHLEVEL==$sdl_patchlevel)];
        test_array[0] = 0;
        return 0;
      }
      ]])],,[[exult_sdlok="no, version of SDL.h doesn't match that of sdl-config"]])
    fi
  fi

  dnl Next: try linking

  if test "x$exult_sdlok" = xyes; then
    LIBS="$LIBS $SDL_LIBS"

    AC_LINK_IFELSE([AC_LANG_SOURCE([[
    #include "SDL.h"

    int main(int argc, char* argv[]) {
      SDL_Init(0);
      return 0;
    }
    ]])],sdllinkok=yes,sdllinkok=no)
    if test x$sdllinkok = xno; then
      exult_sdlok=no
    fi
  fi

  AC_MSG_RESULT($exult_sdlok)

  LDFLAGS="$exult_backupldflags"
  CPPFLAGS="$exult_backupcppflags"
  LIBS="$exult_backuplibs"

  if test "x$exult_sdlok" = xyes; then
    AC_SUBST(SDL_CFLAGS)
    AC_SUBST(SDL_LIBS)
    AC_SUBST(SDL_MAJOR_VERSION)
    ifelse([$1], , :, [$1])
  else
    ifelse([$2], , :, [$2])
  fi
]);


