dnl
dnl MapServer configure.in
dnl
dnl Process this file with autoconf to produce a configure script.
dnl
dnl $Id: configure.in,v 1.157 2006/08/28 14:09:23 hobu Exp $
dnl

AC_INIT(Makefile.in)

dnl Checks for programs.
AC_PROG_CC
AC_PROG_CXX
AC_PROG_RANLIB
AC_PROG_LEX
AC_PROG_YACC
AC_RUNPATH_SWITCH

dnl ---------------------------------------------------------------------
dnl Checks for libraries.
dnl ---------------------------------------------------------------------
AC_CHECK_LIB(m,exp, XTRALIBS="$XTRALIBS -lm",,)
AC_SUBST(XTRALIBS, $XTRALIBS)

AC_CHECK_LIB(stdc++,__gxx_personality_v0, XTRALIBS="$XTRALIBS -lstdc++",,)
AC_SUBST(XTRALIBS, $XTRALIBS)

LIBS="$XTRALIBS $LIBS"

dnl ---------------------------------------------------------------------
dnl Checks for header files.
dnl ---------------------------------------------------------------------
AC_HEADER_STDC

dnl ---------------------------------------------------------------------
dnl Add -fPIC to compiler flags if appropriate
dnl ---------------------------------------------------------------------
AC_COMPILER_PIC
CFLAGS="$CFLAGS $C_PIC"
CXXFLAGS="$CXXFLAGS $C_PIC"

dnl ---------------------------------------------------------------------
dnl Check for some string functions
dnl ---------------------------------------------------------------------
AC_CHECK_FUNC(strcasecmp, ,  STRINGS="-DNEED_STRCASECMP $STRINGS")
AC_CHECK_FUNC(strncasecmp, , STRINGS="-DNEED_STRNCASECMP $STRINGS")
AC_CHECK_FUNC(strdup, ,      STRINGS="-DNEED_STRDUP $STRINGS")
AC_CHECK_FUNC(strlcat, ,      STRINGS="-DNEED_STRLCAT $STRINGS")
AC_CHECK_FUNC(vsnprintf, STRINGS="-DHAVE_VSNPRINTF $STRINGS", )
AC_SUBST(STRINGS, $STRINGS)

dnl ---------------------------------------------------------------------
dnl Look for MapServer version in map.h
dnl ---------------------------------------------------------------------
MS_VERSION=`grep MS_VERSION map.h | grep '#define' | cut -d\" -f2`
AC_MSG_RESULT([MapServer Version from map.h: '$MS_VERSION'])
AC_SUBST(MS_VERSION,    "$MS_VERSION")

dnl ---------------------------------------------------------------------
dnl Several libraries might be needed for GD: png, zlib, freetype,
dnl and jpeg (processed above).  Use where GD was found as default.
dnl ---------------------------------------------------------------------

dnl ---------------------------------------------------------------------
dnl Check where FreeType is installed
dnl ---------------------------------------------------------------------

unset ac_cv_lib_freetype_FT_Init_FreeType
unset ac_cv_lib_ttf_TT_Init_FreeType

AC_ARG_WITH(freetype,
[  --with-freetype=DIR     GD: Specify where FreeType 2.x is installed (DIR is
                              path to freetype-config program or install dir).],
    FT_DIR=$withval,FT_DIR='')

AC_EXPAND_PATH($FT_DIR, FT_DIR)
AC_CHECKING(where FreeType 2.x is installed)
FT_LIB=''
FT_BIN=''
FT_TYPE=''

test -x "$FT_DIR" -a "freetype-config" = "`basename $FT_DIR ''`" && FT_BIN="$FT_DIR"
test -x "$FT_DIR/freetype-config" && FT_BIN="$FT_DIR/freetype-config"
test -x "$FT_DIR/bin/freetype-config" && FT_BIN="$FT_DIR/bin/freetype-config"

if test "$with_freetype" = "yes" ; then
 AC_PATH_PROG(FT_BIN, freetype-config, no)
fi

if test -n "$FT_BIN" ; then
  dnl we have freetype-config program, use its values
  FT_LIB=`$FT_BIN --libs`
  FT_LIBDIR="`$FT_BIN --exec-prefix`/lib"
  AC_MSG_RESULT([        using libfreetype from $FT_LIB])
  AC_ADD_RUNPATH("$FT_LIBDIR")

else
  dnl try freetype 2.x install lib name
  test -f $FT_DIR/lib/libfreetype.a -o -f $FT_DIR/lib/libfreetype.so -o -f $FT_DIR/lib/libfreetype.sl -o -f $FT_DIR/lib/libfreetype.dylib && FT_LIBDIR="$FT_DIR/lib"
  test -f $FT_DIR/lib64/libfreetype.a -o -f $FT_DIR/lib64/libfreetype.so -o -f $FT_DIR/lib64/libfreetype.sl && FT_LIBDIR="$FT_DIR/lib64"
  test -f $FT_DIR/libfreetype.a -o -f $FT_DIR/libfreetype.so -o -f $FT_DIR/libfreetype.sl -o -f $FT_DIR/libfreetype.dylib && FT_LIBDIR="$FT_DIR"

  if test -n "$FT_LIBDIR" ; then
     FT_TYPE="-lfreetype"  
  fi
fi

if test -n "$FT_LIBDIR" -a -z "$FT_BIN" ; then
  FT_LIB="-L$FT_LIBDIR $FT_TYPE"
  AC_MSG_RESULT([        using libfreetype from $FT_LIB])
  AC_ADD_RUNPATH("$FT_LIBDIR")
else
  dnl check for lib in system libs  
  AC_CHECK_LIB(freetype, FT_Init_FreeType, FT_LIB="-lfreetype",,)

  if test -n "$FT_LIB" ; then
    AC_MSG_RESULT([        using libfreetype $FT_LIB from system libs.])
  else
    AC_MSG_RESULT([        freetype-config or libfreetype cannot be found, possibly needed for GD])
  fi
fi


dnl ---------------------------------------------------------------------
dnl Check where zlib is installed (used by libpng and by ZVG compressed output)
dnl ---------------------------------------------------------------------

unset ac_cv_lib_z_zlibVersion

AC_ARG_WITH(zlib,
[  --with-zlib=DIR         GD,SVG: Specify where zlib is installed (DIR is path
                              to zlib install dir).],
    ZLIB_DIR=$withval,ZLIB_DIR='')

AC_EXPAND_PATH($ZLIB_DIR, ZLIB_DIR)
AC_CHECKING(where Zlib is installed)
ZLIB_LIB=''

test -f $ZLIB_DIR/include/zlib.h && ZLIB_INCDIR="$ZLIB_DIR/include"

test -f $ZLIB_DIR/lib/libz.a -o -f $ZLIB_DIR/lib/libz.so -o -f $ZLIB_DIR/lib/libz.sl -o -f $ZLIB_DIR/lib/libz.dylib && ZLIB_LIBDIR="$ZLIB_DIR/lib"
test -f $ZLIB_DIR/lib64/libz.a -o -f $ZLIB_DIR/lib64/libz.so -o -f $ZLIB_DIR/lib64/libz.sl && ZLIB_LIBDIR="$ZLIB_DIR/lib64"
test -f $ZLIB_DIR/libz.a -o -f $ZLIB_DIR/libz.so -o -f $ZLIB_DIR/libz.sl && ZLIB_LIBDIR="$ZLIB_DIR"


if test -n "$ZLIB_LIBDIR" -a -n "$ZLIB_INCDIR" ; then

  dnl
  dnl Found libz runtime and headers. Enable USE_ZLIB (SVG compressed output)
  dnl
  ZLIB_ENABLED="-DUSE_ZLIB"
  ZLIB_INC="-I$ZLIB_INCDIR"
  ZLIB_LIB="-L$ZLIB_LIBDIR -lz"
  AC_MSG_RESULT([        using libz from $ZLIB_LIB ($ZLIB_ENABLED)])
  AC_ADD_RUNPATH("$ZLIB_DIR")

elif test -n "$ZLIB_LIBDIR" ; then

  dnl
  dnl Found only runtime but no headers (good only for libpng)
  dnl
  ZLIB_LIB="-L$ZLIB_LIBDIR -lz"
  AC_MSG_RESULT([        using runtime libz from $ZLIB_LIB])
  AC_ADD_RUNPATH("$ZLIB_DIR")

else

  dnl check for lib in system libs

  AC_CHECK_LIB(z, zlibVersion, ZLIB_LIB="-lz",,)

  if test -n "$ZLIB_LIB" ; then
    ZLIB_ENABLED="-DUSE_ZLIB"
    AC_MSG_RESULT([        using libz from system libs ($ZLIB_ENABLED).])
  else
    AC_MSG_RESULT([        Zlib (libz) library cannot be found, possibly needed for GD])
  fi

fi

dnl If we found ZLIB headers then enable it by default since SVG output
dnl is enabled by default and will take advantage of it, otherwise the 
dnl PNG code below will take care of including the runtime lib only.

AC_SUBST(ZLIB_ENABLED,$ZLIB_ENABLED)
AC_SUBST(ZLIB_INC,    $ZLIB_INC)
AC_SUBST(ZLIB_LIB,    $ZLIB_LIB)

ALL_ENABLED="$ZLIB_ENABLED $ALL_ENABLED"
ALL_INC="$ZLIB_INC $ALL_INC"
ALL_LIB="$ZLIB_LIB $ALL_LIB"

dnl ---------------------------------------------------------------------
dnl Check where PNG is installed
dnl ---------------------------------------------------------------------

unset ac_cv_lib_png_png_init_io

AC_ARG_WITH(png,
[  --with-png=DIR          GD: Specify where PNG is installed (DIR is path to
                              PNG install dir).],
    PNG_DIR=$withval,PNG_DIR='')

AC_EXPAND_PATH($PNG_DIR, PNG_DIR)
AC_CHECKING(where PNG is installed)
PNG_LIB=''

test -f $PNG_DIR/lib/libpng.a -o -f $PNG_DIR/lib/libpng.so -o -f $PNG_DIR/lib/libpng.sl -o -f $PNG_DIR/lib/libpng.dylib && PNG_LIBDIR="$PNG_DIR/lib"
test -f $PNG_DIR/lib64/libpng.a -o -f $PNG_DIR/lib64/libpng.so -o -f $PNG_DIR/lib64/libpng.sl -o -f $PNG_DIR/libpng.dylib && PNG_LIBDIR="$PNG_DIR/lib64"
test -f $PNG_DIR/libpng.a -o -f $PNG_DIR/libpng.so -o -f $PNG_DIR/libpng.sl -o -f $PNG_DIR/libpng.dylib && PNG_LIBDIR="$PNG_DIR"

if test -n "$PNG_LIBDIR" ; then

  PNG_LIB="-L$PNG_LIBDIR -lpng"
  AC_MSG_RESULT([        using libpng from $PNG_LIB])
  AC_ADD_RUNPATH("$PNG_LIBDIR")

else

  dnl check for lib in system libs

  AC_CHECK_LIB(png, png_init_io, PNG_LIB="-lpng",, $ZLIB_LIB)

  if test -n "$PNG_LIB" ; then
    AC_MSG_RESULT([        using libpng from system libs.])
  else
    AC_MSG_RESULT([        PNG (libpng) library cannot be found, possibly needed for GD])
  fi

fi

dnl ---------------------------------------------------------------------
dnl libJPEG (included by default if found on system).
dnl ---------------------------------------------------------------------

unset ac_cv_lib_jpeg_jpeg_read_header

AC_CHECKING(whether we should include JPEG support)
AC_ARG_WITH(jpeg,
[  --with-jpeg[=DIR]       Include JPEG support (DIR is LibJPEG's install dir).],,)

if test -n "$with_jpeg" -a "$with_jpeg" != "yes" ; then

  AC_EXPAND_PATH($with_jpeg, JPEG_DIR)
  test -f $JPEG_DIR/include/jpeg/jpeglib.h && JPEG_INCLUDE="$JPEG_DIR/include/jpeg"
  test -f $JPEG_DIR/include/jpeglib.h && JPEG_INCLUDE="$JPEG_DIR/include"
  test -f $JPEG_DIR/lib/jpeglib.h && JPEG_INCLUDE="$JPEG_DIR/lib"
  test -f $JPEG_DIR/jpeglib.h && JPEG_INCLUDE="$JPEG_DIR"

  test -f $JPEG_DIR/lib/libjpeg.a && JPEG_LIBDIR="$JPEG_DIR/lib"
  test -f $JPEG_DIR/lib64/libjpeg.a && JPEG_LIBDIR="$JPEG_DIR/lib64"
  test -f $JPEG_DIR/libjpeg.a && JPEG_LIBDIR="$JPEG_DIR"

  test -f $JPEG_DIR/lib/libjpeg.so -o -f $JPEG_DIR/lib/libjpeg.sl -o -f $JPEG_DIR/lib/libjpeg.dylib && JPEG_LIBDIR="$JPEG_DIR/lib"
  test -f $JPEG_DIR/lib64/libjpeg.so -o -f $JPEG_DIR/lib64/libjpeg.sl && JPEG_LIBDIR="$JPEG_DIR/lib64"
  test -f $JPEG_DIR/libjpeg.so -o -f $JPEG_DIR/libjpeg.sl -o -f $JPEG_DIR/libjpeg.dylib && JPEG_LIBDIR="$JPEG_DIR"

  if test -n "$JPEG_INCLUDE" -a -n "$JPEG_LIBDIR" ; then
      JPEG_ENABLED="-DUSE_JPEG"
      JPEG_INC=-I$JPEG_INCLUDE
      JPEG_LIB="-L$JPEG_LIBDIR -ljpeg"
      AC_ADD_RUNPATH("$JPEG_LIBDIR")
  else
      if test "$with_jpeg" != "no" ; then
        AC_MSG_RESULT("Could not find jpeglib.h or libjpeg.a/libjpeg.so in $JPEG_DIR.")
      fi
  fi

  dnl if --without-jpeg was requested, disable mapserver from using it, but
  dnl libjpeg may still be needed by GD

  if test "$with_jpeg" = "no" ; then
    AC_MSG_RESULT([        disabled by --without-jpeg])
  else
    JPEG_ENABLED="-DUSE_JPEG"
  fi

  if test -z "$JPEG_LIB" ; then

    AC_CHECK_LIB(jpeg, jpeg_read_header, JPEG_LIB="-ljpeg",,,)

    if test -n "$JPEG_LIB" ; then
        JPEG_LIB="-ljpeg"
        AC_MSG_RESULT([        using libjpeg from system libs.])
    else
        AC_MSG_RESULT([        libjpeg not found. JPEG support not included, possibly needed for GD.])
    fi
  else
     AC_MSG_RESULT([        using libjpeg from system $JPEG_LIB])
  fi

else

  AC_CHECK_LIB(jpeg, jpeg_read_header, JPEG_ENABLED=-DUSE_JPEG,,,)

  if test -n "$JPEG_ENABLED" ; then
      JPEG_LIB="-ljpeg"
      AC_MSG_RESULT([        using libjpeg from system libs.])
  else
      AC_MSG_RESULT([        libjpeg not found. JPEG support not included, possibly needed for GD.])
  fi

fi

AC_SUBST(JPEG_ENABLED,$JPEG_ENABLED)
AC_SUBST(JPEG_INC,    $JPEG_INC)
AC_SUBST(JPEG_LIB,    $JPEG_LIB)

dnl ---------------------------------------------------------------------
dnl Look for X11 and XPM libs used by GD
dnl ---------------------------------------------------------------------

unset ac_cv_lib_Xpm_XpmFreeXpmImage

AC_ARG_WITH(xpm,
[  --with-xpm=DIR          GD: Specify where libXpm is installed (DIR it the 
                              libXpm install prefix).],
    XPM_DIR=$withval,XPM_DIR='')

if test "$with_xpm" != "no" ; then

  AC_EXPAND_PATH($XPM_DIR, XPM_DIR)
  AC_CHECKING(where libXpm is installed)
  XPM_LIB=''

  test -f $XPM_DIR/lib/libXpm.a -o -f $XPM_DIR/lib/libXpm.so -o -f $XPM_DIR/lib/libXpm.sl -o -f $XPM_DIR/lib/libXpm.dylib && XPM_LIBDIR="$XPM_DIR/lib"
  test -f $XPM_DIR/lib64/libXpm.a -o -f $XPM_DIR/lib64/libXpm.so -o -f $XPM_DIR/lib64/libXpm.sl && XPM_LIBDIR="$XPM_DIR/lib64"
  test -f $XPM_DIR/libXpm.a -o -f $XPM_DIR/libXpm.so -o -f $XPM_DIR/libXpm.sl -o -f $XPM_DIR/libXpm.dylib && XPM_LIBDIR="$XPM_DIR"

  if test -n "$XPM_LIBDIR" ; then

    XPM_LIB="-L$XPM_LIBDIR -lXpm -lX11"
    AC_MSG_RESULT([        using libXpm from $XPM_LIB])
    AC_ADD_RUNPATH("$XPM_LIBDIR")

  else
  
    dnl check for lib in system libs

    AC_CHECK_LIB(Xpm, XpmFreeXpmImage, XPM_LIB="-lXpm -lX11",,-lX11)

    if test -n "$XPM_LIB" ; then
      AC_MSG_RESULT([        using libXpm from system libs.])
    else
      AC_MSG_RESULT([        XPM (libXpm) library cannot be found, possibly needed for GD])
    fi
  fi

fi

dnl Note: don't include iconv by default in GD_XTRA_LIBS as it may not
dnl       be required by GD.

GD_XTRA_LIBS="$JPEG_LIB $FT_LIB $PNG_LIB $ZLIB_LIB $XPM_LIB"


dnl ---------------------------------------------------------------------
dnl Check where libiconv is installed. We always look for it, even if not
dnl explicitly requested since GD may require it for linking.
dnl Iconv can come either as libiconv.so or as part of glibc
dnl ---------------------------------------------------------------------

unset ac_cv_lib_iconv_libiconv_open
unset ac_cv_lib_c_iconv_open

AC_ARG_WITH(libiconv,
[  --with-libiconv=DIR     Specify where libiconv is installed (DIR is path
                          to libiconv install dir (prefix)).],
    ICONV_DIR=$withval,ICONV_DIR='')

AC_CHECKING(where libiconv is installed)

ICONV_LIB=''

if test -n "$ICONV_DIR" ; then

  AC_EXPAND_PATH($ICONV_DIR, ICONV_DIR)

  test -f $ICONV_DIR/include/iconv.h && ICONV_LIBDIR="$ICONV_DIR/lib" && ICONV_INCDIR="$ICONV_DIR/include"
  test -f $ICONV_DIR/lib/libiconv.a -o -f $ICONV_DIR/lib/libiconv.so -o -f $ICONV_DIR/lib/libiconv.sl -o -f $ICONV_DIR/lib/libiconv.dylib && ICONV_LIBDIR="$ICONV_DIR/lib" && ICONV_INCDIR="$ICONV_DIR/include"
  test -f $ICONV_DIR/lib64/libiconv.a -o -f $ICONV_DIR/lib64/libiconv.so -o -f $ICONV_DIR/lib64/libiconv.sl && ICONV_LIBDIR="$ICONV_DIR/lib64" && ICONV_INCDIR="$ICONV_DIR/include"

  dnl If we have both the glibc and a local libiconv, then the local libiconv
  dnl will be included in the link line no matter what. This shouldn't hurt
  dnl programs that don't need it, and saves us from having to figure some
  dnl black magic to detect what was used to build GD, etc.
  dnl Just doing the AC_CHECK_LIB(iconv ...) last does the trick.
  AC_CHECK_LIB(c, iconv_open, ICONV_LIB="-L$ICONV_LIBDIR -lc",,"-L$ICONV_LIBDIR")
  AC_CHECK_LIB(iconv, libiconv_open, ICONV_LIB="-L$ICONV_LIBDIR -liconv",,"-L$ICONV_LIBDIR")

fi

if test -n "$ICONV_LIB" ; then
  if test -n "$ICONV_INCDIR"; then 
     ICONV_INC="-I$ICONV_INCDIR"
  fi
  AC_MSG_RESULT([        using libiconv from $ICONV_LIB])
  AC_ADD_RUNPATH("$ICONV_DIR")

else

  dnl check for lib in system libs

  unset ac_cv_lib_iconv_libiconv_open
  unset ac_cv_lib_c_iconv_open
  AC_CHECK_LIB(c, iconv_open, ICONV_LIB="-lc",,)
  AC_CHECK_LIB(iconv, libiconv_open, ICONV_LIB="-liconv",,)

  if test -n "$ICONV_LIB" ; then
    AC_MSG_RESULT([        using libiconv from system libs.])
  else
    AC_MSG_RESULT([        libiconv library cannot be found, possibly needed for GD])
  fi

fi

if test -n "$ICONV_LIB" ; then
  ICONV_ENABLED="-DUSE_ICONV"
  AC_MSG_RESULT([        libiconv found. Enabling internationalization ($ICONV_ENABLED)])
fi


AC_SUBST(ICONV_ENABLED, $ICONV_ENABLED)
AC_SUBST(ICONV_INC,     $ICONV_INC)
AC_SUBST(ICONV_LIB,     $ICONV_LIB)

ALL_ENABLED="$ICONV_ENABLED $ALL_ENABLED"
ALL_INC="$ICONV_INC $ALL_INC"
ALL_LIB="$ICONV_LIB $ALL_LIB"


dnl ---------------------------------------------------------------------
dnl Find GD library
dnl
dnl If --with-gd not given, then check in system libs.
dnl ---------------------------------------------------------------------

dnl We don't want LIBS updated by all the GD stuff, so we save it and
dnl restore later.

ms_saved_LIBS="$LIBS"
IS_GD2="false";

dnl Clear some cache variables
unset ac_cv_lib_gd_gdImageCreate
unset ac_cv_lib_gd_gdImageString16
unset ac_cv_lib_gd_gdImageGif
unset ac_cv_lib_gd_gdImagePng
unset ac_cv_lib_gd_gdImageJpeg
unset ac_cv_lib_gd_gdImageWBMP
unset ac_cv_lib_gd_gdImageStringFT
unset ac_cv_lib_gd_gdImageGifPtr
unset ac_cv_lib_gd_gdFontGetTiny
unset ac_cv_lib_c_gdImageCreate
unset ac_cv_lib_c_gdImageString16
unset ac_cv_lib_c_gdImageGif
unset ac_cv_lib_c_gdImagePng
unset ac_cv_lib_c_gdImageJpeg
unset ac_cv_lib_c_gdImageWBMP
unset ac_cv_lib_c_gdImageStringFT
unset ac_cv_lib_c_gdImageGifPtr
unset ac_cv_lib_c_gdFontGetTiny
unset ac_cv_lib_gd_gdFontCacheSetup

AC_CHECKING(for GD 2.0.16 or higher)
AC_ARG_WITH(gd,
[  --with-gd[=[static,]DIR] Specify which version of GD to use (DIR is GD's
                          install dir).],,)

if test "$with_gd" = "no" ; then
  AC_MSG_ERROR(GD library cannot be disabled)
elif test -n "$with_gd" -a "$with_gd" != "yes" ; then
  dnl
  dnl --with-gd was specified explicitly... 
  dnl
  AC_PARSE_WITH_LIB_STATIC($with_gd, GD_DIR, GD_STATIC)
  AC_EXPAND_PATH($GD_DIR, GD_DIR)
  test -f $GD_DIR/include/gd.h && GD_INCLUDE="$GD_DIR/include"
  test -f $GD_DIR/include/gd/gd.h && GD_INCLUDE="$GD_DIR/include/gd"
  test -f $GD_DIR/gd.h && GD_INCLUDE="$GD_DIR"

  test -f $GD_DIR/lib/libgd.a && GD_LIBDIR="$GD_DIR/lib"
  test -f $GD_DIR/lib64/libgd.a && GD_LIBDIR="$GD_DIR/lib64"
  test -f $GD_DIR/.libs/libgd.a && GD_LIBDIR="$GD_DIR/.libs"
  test -f $GD_DIR/_libs/libgd.a && GD_LIBDIR="$GD_DIR/_libs"
  test -f $GD_DIR/libgd.a && GD_LIBDIR="$GD_DIR"

  test -f $GD_DIR/lib/libgd.so -o -f $GD_DIR/lib/libgd.sl -o -f $GD_DIR/lib/libgd.dylib && GD_LIBDIR="$GD_DIR/lib"
  test -f $GD_DIR/lib64/libgd.so -o -f $GD_DIR/lib/libgd.sl && GD_LIBDIR="$GD_DIR/lib64"
  test -f $GD_DIR/.libs/libgd.so -o -f $GD_DIR/.libs/libgd.sl -o -f $GD_DIR/.libs/libgd.dylib && GD_LIBDIR="$GD_DIR/.libs"
  test -f $GD_DIR/_libs/libgd.so -o -f $GD_DIR/_libs/libgd.sl -o -f $GD_DIR/_libs/libgd.dylib && GD_LIBDIR="$GD_DIR/_libs"
  test -f $GD_DIR/libgd.so -o -f $GD_DIR/libgd.sl -o -f $GD_DIR/libgd.dylib && GD_LIBDIR="$GD_DIR"

  dnl make sure it is GD 2.0.16+ with antialias and gdFontCacheSetup() support
  AC_CHECK_LIB(gd, gdFontCacheSetup,IS_GD2="true",,$GD_XTRA_LIBS -L$GD_LIBDIR)

  if test "$IS_GD2" = "false" -a -n "$ICONV_LIB"; then
    dnl try again including libiconv this time
    unset ac_cv_lib_gd_gdFontCacheSetup
    AC_CHECKING(whether GD needs libiconv)
    AC_CHECK_LIB(gd, gdFontCacheSetup,IS_GD2="true",,$GD_XTRA_LIBS -L$GD_LIBDIR $ICONV_LIB)
    GD_NEED_ICONV_LIB="$ICONV_LIB"
  fi

  if test -n "$GD_INCLUDE" -a -n "$GD_LIBDIR" -a "$IS_GD2" = "true"; then
      GD_INC=-I$GD_INCLUDE
      GD_LIB="-L$GD_LIBDIR -lgd"
      GD_XTRA_LIBS="$GD_XTRA_LIBS $GD_NEED_ICONV_LIB"
      AC_MSG_RESULT([        using libgd 2.0.16 (or higher) from $GD_LIB $GD_XTRA_LIBS])
      AC_ADD_RUNPATH("$GD_LIBDIR")
  else
      AC_MSG_ERROR([Could not find gd.h or libgd.a/libgd.so in $GD_DIR.  Make sure GD 2.0.16 or higher is compiled before calling configure. You may also get this error if you didn't specify the appropriate location for one of GD's dependencies (freetype, libpng, libjpeg or libiconv).])
  fi
else
  dnl  --with-gd specified without a DIR, look for gd in system dirs /usr or /usr/local
  FOUND_GD="false"
  for GD_DIR in /usr /usr/local
  do
    test -f $GD_DIR/include/gd.h && GD_INCLUDE="$GD_DIR/include"
    test -f $GD_DIR/lib/libgd.a && GD_LIBDIR="$GD_DIR/lib"
    test -f $GD_DIR/lib64/libgd.a && GD_LIBDIR="$GD_DIR/lib64"
    test -f $GD_DIR/lib/libgd.so -o -f $GD_DIR/lib/libgd.sl -o -f $GD_DIR/lib/libgd.dylib && GD_LIBDIR="$GD_DIR/lib"
    test -f $GD_DIR/lib64/libgd.so -o -f $GD_DIR/lib/libgd.sl && GD_LIBDIR="$GD_DIR/lib64"

    dnl make sure it is GD 2.0.16 or higher
    AC_CHECK_LIB(gd, gdFontCacheSetup, IS_GD2="true",,$GD_XTRA_LIBS)
 
    if test "$IS_GD2" = "false" -a -n "$ICONV_LIB"; then
      dnl try again including libiconv this time
      AC_CHECKING(whether GD needs libiconv)
      unset ac_cv_lib_gd_gdFontCacheSetup
      AC_CHECK_LIB(gd, gdFontCacheSetup,IS_GD2="true",,$GD_XTRA_LIBS $ICONV_LIB)
      GD_NEED_ICONV_LIB="$ICONV_LIB"
    fi

    if test -n "$GD_INCLUDE" -a -n "$GD_LIBDIR" -a "$IS_GD2" = "true"; then
      GD_INC=-I$GD_INCLUDE
      GD_LIB="-L$GD_LIBDIR -lgd"
      GD_XTRA_LIBS="$GD_XTRA_LIBS $GD_NEED_ICONV_LIB"
      AC_MSG_RESULT([        using libgd 2.0.16 (or higher) from system libs ($GD_LIB $GD_XTRA_LIBS).])
      AC_ADD_RUNPATH("$GD_LIBDIR")
      FOUND_GD="true"
      break;
    fi
  done

  if test "$FOUND_GD" = "false" ; then
    AC_MSG_ERROR([Could not find gd.h or libgd.a/libgd.so in $GD_DIR.  Make sure GD 2.0.16 or higher is compiled before calling configure. You may also get this error if you didn't specify the appropriate location for one of GD's dependencies (freetype, libpng, libjpeg or libiconv).])
  fi
fi

dnl ----------------------------------------------------------------------
dnl Continue with GD processing - determine GD formats and other features
dnl ----------------------------------------------------------------------

if test -n "$with_gd" -a "$with_gd" != "yes" ; then

  dnl check gd lib that was found on --with-gd=

  if test "$GD_STATIC" = "yes" ; then
    dnl To test against static GD, do AC_CHECK_LIB calls against -lc and
    dnl include libgd.a in GD_LIB param
    GD_CHECKLIB=c
    GD_LIB=""
    GD_STATIC=$GD_LIBDIR/libgd.a
    AC_MSG_RESULT([static linkage of $GD_STATIC requested... testing supported formats...])
  else
    GD_CHECKLIB=c
    # GD_LIB was already set above
    GD_STATIC=""
  fi

  AC_CHECK_LIB($GD_CHECKLIB, gdImageGif, GD_DEF="$GD_DEF -DUSE_GD_GIF",, $GD_STATIC $GD_LIB $GD_XTRA_LIBS)
  AC_CHECK_LIB($GD_CHECKLIB, gdImagePng, GD_DEF="$GD_DEF -DUSE_GD_PNG",, $GD_STATIC $GD_LIB $GD_XTRA_LIBS)
  AC_CHECK_LIB($GD_CHECKLIB, gdImageJpeg, GD_DEF="$GD_DEF -DUSE_GD_JPEG",, $GD_STATIC $GD_LIB $GD_XTRA_LIBS)
  AC_CHECK_LIB($GD_CHECKLIB, gdImageWBMP, GD_DEF="$GD_DEF -DUSE_GD_WBMP",, $GD_STATIC $GD_LIB $GD_XTRA_LIBS)

  dnl check for Freetype 2.x support  
  AC_CHECK_LIB($GD_CHECKLIB, gdImageStringFT, GD_DEF="$GD_DEF -DUSE_GD_FT",, $GD_STATIC $GD_LIB $GD_XTRA_LIBS)

  dnl check for gdImageOpenPolygon (means GD version 2.0.29 or higher)
  AC_CHECK_LIB($GD_CHECKLIB, gdImageOpenPolygon, GD_DEF="$GD_DEF -DGD_HAS_FTEX_XSHOW",, $GD_STATIC $GD_LIB $GD_XTRA_LIBS)

  dnl gdImageGifPtr is there only in GIF-patched GD versions
  AC_CHECK_LIB($GD_CHECKLIB, gdImageGifPtr, GD_DEF="$GD_DEF -DGD_HAS_GDIMAGEGIFPTR",, $GD_STATIC $GD_LIB $GD_XTRA_LIBS)

  dnl GD 2.0.22 and more recent replaced the static font pointers with 
  dnl getter functions.
  AC_CHECK_LIB($GD_CHECKLIB, gdFontGetTiny, GD_DEF="$GD_DEF -DGD_HAS_GETBITMAPFONTS",, $GD_STATIC $GD_LIB $GD_XTRA_LIBS)

  AC_MSG_RESULT([        using GD ($GD_DEF) from $GD_DIR.])

else

  dnl
  dnl --with-gd was not given... then look in system libs
  dnl

  dnl Make sure GD is at least there!
  AC_CHECK_LIB(gd, gdImageCreate, GD_FOUND=yes , ,$GD_XTRA_LIBS)
  if test -z "$GD_FOUND" ; then
      AC_MSG_ERROR([GD is required but was not found... use --with-gd=DIR to specify its location.])
  fi

  dnl Check supported formats
  AC_CHECK_LIB(gd, gdImageGif, GD_DEF="$GD_DEF -DUSE_GD_GIF",, $GD_LIB $GD_XTRA_LIBS)
  AC_CHECK_LIB(gd, gdImagePng, GD_DEF="$GD_DEF -DUSE_GD_PNG",, $GD_LIB $GD_XTRA_LIBS)
  AC_CHECK_LIB(gd, gdImageJpeg, GD_DEF="$GD_DEF -DUSE_GD_JPEG",, $GD_LIB $GD_XTRA_LIBS)
  AC_CHECK_LIB(gd, gdImageWBMP, GD_DEF="$GD_DEF -DUSE_GD_WBMP",, $GD_LIB $GD_XTRA_LIBS)

  dnl Check for Freetype 2 support
  AC_CHECK_LIB(gd, gdImageStringFT, GD_DEF="$GD_DEF -DUSE_GD_FT",, $GD_LIB $GD_XTRA_LIBS)

  dnl check for gdImageOpenPolygon (means GD version 2.0.29 or higher)
  AC_CHECK_LIB(gd, gdImageOpenPolygon, GD_DEF="$GD_DEF -DGD_HAS_FTEX_XSHOW",, $GD_STATIC $GD_LIB $GD_XTRA_LIBS)

  dnl gdImageGifPtr is there only in GIF-patched GD versions
  AC_CHECK_LIB(gd, gdImageGifPtr, GD_DEF="$GD_DEF -DGD_HAS_GDIMAGEGIFPTR",, $GD_LIB $GD_XTRA_LIBS)

  dnl GD 2.0.22 and more recent replaced the static font pointers with 
  dnl getter functions.
  AC_CHECK_LIB(gd, gdFontGetTiny, GD_DEF="$GD_DEF -DGD_HAS_GETBITMAPFONTS",, $GD_LIB $GD_XTRA_LIBS)

  if test -n "$GD_DEF" ; then
      GD_LIB="-lgd $GD_LIB"
      AC_MSG_RESULT([        using GD ($GD_DEF) from system libs.])
  fi
fi

GD_LIB="$GD_LIB $GD_XTRA_LIBS"

AC_SUBST(GD_DEF, $GD_DEF)
AC_SUBST(GD_INC, $GD_INC)
AC_SUBST(GD_LIB, $GD_LIB)
AC_SUBST(GD_STATIC, $GD_STATIC)
AC_SUBST(MAKE_GD, $MAKE_GD)
AC_SUBST(MAKE_GD_CLEAN, $MAKE_GD_CLEAN)

ALL_ENABLED="$GD_DEF $ALL_ENABLED"
ALL_INC="$GD_INC $ALL_INC"
ALL_LIB="$GD_LIB $ALL_LIB"
ALL_STATIC_LIB="$GD_STATIC $ALL_STATIC_LIB"

dnl Restore LIBS environment to state at beginning of gd tests.
LIBS="$ms_saved_LIBS"

dnl ---------------------------------------------------------------------
dnl Find PDFlib library
dnl
dnl If --with-pdflib not given, then check in system libs.
dnl ---------------------------------------------------------------------

dnl We don't want LIBS updated by all the PDF stuff, so we save it and
dnl restore later.

ms_saved_LIBS="$LIBS"

dnl Clear some cache variables
unset ac_cv_lib_pdf_PDF_setlinewidth
unset ac_cv_lib_pdf_PDF_setrgbcolor
unset ac_cv_lib_pdf_PDF_moveto
unset ac_cv_lib_pdf_PDF_curveto
unset ac_cv_lib_pdf_PDF_show_xy

AC_CHECKING(whether we should include PDF support)
AC_ARG_WITH(pdf,
[  --without-pdf           Disable PDF support.
  --with-pdf[=DIR]        Include PDF support (DIR is PDFlib's install dir).],,)
if test "$with_pdf" = "no" ; then
  AC_MSG_RESULT([        disabled by --without-pdf])
elif test -n "$with_pdf" -a "$with_pdf" != "yes" ; then

  AC_EXPAND_PATH($with_pdf, PDF_DIR)
  test -f $PDF_DIR/include/pdflib.h && PDF_INCLUDE="$PDF_DIR/include"
  test -f $PDF_DIR/libpdf/pdflib.h && PDF_INCLUDE="$PDF_DIR/libpdf"
  test -f $PDF_DIR/include/libpdf/pdflib.h && PDF_INCLUDE="$PDF_DIR/include/libpdf"
  test -f $PDF_DIR/pdflib.h && PDF_INCLUDE="$PDF_DIR"

  test -f $PDF_DIR/lib/libpdf.a && PDF_LIBDIR="$PDF_DIR/lib"
  test -f $PDF_DIR/lib64/libpdf.a && PDF_LIBDIR="$PDF_DIR/lib64"
  test -f $PDF_DIR/libpdf/libpdf.a && PDF_LIBDIR="$PDF_DIR/libpdf"
  test -f $PDF_DIR/libpdf.a && PDF_LIBDIR="$PDF_DIR"

  test -f $PDF_DIR/lib/libpdf.so -o -f $PDF_DIR/lib/libpdf.sl -o -f $PDF_DIR/lib/libpdf.dylib && PDF_LIBDIR="$PDF_DIR/lib"
  test -f $PDF_DIR/lib64/libpdf.so -o -f $PDF_DIR/lib64/libpdf.sl && PDF_LIBDIR="$PDF_DIR/lib64"
  test -f $PDF_DIR/libpdf/libpdf.so -o -f $PDF_DIR/libpdf/libpdf.sl -o -f $PDF_DIR/libpdf/libpdf.dylib && PDF_LIBDIR="$PDF_DIR/libpdf"
  test -f $PDF_DIR/libpdf.so -o -f $PDF_DIR/libpdf.sl -o -f $PDF_DIR/libpdf.dylib && PDF_LIBDIR="$PDF_DIR"

  if test -n "$PDF_INCLUDE" -a -n "$PDF_LIBDIR" ; then

      PDF_LIB="-L$PDF_LIBDIR -lpdf"
      PDF_INC=-I$PDF_INCLUDE
      AC_ADD_RUNPATH("$PDF_LIBDIR")
  else
      AC_MSG_ERROR("Could not find pdflib.h or libpdf.a/libpdf.so in $PDF_DIR.")
  fi

  PDF_ENABLED="-DUSE_PDF"
  AC_MSG_RESULT([        using libpdf from $PDF_LIB.])

else

  AC_CHECK_LIB(pdf,PDF_setlinewidth,PDF_LIB="-lpdf",,)
  AC_CHECK_LIB(pdf,PDF_setrgbcolor,PDF_LIB="-lpdf",,)
  AC_CHECK_LIB(pdf,PDF_moveto,PDF_LIB="-lpdf",,)
  AC_CHECK_LIB(pdf,PDF_curveto,PDF_LIB="-lpdf",,)
  AC_CHECK_LIB(pdf,PDF_show_xy,PDF_LIB="-lpdf",,)
  if test -n "$PDF_LIB" ; then
      PDF_ENABLED=-DUSE_PDF
      AC_MSG_RESULT([        using libpdf from system libs.])
  else
      AC_MSG_RESULT([        libpdf not found... PDF support not included.])
  fi

fi

dnl
dnl libpdf may need the same libs (libz, libjpeg) that are included with GD
dnl
PDF_LIB="$PDF_LIB"

AC_SUBST(PDF_ENABLED,$PDF_ENABLED)
AC_SUBST(PDF_INC,    $PDF_INC)
AC_SUBST(PDF_LIB,    $PDF_LIB)

ALL_ENABLED="$PDF_ENABLED $ALL_ENABLED"
ALL_INC="$PDF_INC $ALL_INC"
ALL_LIB="$PDF_LIB $ALL_LIB"

dnl ---------------------------------------------------------------------
dnl EPPL7 support (included by default)
dnl ---------------------------------------------------------------------

AC_CHECKING(whether we should include EPPL7 support)
AC_ARG_WITH(eppl,
[  --without-eppl        Disable EPPL7 support.
  --with-eppl           Include EPPL7 support.],,)
if test "$with_eppl" = "no" ; then
  AC_MSG_RESULT([        disabled by --without-eppl])
else

  EPPL_OBJ="epplib.o"
  EPPL_ENABLED="-DUSE_EPPL"

  AC_MSG_RESULT([        including EPPL7 support.])

fi

AC_SUBST(EPPL_ENABLED,$EPPL_ENABLED)
AC_SUBST(EPPL_OBJ,    $EPPL_OBJ)

ALL_ENABLED="$EPPL_ENABLED $ALL_ENABLED"

dnl ---------------------------------------------------------------------
dnl Find PROJ.4 ONLY IF REQUESTED.
dnl ---------------------------------------------------------------------

dnl Clear some cache variables
unset ac_cv_lib_proj_pj_init
unset ac_cv_lib_proj_pj_transform
unset ac_cv_lib_proj_pj_latlong_from_proj

AC_CHECKING(whether we should include PROJ.4 support)
AC_ARG_WITH(proj,
[  --with-proj[=DIR]       Include PROJ.4 support (DIR is PROJ.4's install dir).],,)

if test "$with_proj" = "yes" ; then

  AC_CHECK_LIB(proj,pj_init,PROJ_ENABLED=-DUSE_PROJ,,,)
  AC_CHECK_LIB(proj,pj_transform,PJ_TRANSFORM=yes,PJ_TRANSFORM=no,,)

  if test -n "$PROJ_ENABLED" -a "$PJ_TRANSFORM" = "no"; then
      AC_MSG_ERROR([This version of PROJ is too old.  PROJ4.4.2 or later is required.])
  elif test -n "$PROJ_ENABLED" -a "$PJ_TRANSFORM" = "yes"; then
      PROJ_LIBS="-lproj"
      AC_MSG_RESULT([        using PROJ.4 from system libs.])
  else
      AC_MSG_WARN([        PROJ.4 not found in system libs... use --with-proj=DIR.])
  fi

elif test -n "$with_proj" -a "$with_proj" != "no" ; then

  AC_EXPAND_PATH($with_proj, PROJ_DIR)
  test -f $PROJ_DIR/include/proj_api.h && PROJ_INCLUDE="$PROJ_DIR/include"
  test -f $PROJ_DIR/include/proj/proj_api.h && PROJ_INCLUDE="$PROJ_DIR/include/proj"
  test -f $PROJ_DIR/src/proj_api.h && PROJ_INCLUDE="$PROJ_DIR/src"
  test -f $PROJ_DIR/proj_api.h && PROJ_INCLUDE="$PROJ_DIR"

  test -f $PROJ_DIR/src/.libs/libproj.a && PROJ_LIBDIR="$PROJ_DIR/src/.libs"
  test -f $PROJ_DIR/lib/libproj.a && PROJ_LIBDIR="$PROJ_DIR/lib"
  test -f $PROJ_DIR/lib64/libproj.a && PROJ_LIBDIR="$PROJ_DIR/lib64"
  test -f $PROJ_DIR/libproj.a && PROJ_LIBDIR="$PROJ_DIR"

  test -f $PROJ_DIR/src/.libs/libproj.so -o -f $PROJ_DIR/src/.libs/libproj.sl -o -f $PROJ_DIR/src/.libs/libproj.dylib && PROJ_LIBDIR="$PROJ_DIR/src/.libs"
  test -f $PROJ_DIR/lib/libproj.so -o -f $PROJ_DIR/lib/libproj.sl -o -f $PROJ_DIR/lib/libproj.dylib && PROJ_LIBDIR="$PROJ_DIR/lib"
  test -f $PROJ_DIR/lib64/libproj.so -o -f $PROJ_DIR/lib64/libproj.sl && PROJ_LIBDIR="$PROJ_DIR/lib64"
  test -f $PROJ_DIR/libproj.so -o -f $PROJ_DIR/libproj.sl -o -f $PROJ_DIR/libproj.dylib && PROJ_LIBDIR="$PROJ_DIR"

  if test -n "$PROJ_INCLUDE" -a -n "$PROJ_LIBDIR" ; then
      PROJ_INC=-I$PROJ_INCLUDE
      PROJ_LIBS="-L$PROJ_LIBDIR -lproj"
      AC_ADD_RUNPATH("$PROJ_LIBDIR")
  else
      AC_MSG_ERROR("Could not find proj_api.h or libproj.a/libproj.so in $PROJ_DIR.")
  fi

  AC_CHECK_LIB(proj,pj_transform,PJ_TRANSFORM=yes,PJ_TRANSFORM=no,$PROJ_LIBS)
  if test "$PJ_TRANSFORM" = "no"; then
      AC_MSG_ERROR([This version of PROJ is too old.  PROJ4.4.2 or later is required.])
  fi

  PROJ_ENABLED=-DUSE_PROJ
  AC_MSG_RESULT([        using PROJ.4 from $PROJ_DIR.])

else

  AC_MSG_RESULT([        PROJ.4 support not requested.])
fi

AC_SUBST(PROJ_ENABLED,$PROJ_ENABLED)
AC_SUBST(PROJ_INC,    $PROJ_INC)
AC_SUBST(PROJ_LIBS,   $PROJ_LIBS)

ALL_ENABLED="$PROJ_ENABLED $ALL_ENABLED"
ALL_INC="$PROJ_INC $ALL_INC"
ALL_LIB="$PROJ_LIBS $ALL_LIB"

dnl ---------------------------------------------------------------------
dnl ENABLE THREADS ONLY IF REQUESTED.
dnl ---------------------------------------------------------------------

THREAD_LIB=""
THREAD_FLAG=""

AC_CHECKING(whether we should include thread safe support)
AC_ARG_WITH(threads,
[  --with-threads[=linkopt]Include thread safe support],,)

if test "$with_threads" = "yes" ; then

  AC_CHECK_LIB(pthread,pthread_create,THREAD_FLAG=-DUSE_THREAD,,,)

  if test -n "$THREAD_FLAG" ; then
      THREAD_LIB="-lpthread"
      AC_MSG_RESULT([        using -lpthread from system libs.])
  else
      AC_MSG_ERROR([        -lpthread not found, use --with-thread=<pthread link options>])
  fi

elif test -n "$with_threads" -a "$with_threads" != "no" ; then

  THREAD_FLAG=-DUSE_THREAD
  THREAD_LIB="$with_threads"  

  AC_MSG_RESULT([        using threads with link options "$THREAD_LIB".])

else
  AC_MSG_RESULT([        thread safe support disabled.])
fi

AC_SUBST(THREAD_FLAG,$THREAD_FLAG)
AC_SUBST(THREAD_LIB,$THREAD_LIB)

ALL_ENABLED="$THREAD_FLAG $ALL_ENABLED"
ALL_LIB="$THREAD_LIB $ALL_LIB"

dnl ---------------------------------------------------------------------
dnl Find ESRI SDE ONLY IF REQUESTED.
dnl ---------------------------------------------------------------------

AC_CHECKING(whether we should include ESRI SDE support)
AC_ARG_WITH(sde,
[  --with-sde[=DIR]        Include ESRI SDE support (DIR is SDE's install dir).],,)

AC_ARG_WITH(sde-version,
[  --with-sde-version[=VERSION NUMBER]  Set ESRI SDE version number (Default is 80).],SDE_VERSION=$with_sde_version,SDE_VERSION=80)

if test "$with_sde" = "yes" ; then

  AC_CHECK_LIB(sde$SDE_VERSION,SE_connection_create,SDE_ENABLED=-DUSE_SDE,,,)

  if test -n "$SDE_ENABLED" ; then
      SDE_LIB="-lsde$SDE_VERSION -lpe$SDE_VERSION -lsg$SDE_VERSION"
      AC_CHECK_LIB(pthread, main, SDE_LIB="$SDE_LIB -lpthread",,)
      AC_CHECK_LIB(socket, main, SDE_LIB="$SDE_LIB -lsocket",,)
      AC_CHECK_LIB(dl, main, SDE_LIB="$SDE_LIB -ldl",,)
      AC_MSG_RESULT([        using ESRI SDE from system libs.])
  else
      AC_MSG_WARN([        ESRI SDE not found in system libs... use --with-sde=DIR.])
  fi

elif test -n "$with_sde" -a "$with_sde" != "no" ; then

  AC_EXPAND_PATH($with_sde, SDE_DIR)

  test -f $SDE_DIR/arcsde/coverages/include/sdetype.h && SDE_INCDIR="$SDE_DIR/arcsde/coverages/include"
  test -f $SDE_DIR/include/sdetype.h && SDE_INCDIR="$SDE_DIR/include"
  test -f $SDE_DIR/sdetype.h && SDE_INCDIR="$SDE_DIR"

  test -f $SDE_DIR/lib/libsde$SDE_VERSION.a && SDE_LIBDIR="$SDE_DIR/lib"
  test -f $SDE_DIR/lib64/libsde$SDE_VERSION.a && SDE_LIBDIR="$SDE_DIR/lib64"
  test -f $SDE_DIR/libsde$SDE_VERSION.a && SDE_LIBDIR="$SDE_DIR"

  test -f $SDE_DIR/lib/libsde$SDE_VERSION.so -o -f $SDE_DIR/lib/libsde$SDE_VERSION.sl && SDE_LIBDIR="$SDE_DIR/lib"
  test -f $SDE_DIR/lib64/libsde$SDE_VERSION.so -o -f $SDE_DIR/lib64/libsde$SDE_VERSION.sl && SDE_LIBDIR="$SDE_DIR/lib64"
  test -f $SDE_DIR/libsde$SDE_VERSION.so -o -f $SDE_DIR/libsde$SDE_VERSION.sl && SDE_LIBDIR="$SDE_DIR"

  if test -n "$SDE_INCDIR" -a -n "$SDE_LIBDIR" ; then
      SDE_INC=-I$SDE_INCDIR
      SDE_LIB="-L$SDE_LIBDIR -lsde$SDE_VERSION -lpe$SDE_VERSION -lsg$SDE_VERSION"
      AC_CHECK_LIB(pthread, main, SDE_LIB="$SDE_LIB -lpthread",,)
      AC_CHECK_LIB(socket, main, SDE_LIB="$SDE_LIB -lsocket",,)
      AC_CHECK_LIB(dl, main, SDE_LIB="$SDE_LIB -ldl",,)
      AC_ADD_RUNPATH("$SDE_LIBDIR")
  else
      AC_MSG_ERROR("Could not find sdetype.h or libsde$SDE_VERSION.a/libsde$SDE_VERSION.so in $SDE_DIR.")
  fi

  SDE_ENABLED=-DUSE_SDE
  AC_MSG_RESULT([        using ESRI SDE from $SDE_DIR.])

else

  AC_MSG_RESULT([        ESRI SDE support not requested.])
fi

AC_SUBST(SDE_ENABLED,$SDE_ENABLED)
AC_SUBST(SDE_INC,    $SDE_INC)
AC_SUBST(SDE_LIB,    $SDE_LIB)

ALL_ENABLED="$SDE_ENABLED $ALL_ENABLED"
ALL_INC="$SDE_INC $ALL_INC"
ALL_LIB="$SDE_LIB $ALL_LIB"

dnl ---------------------------------------------------------------------
dnl Find GEOS ONLY IF REQUESTED.
dnl ---------------------------------------------------------------------

AC_MSG_CHECKING(if GEOS support requested)

AC_ARG_WITH(geos,
[  --with-geos[=PATH]      Include GEOS support (PATH is path to geos-config)],,)

if test "$with_geos" = "yes" ; then

  AC_MSG_RESULT([yes])

  if test "`basename xx/$with_geos`" = "geos-config" ; then
    GEOS_CONFIG="$with_geos"
  fi

  if test -z "$GEOS_CONFIG" ; then
    AC_PATH_PROG(GEOS_CONFIG, geos-config, no)
  fi

  if test "$GEOS_CONFIG" = "no" ; then
    AC_MSG_ERROR([couldn't find geos-config])
  fi

  GEOS_ENABLED=-DUSE_GEOS

  GEOS_INCDIR=`$GEOS_CONFIG --includes`
  GEOS_INC="-I$GEOS_INCDIR"
  GEOS_LIB="`$GEOS_CONFIG --libs`_c"

  AC_SUBST(GEOS_ENABLED,$GEOS_ENABLED)
  AC_SUBST(GEOS_INC,    $GEOS_INC)
  AC_SUBST(GEOS_LIB,    $GEOS_LIB)

  ALL_ENABLED="$GEOS_ENABLED $ALL_ENABLED"
  ALL_INC="$GEOS_INC $ALL_INC"
  ALL_LIB="$GEOS_LIB $ALL_LIB"

elif test -n "$with_geos" -a "$with_geos" != "no" ; then

  GEOS_CONFIG=$with_geos

  if test -f "$GEOS_CONFIG" -a -x "$GEOS_CONFIG" ; then
    AC_MSG_RESULT([yes, user supplied geos-config ($GEOS_CONFIG)])
  else
    AC_MSG_ERROR(['$GEOS_CONFIG' is not an executable.  Make sure you use --with-geos=/path/to/geos-config])
  fi

  GEOS_ENABLED=-DUSE_GEOS

  GEOS_INCDIR=`$GEOS_CONFIG --includes`
  GEOS_INC="-I$GEOS_INCDIR"
  GEOS_LIB="`$GEOS_CONFIG --libs`_c"

  AC_SUBST(GEOS_ENABLED,$GEOS_ENABLED)
  AC_SUBST(GEOS_INC,    $GEOS_INC)
  AC_SUBST(GEOS_LIB,    $GEOS_LIB)

  ALL_ENABLED="$GEOS_ENABLED $ALL_ENABLED"
  ALL_INC="$GEOS_INC $ALL_INC"
  ALL_LIB="$GEOS_LIB $ALL_LIB"

else

  AC_MSG_RESULT([no])

fi

dnl ---------------------------------------------------------------------
dnl Find OGR ONLY IF REQUESTED.
dnl ---------------------------------------------------------------------

AC_CHECKING(whether we should include OGR support)
AC_ARG_WITH(ogr,
[  --with-ogr[=PATH]       Include OGR support (PATH is path to gdal-config).],,)

if test "`basename xx/$with_ogr`" = "gdal-config" ; then
  GDAL_CONFIG="$with_ogr"
fi

if test "$with_ogr" = "yes" ; then
 AC_PATH_PROG(GDAL_CONFIG, gdal-config, no)

  if test "$GDAL_CONFIG" = "no" ; then
    AC_MSG_ERROR([couldn't find gdal-config])
  fi
fi

if test -n "$GDAL_CONFIG" -a "$with_ogr" != "no" ; then

  dnl Is OGR enabled in this gdal build?

  if test x`$GDAL_CONFIG --ogr-enabled` = "xyes" ; then
      OGR_ENABLED=-DUSE_OGR
      AC_MSG_RESULT([        OGR enabled ($OGR_ENABLED).])
  else
      AC_MSG_WARN([        OGR not available.])
  fi

else

  AC_MSG_RESULT([        OGR support not requested.])
fi

dnl
dnl OGR Makefile substitutions are done together with GDAL stuff below.
dnl

dnl ---------------------------------------------------------------------
dnl Find GDAL only if requested (and not supplied by OGR)
dnl ---------------------------------------------------------------------
AC_MSG_CHECKING(if GDAL support requested)

AC_ARG_WITH(gdal,
[  --with-gdal[=PATH]      Include GDAL support (PATH is path to gdal-config)],,)

if test "$with_gdal" = "yes" ; then

  AC_MSG_RESULT([yes])

  if test "`basename xx/$with_gdal`" = "gdal-config" ; then
    GDAL_CONFIG="$with_gdal"
  fi

  if test -z "$GDAL_CONFIG" ; then
    AC_PATH_PROG(GDAL_CONFIG, gdal-config, no)
  fi

  if test "$GDAL_CONFIG" = "no" ; then
    AC_MSG_ERROR([couldn't find gdal-config])
  fi

  GDAL_ENABLED=-DUSE_GDAL

elif test -n "$with_gdal" -a "$with_gdal" != "no" ; then

  GDAL_CONFIG=$with_gdal

  if test -f "$GDAL_CONFIG" -a -x "$GDAL_CONFIG" ; then
    AC_MSG_RESULT([yes, user supplied gdal-config ($GDAL_CONFIG)])
  else
    AC_MSG_ERROR(['$GDAL_CONFIG' is not an executable.  Make sure you use --with-gdal=/path/to/gdal-config])
  fi

  GDAL_ENABLED=-DUSE_GDAL

else

  AC_MSG_RESULT([no])

fi

dnl
dnl GDAL_LIB and GDAL_INC are used for both GDAL and OGR
dnl

if test -n "$GDAL_ENABLED" -o "$OGR_ENABLED"; then

  GDAL_LIB=`$GDAL_CONFIG --libs`
  GDAL_INC=`$GDAL_CONFIG --cflags`

fi

AC_SUBST(GDAL_ENABLED,$GDAL_ENABLED)
AC_SUBST(OGR_ENABLED,$OGR_ENABLED)
AC_SUBST(GDAL_INC,    $GDAL_INC)
AC_SUBST(GDAL_LIB,    $GDAL_LIB)

ALL_ENABLED="$GDAL_ENABLED $OGR_ENABLED $ALL_ENABLED"
ALL_INC="$GDAL_INC $ALL_INC"
ALL_LIB="$GDAL_LIB $ALL_LIB"

dnl ---------------------------------------------------------------------------
dnl Determine whether we should enable JPEG output - not if we have GDAL.
dnl ---------------------------------------------------------------------------

if test -n "$JPEG_ENABLED" ; then
  AC_MSG_CHECKING(checking ms JPEG output)
  if test -n "$GDAL_ENABLED" ; then 
    JPEG_ENABLED=
    AC_MSG_RESULT([no we, have GDAL available.])
  else
    ALL_ENABLED="$JPEG_ENABLED $ALL_ENABLED"
    ALL_INC="$JPEG_INC $ALL_INC"
    ALL_LIB="$JPEG_LIB $ALL_LIB"
    AC_MSG_RESULT([yes, GDAL is not available but libjpeg is.])
  fi
fi

dnl ---------------------------------------------------------------------
dnl libTIFF (included by default if found on system).
dnl ---------------------------------------------------------------------

dnl Clear some cache variables
unset ac_cv_lib_tiff_TIFFOpen

AC_CHECKING(whether we should include TIFF support)
AC_ARG_WITH(tiff,
[  --without-tiff        Disable TIFF support.
  --with-tiff[=DIR]       Include TIFF support (DIR is LibTIFF's install dir).],,)
if test -z "$with_tiff" -a -n "$GDAL_ENABLED" ; then
  AC_MSG_RESULT([        disabled since gdal is in use])

elif test "$with_tiff" = "no" ; then
  AC_MSG_RESULT([        disabled by --without-tiff])

elif test -n "$with_tiff" -a "$with_tiff" != "yes" ; then

  AC_EXPAND_PATH($with_tiff, TIFF_DIR)
  test -f $TIFF_DIR/include/tiffio.h && TIFF_INCLUDE="$TIFF_DIR/include"
  test -f $TIFF_DIR/libtiff/tiffio.h && TIFF_INCLUDE="$TIFF_DIR/libtiff"
  test -f $TIFF_DIR/include/libtiff/tiffio.h && TIFF_INCLUDE="$TIFF_DIR/include/libtiff"
  test -f $TIFF_DIR/tiffio.h && TIFF_INCLUDE="$TIFF_DIR"

  test -f $TIFF_DIR/lib/libtiff.a && TIFF_LIBDIR="$TIFF_DIR/lib"
  test -f $TIFF_DIR/lib64/libtiff.a && TIFF_LIBDIR="$TIFF_DIR/lib64"
  test -f $TIFF_DIR/libtiff/libtiff.a && TIFF_LIBDIR="$TIFF_DIR/libtiff"
  test -f $TIFF_DIR/libtiff.a && TIFF_LIBDIR="$TIFF_DIR"

  test -f $TIFF_DIR/lib/libtiff.so -o $TIFF_DIR/lib/libtiff.sl -o -f $TIFF_DIR/lib/libtiff.dylib && TIFF_LIBDIR="$TIFF_DIR/lib"
  test -f $TIFF_DIR/lib64/libtiff.so -o $TIFF_DIR/lib64/libtiff.sl && TIFF_LIBDIR="$TIFF_DIR/lib64"
  test -f $TIFF_DIR/libtiff/libtiff.so -o -f $TIFF_DIR/libtiff/libtiff.sl -o -f $TIFF_DIR/libtiff/libtiff.dylib && TIFF_LIBDIR="$TIFF_DIR/libtiff"
  test -f $TIFF_DIR/libtiff.so -o -f $TIFF_DIR/libtiff.sl -o -f $TIFF_DIR/libtiff.dylib && TIFF_LIBDIR="$TIFF_DIR"

  if test -n "$TIFF_INCLUDE" -a -n "$TIFF_LIBDIR" ; then

      TIFF_LIB="-L$TIFF_LIBDIR -ltiff"
      TIFF_INC=-I$TIFF_INCLUDE
      AC_ADD_RUNPATH("$TIFF_LIBDIR")
  else
      AC_MSG_ERROR("Could not find tiffio.h or libtiff.a/libtiff.so in $TIFF_DIR.")
  fi

  TIFF_ENABLED="-DUSE_TIFF"
  AC_MSG_RESULT([        using libtiff from $TIFF_LIB.])

else

  AC_CHECK_LIB(tiff,TIFFOpen,TIFF_LIB="-ltiff",,$GD_XTRA_LIBS)
  if test -n "$TIFF_LIB" ; then
      TIFF_ENABLED=-DUSE_TIFF
      AC_MSG_RESULT([        using libtiff from system libs.])
  else
      AC_MSG_RESULT([        libtiff not found... TIFF support not included.])
  fi

fi

dnl
dnl libtiff may need the same libs (libz, libjpeg) that are included with GD
dnl
TIFF_LIB="$TIFF_LIB $GD_XTRA_LIBS"

AC_SUBST(TIFF_ENABLED,$TIFF_ENABLED)
AC_SUBST(TIFF_INC,    $TIFF_INC)
AC_SUBST(TIFF_LIB,    $TIFF_LIB)

ALL_ENABLED="$TIFF_ENABLED $ALL_ENABLED"
ALL_INC="$TIFF_INC $ALL_INC"
ALL_LIB="$TIFF_LIB $ALL_LIB"

dnl ---------------------------------------------------------------------------
dnl Try to find PostgreSQL if PostGIS support requested.
dnl ---------------------------------------------------------------------------

AC_MSG_CHECKING(if PostGIS support requested)

AC_ARG_WITH(postgis,[  --with-postgis[=ARG]    Include PostGIS Support (ARG=yes/path to pg_config)],,)

if test "$with_postgis" = "no" -o "$with_postgis" = "" ; then
  AC_MSG_RESULT(no)
  PG_CONFIG="no"
elif test "$with_postgis" = "yes" ; then
  AC_MSG_RESULT(yes)
  AC_PATH_PROG(PG_CONFIG, pg_config, no)
else
  PG_CONFIG=$with_postgis
fi

if test "$PG_CONFIG" = "no" ; then
  POSTGIS_ENABLED=
  POSTGIS_INC=
  POSTGIS_LIB=
else
  AC_MSG_RESULT([yes, user supplied pg_config])
  POSTGIS_ENABLED=-DUSE_POSTGIS
  POSTGIS_LIB="-L`$PG_CONFIG --libdir` -lpq"
  POSTGIS_INC="-I`$PG_CONFIG --includedir`"
fi

AC_SUBST(POSTGIS_ENABLED,$POSTGIS_ENABLED)
AC_SUBST(POSTGIS_INC,$POSTGIS_INC)
AC_SUBST(POSTGIS_LIB,$POSTGIS_LIB)

ALL_ENABLED="$POSTGIS_ENABLED $ALL_ENABLED"
ALL_INC="$POSTGIS_INC $ALL_INC"
ALL_LIB="$POSTGIS_LIB $ALL_LIB"

dnl ---------------------------------------------------------------------------
dnl Try to find MySQL if MYGIS support requested.
dnl ---------------------------------------------------------------------------

AC_MSG_CHECKING(if MyGIS support requested)

AC_ARG_WITH(mygis,[  --with-mygis[=ARG]      Include MyGIS Support (ARG=yes/path to my_config)],,)

if test "$with_mygis" = "no" -o "$with_mygis" = "" ; then
  AC_MSG_RESULT(no)
  MY_CONFIG="no"
elif test "$with_mygis" = "yes" ; then
  AC_MSG_RESULT(yes)
  AC_PATH_PROG(MY_CONFIG, mysql_config, no)
else
  MY_CONFIG=$with_mygis
fi

if test "$MY_CONFIG" = "no" ; then
  MYGIS_ENABLED=
  MYGIS_INC=
  MYGIS_LIB=
else
  AC_MSG_RESULT([yes, user supplied my_config])
#  AC_MSG_RESULT([yes, user supplied my_config])
  MYGIS_ENABLED=-DUSE_MYGIS
  MYGIS_LIB="`$MY_CONFIG --libs` -lmysqlclient"
#  MYGIS_LIB="-L/usr/lib/mysql -lmysqlclient"
  MYGIS_INC="`$MY_CONFIG --cflags`"
#  MYGIS_INC="-I/usr/include/mysql"
fi

AC_SUBST(MYGIS_ENABLED,$MYGIS_ENABLED)
AC_SUBST(MYGIS_INC,$MYGIS_INC)
AC_SUBST(MYGIS_LIB,$MYGIS_LIB)

ALL_ENABLED="$MYGIS_ENABLED $ALL_ENABLED"
ALL_INC="$MYGIS_INC $ALL_INC"
ALL_LIB="$MYGIS_LIB $ALL_LIB"

dnl ---------------------------------------------------------------------------
dnl Try to find out if OracleSpatial support requested.
dnl ---------------------------------------------------------------------------

AC_MSG_CHECKING(if OracleSpatial support requested)
AC_ARG_WITH(oraclespatial,
[  --with-oraclespatial[=ARG] Include OracleSpatial Support (ARG=yes/path to Oracle home)],,)

if test "$with_oraclespatial" = "no" -o "$with_oraclespatial" = ""; then
  AC_MSG_RESULT([no])
  ORA_HOME="no"
elif test "$with_oraclespatial" = "yes"; then
  if test -d "$ORACLE_HOME"; then
    AC_MSG_RESULT([yes, set from ORACLE_HOME environment variable])
    ORA_HOME="$ORACLE_HOME"
  else
    ORA_HOME="no"
    AC_MSG_ERROR([ORACLE_HOME variable is not properly set])
  fi
else
  AC_MSG_RESULT([yes, user supplied ORACLE_HOME])
  ORA_HOME=$with_oraclespatial
fi

if test "$ORA_HOME" = "no" ; then
  ORACLESPATIAL_ENABLED=
  ORACLESPATIAL_INC=
  ORACLESPATIAL_LIB=
else

  dnl Try to locate lib directory
  test -f $ORA_HOME/lib/libclntsh.so && ORA_LIBDIR="$ORA_HOME/lib"
  test -f $ORA_HOME/libclntsh.so &&     ORA_LIBDIR="$ORA_HOME"
  test -f $ORA_HOME/lib/oracle/10*/client/lib/libclntsh.so && ORA_LIBDIR=`dirname $ORA_HOME/lib/oracle/10*/client/lib/libclntsh.so`

  if test -z "$ORA_LIBDIR" ; then
    AC_MSG_ERROR([Oracle Spatial: libclntsh.so not found.])
  fi

  dnl Try to locate include directory
  test -f $ORA_HOME/sdk/include/oci.h && ORA_INCDIR="$ORA_HOME/sdk/include"
  test -f $ORA_HOME/rdbms/public/oci.h && ORA_INCDIR="$ORA_HOME/rdbms/public"
  test -f $ORA_HOME/rdbms/demo/oci.h && ORA_INCDIR="$ORA_HOME/rdbms/public"
  test -f $ORA_HOME/include/oracle/10*/client/oci.h && ORA_INCDIR=`dirname $ORA_HOME/include/oracle/10*/client/oci.h`

  if test -z "$ORA_INCDIR" ; then
    AC_MSG_ERROR([Oracle Spatial: oci.h not found.])
  fi

  ORACLESPATIAL_ENABLED=-DUSE_ORACLESPATIAL
  ORACLESPATIAL_LIB="-L$ORA_LIBDIR -lclntsh"
  ORACLESPATIAL_INC="-I$ORA_INCDIR -I$ORA_HOME/rdbms/demo -I$ORA_HOME/rdbms/public"

  AC_MSG_RESULT([        Oracle Spatial found in $ORA_HOME.])

fi

AC_SUBST(ORACLESPATIAL_ENABLED,$ORACLESPATIAL_ENABLED)
AC_SUBST(ORACLESPATIAL_INC,$ORACLESPATIAL_INC)
AC_SUBST(ORACLESPATIAL_LIB,$ORACLESPATIAL_LIB)

ALL_ENABLED="$ORACLESPATIAL_ENABLED $ALL_ENABLED"
ALL_INC="$ORACLESPATIAL_INC $ALL_INC"
ALL_LIB="$ORACLESPATIAL_LIB $ALL_LIB"

dnl ---------------------------------------------------------------------------
dnl Try to find out if MING FLASH support requested.
dnl ---------------------------------------------------------------------------

AC_MSG_CHECKING(if MING/Flash support requested)
AC_ARG_WITH(ming,
[  --with-ming[=DIR]       Include MING/Flash Support (DIR=path to Ming
                          directory)],,)

if test "$with_ming" = "no" -o "$with_ming" = ""; then
  AC_MSG_RESULT([no])
  MING_ENABLED=
  MING_LIB=
  MING_INC=
elif test "$with_ming" = "yes"; then
  AC_MSG_RESULT([yes])
  MING_ENABLED=-DUSE_MING_FLASH
  MING_LIB="-lming"
  MING_INC=
else
  AC_MSG_RESULT([yes, user supplied MING directory])
  MING_ENABLED=-DUSE_MING_FLASH
  MING_LIB="-L$with_ming/lib -lming"
  MING_INC="-I$with_ming/include"
fi

AC_SUBST(MING_ENABLED,$MING_ENABLED)
AC_SUBST(MING_INC,$MING_INC)
AC_SUBST(MING_LIB,$MING_LIB)

ALL_ENABLED="$MING_ENABLED $ALL_ENABLED"
ALL_INC="$MING_INC $ALL_INC"
ALL_LIB="$MING_LIB $ALL_LIB"

dnl ---------------------------------------------------------------------
dnl WMS Server Compatibility - Enabled by default if PROJ.4 is there.
dnl Can be explicitly disabled using --without-wms
dnl ---------------------------------------------------------------------

AC_CHECKING(whether we should include WMS Server support)
AC_ARG_WITH(wms,
[  --without-wms           Disable OGC WMS Compatibility (enabled by default).],,)

if test "$with_wms" = "no" ; then

  AC_MSG_RESULT([        OGC WMS Compatibility disabled by --without-wms.])

elif test -z "$PROJ_ENABLED" ; then

  AC_MSG_RESULT([        OGC WMS Compatibility not enabled (PROJ.4 is required for WMS).])

else

  WMS_SVR_ENABLED="-DUSE_WMS_SVR"
  AC_MSG_RESULT([        OGC WMS compatibility enabled ($WMS_SVR_ENABLED).])

fi

AC_SUBST(WMS_SVR_ENABLED,$WMS_SVR_ENABLED)

ALL_ENABLED="$WMS_SVR_ENABLED $ALL_ENABLED"


dnl ---------------------------------------------------------------------
dnl WFS Server support - Requires OGR and PROJ
dnl ---------------------------------------------------------------------

AC_CHECKING(whether we should include WFS Server support)
AC_ARG_WITH(wfs,
[  --with-wfs              Enable OGC WFS Server Support (OGR+PROJ4 required).],,)

if test "$with_wfs" != "yes" ; then

  AC_MSG_RESULT([        OGC WFS Server support not requested.])

elif test -z "$OGR_ENABLED" ; then

  AC_MSG_ERROR([OGC WFS Server support cannot be enabled: missing OGR which is required).])

elif test -z "$PROJ_ENABLED" ; then

  AC_MSG_ERROR([OGC WFS Server support cannot be enabled: missing PROJ4 which is required).])

elif test "$with_wfs" = "yes" ; then

  WFS_SVR_ENABLED="-DUSE_WFS_SVR"
  AC_MSG_RESULT([        OGC WFS Server support enabled ($WFS_SVR_ENABLED).])

fi

AC_SUBST(WFS_SVR_ENABLED,$WFS_SVR_ENABLED)

ALL_ENABLED="$WFS_SVR_ENABLED $ALL_ENABLED"


dnl ---------------------------------------------------------------------
dnl WCS Server support - Requires GDAL and PROJ
dnl ---------------------------------------------------------------------

AC_CHECKING(whether we should include WCS Server support)
AC_ARG_WITH(wcs,[  --with-wcs              Enable OGC WCS Server Support (GDAL+PROJ4 required).],,)

if test "$with_wcs" != "yes" ; then
  AC_MSG_RESULT([        OGC WCS Server support not requested.])  
elif test -z "$GDAL_ENABLED" ; then
  AC_MSG_ERROR([OGC WCS Server support cannot be enabled: missing GDAL which is required).])
elif test -z "$PROJ_ENABLED" ; then
  AC_MSG_ERROR([OGC WCS Server support cannot be enabled: missing PROJ4 which is required).])
elif test "$with_wcs" = "yes" ; then
  WCS_SVR_ENABLED="-DUSE_WCS_SVR"
  AC_MSG_RESULT([        OGC WCS Server support enabled ($WCS_SVR_ENABLED).])
fi

AC_SUBST(WCS_SVR_ENABLED,$WCS_SVR_ENABLED)
ALL_ENABLED="$WCS_SVR_ENABLED $ALL_ENABLED"


dnl ---------------------------------------------------------------------
dnl WMS and WFS Client support - Requires PROJ.4 and libcurl.
dnl OGR recommended for WMS and required for WFS
dnl ---------------------------------------------------------------------

AC_CHECKING(whether we should include WMS Client Connections support)
AC_ARG_WITH(wmsclient,
[  --with-wmsclient        Enable OGC WMS Client Connections (PROJ4 and libcurl
                          required).],,)

if test "$with_wmsclient" = "no" ; then

  AC_MSG_RESULT([        OGC WMS Client Layers disabled by --without-wmsclient.])

elif test -z "$PROJ_ENABLED" ; then

  AC_MSG_RESULT([        OGC WMS Client Connections not enabled (PROJ.4 and libcurl required).])

elif test "$with_wmsclient" = "yes" ; then

  dnl we'll test for libcurl below.
  WMS_REQUESTED="yes"

fi


AC_CHECKING(whether we should include WFS Client Connections support)
AC_ARG_WITH(wfsclient,
[  --with-wfsclient        Enable OGC WFS Client Connections (PROJ4, libcurl and
                          OGR required).],,)

if test "$with_wfsclient" = "no" ; then

  AC_MSG_RESULT([        OGC WFS Client Layers disabled by --without-wfsclient.])

elif test -z "$PROJ_ENABLED" -o -z "$OGR_ENABLED"; then

  AC_MSG_RESULT([        OGC WFS Client Connections not enabled (PROJ.4, libcurl and OGR required).])

elif test "$with_wfsclient" = "yes" ; then

  dnl we'll test for libcurl below.
  WFS_REQUESTED="yes"

fi

dnl ---------------------------------------------------------------------
dnl OGC SOS Server support - Requires libxml2 and PROJ4
dnl ---------------------------------------------------------------------

AC_CHECKING(whether we should include OGC SOS Server support)
AC_ARG_WITH(sos,
[  --with-sos              Enable OGC SOS Server Support (libxml2 required).],,)

if test "$with_sos" = "no" ; then

  AC_MSG_RESULT([        OGC SOS Server support disabled by --without-sos.])

elif test -z "$PROJ_ENABLED" ; then

  AC_MSG_RESULT([        OGC SOS Support not enabled (PROJ.4 and libxml2 required).])

elif test "$with_sos" = "yes" ; then

  dnl we'll test for libxml2 below.
  SOS_REQUESTED="yes"

fi

dnl ---------------------------------------------------------------------
dnl Look for libcurl if WMS and WFS Client requested
dnl ---------------------------------------------------------------------

AC_CHECKING(for curl-config)
AC_ARG_WITH(curl-config,
[  --with-curl-config=PATH Specify path to curl-config.],,)

dnl Clear some cache variables
unset ac_cv_path_LIBCURL_CONFIG

if test "$WMS_REQUESTED" = "yes" -o "$WFS_REQUESTED" = "yes" ; then

  if test "`basename xx/$with_curl_config`" = "curl-config" ; then
    LIBCURL_CONFIG="$with_curl_config"
  else
    AC_PATH_PROG(LIBCURL_CONFIG, curl-config, no)
  fi

  if test "$LIBCURL_CONFIG" = "no" ; then
    AC_MSG_ERROR([couldn't find curl-config, try using --with-curl-config=PATH])
  fi

  CURL_VERNUM=`$LIBCURL_CONFIG --vernum`
  CURL_VER=`$LIBCURL_CONFIG --version | awk '{print $2}'`

  AC_MSG_RESULT([        found libcurl version $CURL_VER])
  
  dnl Need libcurl 7.10.1 or more recent
  if test -z "$CURL_VERNUM" -o `expr "0x$CURL_VERNUM" \< "0x070a01"` = 1; then
    AC_MSG_ERROR([libcurl version 7.10.1 or more recent is required.])
  fi

  dnl set WMS_LYR_ENABLED/WFS_LYR_ENABLED here, Makefile substitutions are
  dnl done only further down, after checking for libxml2
  if test "$WMS_REQUESTED" = "yes" ; then
    WMS_LYR_ENABLED="-DUSE_WMS_LYR"
    AC_MSG_RESULT([        OGC WMS Client Connections enabled ($WMS_LYR_ENABLED).])
  fi

  if test "$WFS_REQUESTED" = "yes" ; then
    WFS_LYR_ENABLED="-DUSE_WFS_LYR"
    AC_MSG_RESULT([        OGC WFS Client Connections enabled ($WFS_LYR_ENABLED).])
  fi

  CURL_INC=`$LIBCURL_CONFIG --cflags`
  CURL_LIB=`$LIBCURL_CONFIG --libs`

fi

AC_SUBST(CURL_INC,    $CURL_INC)
AC_SUBST(CURL_LIB,    $CURL_LIB)

ALL_INC="$CURL_INC $ALL_INC"
ALL_LIB="$CURL_LIB $ALL_LIB"


dnl ---------------------------------------------------------------------
dnl Look for libxml2 if SOS Server requested
dnl ---------------------------------------------------------------------

AC_CHECKING(for xml2-config)
AC_ARG_WITH(xml2-config,
[  --with-xml2-config=PATH Specify path to xml2-config.],,)

dnl Clear some cache variables
unset ac_cv_path_LIBXML2_CONFIG

if test "$SOS_REQUESTED" = "yes" ; then

  if test "`basename xx/$with_xml2_config`" = "xml2-config" ; then
    LIBXML2_CONFIG="$with_xml2_config"
  else
    AC_PATH_PROG(LIBXML2_CONFIG, xml2-config, no)
  fi

  if test "$LIBXML2_CONFIG" = "no" ; then
    AC_MSG_ERROR([couldn't find xml2-config, try using --with-xml2-config=PATH])
  fi

  XML2_VER=`$LIBXML2_CONFIG --version`

  AC_MSG_RESULT([        found libxml2 version $XML2_VER])
  
  SOS_SVR_ENABLED="-DUSE_SOS_SVR"
  XML2_INC=`$LIBXML2_CONFIG --cflags`
  XML2_LIB=`$LIBXML2_CONFIG --libs`

  AC_MSG_RESULT([        OGC SOS Server enabled ($SOS_SVR_ENABLED).])

fi

AC_SUBST(XML2_INC,    $XML2_INC)
AC_SUBST(XML2_LIB,    $XML2_LIB)

ALL_INC="$XML2_INC $ALL_INC"
ALL_LIB="$XML2_LIB $ALL_LIB"

dnl Substitutions for vars which are set only if curl and/or libxml2 are found
AC_SUBST(WMS_LYR_ENABLED,$WMS_LYR_ENABLED)
AC_SUBST(WFS_LYR_ENABLED,$WFS_LYR_ENABLED)
AC_SUBST(SOS_SVR_ENABLED,$SOS_SVR_ENABLED)
ALL_ENABLED="$WMS_LYR_ENABLED $WFS_LYR_ENABLED $SOS_SVR_ENABLED $ALL_ENABLED"


dnl ---------------------------------------------------------------------
dnl Support for FastCGI enabled builds.
dnl ---------------------------------------------------------------------
AC_CHECKING(whether FastCGI support should be enabled)
AC_ARG_WITH(fastcgi,
[  --with-fastcgi=path     Enable FastCGI, point to installed tree.],,)

if test "$with_fastcgi" = "no" -o "$with_fastcgi" = "" ; then

  AC_MSG_RESULT([        FastCGI support not enabled.])

else 

  if test "$with_fastcgi" = "yes" ; then
    FASTCGI_INC=""
    FASTCGI_LIB="-lfcgi"
  else
    FASTCGI_INC="-I$with_fastcgi/include"
    FASTCGI_LIB="-L$with_fastcgi/lib -lfcgi"
  fi

  AC_CHECK_LIB(fcgi,FCGI_Accept,FASTCGI_ENABLED=-DUSE_FASTCGI,,,)

fi

AC_SUBST(FASTCGI_ENABLED,$FASTCGI_ENABLED)
AC_SUBST(FASTCGI_INC,    $FASTCGI_INC)
AC_SUBST(FASTCGI_LIB,    $FASTCGI_LIB)

ALL_ENABLED="$FASTCGI_ENABLED $ALL_ENABLED"
ALL_INC="$FASTCGI_INC $ALL_INC"
ALL_LIB="$FASTCGI_LIB $ALL_LIB"

dnl ---------------------------------------------------------------------
dnl Try to detect Apache version (httpd -v)
dnl ---------------------------------------------------------------------

AC_CHECKING(HTTPD server (Apache) version)
AC_ARG_WITH(httpd,
[  --with-httpd            Specify path to 'httpd' executable.],,)


if test "$with_httpd" = "no" ; then

  AC_MSG_RESULT([        HTTPD server (Apache) version detection disabled by --without-httpd.])

elif test "$with_httpd" = "yes" ; then
  AC_MSG_ERROR([full path to httpd executable is required as argument when using --with-httpd])

else

  if test -n "$with_httpd" ; then
    dnl
    dnl User supplied explicit httpd path
    dnl
    HTTPD="$with_httpd"
    
    if test -f "$HTTPD" -a -x "$HTTPD" ; then
      AC_MSG_RESULT([        using user-supplied httpd ($HTTPD)])
    else
      AC_MSG_ERROR([$HTTPD is not an executable.  Make sure you use --with-httpd=/path/to/httpd])
    fi
  else
    dnl 
    dnl Try to locate httpd
    dnl
    AC_PATH_PROG(HTTPD, httpd, "", "$PATH:/usr/sbin:/usr/bin:/usr/local/bin:/usr/local/apache/bin")
  fi

  if test -z "$HTTPD" ; then
    AC_MSG_RESULT([        'httpd' not found, skipping Apache version detection. You may want to use --with-httpd=/path/to/httpd])
  else
    HTTPD_VERSION_STRING=`$HTTPD -v | grep "Server version" | awk '{print $3}'`
    dnl If server is not Apache then APACHE_VERSION will be empty
    APACHE_VERSION=`echo "$HTTPD_VERSION_STRING" | grep Apache | awk -F / '{print $2}' | awk -F . '{print $1*1000000 + $2*1000 + $3}'`

    AC_MSG_RESULT([        $HTTPD version is $HTTPD_VERSION_STRING ($APACHE_VERSION).])
  fi

fi


dnl ---------------------------------------------------------------------
dnl Define some vars for the mapserver library itself for modules that
dnl link with it.
dnl ---------------------------------------------------------------------

MS_DIR="`pwd`"
MS_INC="-I$MS_DIR"
MS_LIB="-L$MS_DIR -lmap"
AC_ADD_RUNPATH("$MS_DIR")
AC_SUBST(MS_INC, $MS_INC)
AC_SUBST(MS_LIB, $MS_LIB)

dnl ---------------------------------------------------------------------
dnl Check IGNORE_MISSING_DATA option (OFF by default)
dnl ---------------------------------------------------------------------

AC_ARG_ENABLE(ignore-missing-data,
[  --enable-ignore-missing-data   Ignore missing data file errors at runtime
  --disable-ignore-missing-data  Report all missing data files (enabled by default).],,)
if test "$enable_ignore_missing_data" = "yes" ; then

  IGNORE_MISSING_DATA="-DIGNORE_MISSING_DATA"

  AC_MSG_RESULT([Compiling with -DIGNORE_MISSING_DATA.])
fi
AC_SUBST(IGNORE_MISSING_DATA, $IGNORE_MISSING_DATA)

dnl ---------------------------------------------------------------------
dnl Check USE_POINT_Z_M option (OFF by default)
dnl ---------------------------------------------------------------------

AC_ARG_ENABLE(point-z-m,
[  --enable-point-z-m      Use point Z and M parameters (disabled by default).
  --disable-point-z-m     Disable support for point Z and M parameters.],,)
if test "$enable_point_z_m" = "yes" ; then

  USE_POINT_Z_M="-DUSE_POINT_Z_M"

  AC_MSG_RESULT([Compiling with -DUSE_POINT_Z_M.])
fi
AC_SUBST(USE_POINT_Z_M, $USE_POINT_Z_M)
ALL_ENABLED="$USE_POINT_Z_M $ALL_ENABLED"


dnl ---------------------------------------------------------------------
dnl Force use of slow (generic) MS_NINT macro.
dnl ---------------------------------------------------------------------

AC_ARG_ENABLE(point-z-m,
[  --disable-fast-nint     Use safe MS_NINT with reliable rounding],,)

if test "$enable_fast_nint" = "no" ; then

  USE_NINT="-DUSE_GENERIC_MS_NINT"

  AC_MSG_RESULT([Compiling with safe MS_NINT])
else
  USE_NINT=""

  AC_MSG_RESULT([Compiling with fast MS_NINT])
fi
AC_SUBST(USE_NINT, $USE_NINT)
ALL_ENABLED="$USE_NINT $ALL_ENABLED"

dnl ---------------------------------------------------------------------
dnl Check for warning flags. 
dnl 
dnl By default we use -Wall with gcc.  We provide an option for very 
dnl strict gcc warnings if so desired. 
dnl ---------------------------------------------------------------------

AC_MSG_CHECKING(compiler warnings)

AC_ARG_WITH(warnings,
[  --with-warnings[=flags]   Enable strict warnings (or user defined warnings)],,)

if test "$with_warnings" = "yes" ; then 
  if test "$GCC" = "yes" ; then
    WFLAGS="-W -Wall -Wcast-align -Wmissing-prototypes -Wstrict-prototypes -Wpointer-arith -Wreturn-type"
    C_EXTRA_WFLAGS="-Wmissing-declarations"
  
    AC_MSG_RESULT([strict])
  else
    WFLAGS=""
    C_EXTRA_WFLAGS=""
    AC_MSG_RESULT([disabled])
  fi

elif test "$with_warnings" = "" ; then 
  if test "$GCC" = "yes" ; then
    WFLAGS="-Wall"
    C_EXTRA_WFLAGS=""
    AC_MSG_RESULT([basic])
  else
    WFLAGS=""
    C_EXTRA_WFLAGS=""
    AC_MSG_RESULT([disabled])
  fi

elif test "$with_warnings" = "no" ; then
  WFLAGS=""
  C_EXTRA_WFLAGS=""
  AC_MSG_RESULT([disabled])

else
  WFLAGS="$with_warnings"
  C_EXTRA_WFLAGS=""
  AC_MSG_RESULT([user defined])
fi

CFLAGS="$CFLAGS $WFLAGS $C_EXTRA_WFLAGS"
CXXFLAGS="$CXXFLAGS $WFLAGS "

dnl ---------------------------------------------------------------------
dnl Check --enable-debug option for "-g" compile flag. (OFF by default)
dnl ---------------------------------------------------------------------

AC_CHECKING(whether we should enable debug features)
AC_ARG_ENABLE(debug,
[  --enable-debug          Include "-g" in CFLAGS for debugging and enable
                          msDebug() output to stderr (i.e. server log file).
  --disable-debug         Do not include "-g" in CFLAGS and disable msDebug()
                          output (the default).],,)

if test "$enable_debug" = "yes" ; then
  dnl also enable output of msDebug() calls to stderr.
  DEBUG_FLAGS="-DENABLE_STDERR_DEBUG"

  if test -n "$APACHE_VERSION" -a `expr "$APACHE_VERSION" \>= "2000000"` = 1 ; then
    AC_MSG_RESULT([        You requested msDebug() output on a system apparently running $HTTPD_VERSION_STRING.  Setting stderr to non-blocking due to Apache 2.x bug (see MapServer bug 458 or Apache bug 22030).])
    DEBUG_FLAGS="$DEBUG_FLAGS -DNEED_NONBLOCKING_STDERR"
  fi

  AC_MSG_RESULT([        Enabling debug features: -g in CFLAGS and msDebug() to stderr ($DEBUG_FLAGS).])

else
  CXXFLAGS=`echo "$CXXFLAGS " | sed "s/-g //"`
  CFLAGS=`echo "$CFLAGS " | sed "s/-g //"`
fi

AC_SUBST(DEBUG_FLAGS, $DEBUG_FLAGS)
ALL_ENABLED="$DEBUG_FLAGS $ALL_ENABLED"


dnl ---------------------------------------------------------------------
dnl Enable source code coverage reporting for GCC
dnl ---------------------------------------------------------------------

AC_ARG_ENABLE(coverage,
[  --enable-coverage       Enable coverage testing],
[CFLAGS="$CFLAGS -fprofile-arcs -ftest-coverage"])


dnl ---------------------------------------------------------------------
dnl PHP/MapScript module options
dnl ---------------------------------------------------------------------

AC_CHECKING(for PHP/MapScript module options)
AC_ARG_WITH(php,
[  --with-php=DIR          Specify directory where PHP4's include files are
                          installed (or a pointer to the full source tree)
                          Required in order to compile the PHP/MapScript 
                          module.],,)

AC_ARG_ENABLE(internal-ld-detect,
[  --enable-perlv-ld-detect (applies to --with-php only) 
                          Use perl -V output to figure the command to use to 
                          link php_mapscript.so.  Try this only if the default
                          internal macro didn't work.],,)

if test -n "$with_php" -a -d "$with_php" ; then
  AC_EXPAND_PATH($with_php, PHP_SRC_DIR)
  dnl
  dnl Checks for shared library linking.
  dnl
  dnl Default to internal AC_LD_SHARED macro, or use the the perl-based 
  dnl if requested and perl is available... 
  dnl (The perl-V macro was the default in 3.6 and before but this was
  dnl  changed in 3.7 to use the internal AC_LD_SHARED macro by default)
  dnl

  if test "$enable_perlv_ld_detect" = "yes" ; then
      AC_MSG_RESULT(Using perl -V macro to figure ld command to link php_mapscript.so)

      AC_MSG_CHECKING([whether 'perl -V' works])
      if  (perl -V > /dev/null 2>&1)  ; then
        AC_MSG_RESULT(yes)
        AC_LD_SHARED_FROM_PERL
        PHP_CC="$PERL_CC"
        PHP_LD="$PERL_LD"
      else
        AC_MSG_RESULT(no)
        AC_MSG_ERROR([ERROR: Cannot use --enable-perlv-ld-detect since Perl -V does not work on this system.])
      fi
  else
    dnl The default: use the internal AC_LD_SHARED macro
    dnl
    AC_COMPILER_PIC
    dnl AC_LD_SHARED
    AC_PHP_LD_SHARED
    PHP_CC="$CC $C_PIC"
    PHP_LD="$PHP_LD_SHARED"
  fi

  dnl
  dnl Look for PHP3's config.h or PHP4's php_config.h.
  dnl We'll need the config file to find info about the PHP configuration
  dnl
  AC_MSG_CHECKING([for location of config.h or php_config.h])
  dnl In PHP3, it was called config.h
  test -f "$PHP_SRC_DIR/config.h" && PHP_CONFIG_H="$PHP_SRC_DIR/config.h"

  dnl In PHP 4.0.1 to 4.0.3, it was php-4.0.x/php_config.h
  test -f "$PHP_SRC_DIR/php_config.h" && PHP_CONFIG_H="$PHP_SRC_DIR/php_config.h"

  dnl Starting with PHP 4.0.4, it's php-4.0.x/main/php_config.h
  test -f "$PHP_SRC_DIR/main/php_config.h" && PHP_CONFIG_H="$PHP_SRC_DIR/main/php_config.h"

  dnl If php was installed, then the headers are under $prefix/include/php/*
  test -f "$PHP_SRC_DIR/include/php/main/php_config.h" && PHP_SRC_DIR="$PHP_SRC_DIR/include/php/" && PHP_CONFIG_H="$PHP_SRC_DIR/main/php_config.h"

  if test -n "$PHP_CONFIG_H" ; then
    AC_MSG_RESULT([$PHP_CONFIG_H])
  else
    AC_MSG_ERROR([
!!! Could not find config.h or php_config.h in $PHP_SRC_DIR.          !!!
!!! Has PHP been configured yet?                                      !!!])
  fi

  dnl
  dnl Check which PHP version we're using.
  dnl Default is PHP3, and if ZEND_API is set then we assume that we have PHP4.
  dnl
  AC_MSG_CHECKING([whether we have PHP3 or PHP4])
  if test -n "`grep 'ZEND_API' $PHP_CONFIG_H`"  ; then
    PHP_VERSION_FLAG="-DPHP4"
  else
    PHP_VERSION_FLAG="-DPHP3"
    AC_MSG_ERROR([
!!! PHP MapScript now requires PHP 4.1.2 or more recent.              !!!
!!! Support for PHP3 has been dropped after MapServer version 3.5.    !!!])
  fi
  AC_MSG_RESULT([$PHP_VERSION_FLAG])

  dnl
  dnl Check if PHP was compiled with the bundled regex, and if so then
  dnl use the same version to compile MapServer.
  dnl
  AC_MSG_CHECKING([whether we should use PHP's regex])
  if test -n "`grep 'define REGEX 1' $PHP_CONFIG_H`"  ; then
    AC_MSG_RESULT(yes)

    dnl We'll check for regex_extra.h - that might let use build
    dnl without the source using libphp_common.so
    test -f "$PHP_SRC_DIR/regex/regex_extra.h" && PHP_NO_SOURCE="1"
        if test -n "$PHP_NO_SOURCE" ; then
          dnl Found regex_extra.h
          USE_PHP_REGEX="-DUSE_PHP_REGEX"
          PHP_REGEX_OBJ=php_regex.o
          AC_MSG_RESULT([        found regex_extra.h - building PHP MapScript with PHP's bundled regex ])
        else
          AC_MSG_ERROR([
!!! PHP uses its bundled regex library but regex/regex_extra.h cannot be !!!
!!! found.                                                               !!!])
	fi

  else
    AC_MSG_RESULT(no)
  fi

  PHPMS_MAKEFILE=mapscript/php3/Makefile
  MAKE_PHPMS=php3_mapscript
  MAKE_PHPMS_CLEAN=php3_mapscript_clean

  AC_SUBST(PHP_VERSION_FLAG, $PHP_VERSION_FLAG)
  AC_SUBST(PHP_SRC_DIR, $PHP_SRC_DIR)
  AC_SUBST(PHP_CC, $PHP_CC)
  AC_SUBST(PHP_LD, $PHP_LD)
  AC_SUBST(PHP_REGEX_INC, $PHP_REGEX_INC)
  AC_SUBST(PHP_REGEX_OBJ, $PHP_REGEX_OBJ)
  AC_SUBST(USE_PHP_REGEX, $USE_PHP_REGEX)

  AC_MSG_RESULT([        PHP/MapScript module configured.])
elif test -n "$with_php" -a "$with_php" != "no" ; then
  AC_MSG_ERROR([Missing or invalid PHP source directory in --with-php=DIR.])
else
  AC_MSG_RESULT([        PHP/MapScript module not configured.])
fi

AC_SUBST(MAKE_PHPMS, $MAKE_PHPMS)
AC_SUBST(MAKE_PHPMS_CLEAN, $MAKE_PHPMS_CLEAN)

dnl ---------------------------------------------------------------------
dnl Shared library building.
dnl
dnl If we didn't select an LD_SHARED for PHP then we should do so here.
dnl ---------------------------------------------------------------------

if test "x$LD_SHARED" = "x" ; then
  AC_COMPILER_PIC
  AC_LD_SHARED
fi

dnl ---------------------------------------------------------------------
dnl 'apxs' option for PHP built as an Apache module (libphp3.so)
dnl --with-apxs[=FILE] should be the exact same value that was passed
dnl   to the PHP configure command.
dnl
dnl All we need is to add the Apache include files to the php module
dnl include path.
dnl
dnl The APXS variable also affects the way AC_ADD_RUNPATH() works
dnl ---------------------------------------------------------------------

AC_ARG_WITH(apxs,
[  --with-apxs[=FILE]      (CURRENTLY DISABLED) 
                          Use this option only if building the PHP MapScript
                          on a system where PHP was built as a shared Apache
                          module.  FILE is the optional pathname to the
                          Apache apxs tool; defaults to apxs.],,)

if test -n "$with_apxs" -a "$with_apxs" != "no" ; then

  AC_MSG_ERROR([
!!! The current version of PHP MapScript requires PHP4 configured as  !!!
!!! a CGI and cannot be used with PHP4 configured as an Apache module,!!!
!!! so you cannot use the --with-apxs option.                         !!!
!!! See: http://mapserver.gis.umn.edu/cgi-bin/wiki.pl?PHPMapScriptCGI !!!])


  AC_MSG_CHECKING([for location of Apache headers using apxs])
  if test -z "$MAKE_PHPMS" ; then
    AC_MSG_ERROR([--with-apxs applies only if --with-php is used.])

  elif test "$with_apxs" = "yes" ; then
    APXS=apxs
  else
    APXS="$with_apxs"
  fi

  APACHE_INC=-I`$APXS -q INCLUDEDIR`
  AC_MSG_RESULT($APACHE_INC)

  dnl APACHE_INC substitution is done after --with-apache switch processing
  dnl below.
fi


dnl ---------------------------------------------------------------------
dnl '--with-apache' option for PHP built as an Apache module and statically
dnl linked into the httpd executable.  This option is used to specify the
dnl location of the apache include files.
dnl ---------------------------------------------------------------------

AC_ARG_WITH(apache,
[  --with-apache=DIR       (CURRENTLY DISABLED) 
                          Use this option only if building the PHP MapScript
                          on a system where PHP was built as an Apache
                          module statically linked into the httpd executable.
                          DIR is the path to the apache include files.],,)

if test -n "$with_apache" -a "$with_apache" != "no" ; then

  AC_MSG_ERROR([
!!! The current version of PHP MapScript requires PHP4 configured as  !!!
!!! a CGI and cannot be used with PHP4 configured as an Apache module,!!!
!!! so you cannot use the --with-apache option.                       !!!
!!! See: http://mapserver.gis.umn.edu/cgi-bin/wiki.pl?PHPMapScriptCGI !!!])

  AC_MSG_CHECKING([for location of Apache headers])
  if test -z "$MAKE_PHPMS" ; then
    AC_MSG_ERROR([--with-apache applies only if --with-php is used.])
  fi

  AC_EXPAND_PATH($with_apache, APACHE_DIR)
  test -f $APACHE_DIR/httpd.h && APACHE_INC_DIR="$APACHE_DIR"
  test -f $APACHE_DIR/include/httpd.h && APACHE_INC_DIR="$APACHE_DIR/include"
  test -f $APACHE_DIR/src/include/httpd.h && APACHE_INC_DIR="$APACHE_DIR/src/include"
  if test -n "$APACHE_INC_DIR" ; then
    APACHE_INC="-I$APACHE_INC_DIR $APACHE_INC"
    AC_MSG_RESULT("-I$APACHE_INC_DIR")
  else
    AC_MSG_ERROR("Could not find httpd.h in $APACHE_DIR.")
  fi
fi

AC_SUBST(APACHE_INC, $APACHE_INC)


dnl ---------------------------------------------------------------------
dnl Check --enable-runpath to request including -Wl,-R for runtime
dnl lib path in link command (disabled by default)
dnl ---------------------------------------------------------------------

AC_MSG_CHECKING([if --enable-runpath requested])
AC_ARG_ENABLE(runpath,
[  --enable-runpath        Include runtime library path flags (-Wl,-R) in link.
                          Required on *BSD systems.
  --disable-runpath       Do not link with runtime lib path (the default).],,)

if test "$enable_runpath" != "yes" ; then
  RPATHS=""
  AC_MSG_RESULT([no])
else
  AC_MSG_RESULT([yes ($RPATHS)])
fi

dnl ---------------------------------------------------------------------
dnl Check operating system (used by Java Mapscript and possibily
dnl other mapscripts)
dnl ---------------------------------------------------------------------

AC_MSG_CHECKING([if --with-java-include-os-name specified])
AC_ARG_WITH(java-include-os-name,
[  --with-java-include-os-name=dirname
                          (AUTODETECTED)
                          Use this option only if building of Java Mapscript
                          fails because os-dependent headers are not found.
                          dirname is the name of subdirectory of the Java installation
                          where os-dependent include files are found (for instance
                          linux or solaris).
                          Only used by Java mapscript.],,)
if test -n "${with_java_include_os_name}" ; then
OS_INCLUDE_DIR="${with_java_include_os_name}"
AC_MSG_RESULT([yes ($OS_INCLUDE_DIR)])
else
dnl might break on Windows???
OUR_OSNAME=`uname -s`
case "${OUR_OSNAME}" in
[[Ll]inux]) OS_INCLUDE_DIR="linux" ;;
[[sS]olaris*]) OS_INCLUDE_DIR="solaris" ;;
[[sS]unOS*]) OS_INCLUDE_DIR="solaris" ;;
[[Ww]in*]) OS_INCLUDE_DIR="win32" ;;
*) OS_INCLUDE_DIR="linux" ;;
esac
AC_MSG_RESULT([no, autodetected $OS_INCLUDE_DIR])
fi

AC_SUBST(OS_INCLUDE_DIR, $OS_INCLUDE_DIR)

dnl ---------------------------------------------------------------------
dnl Generate output files
dnl ---------------------------------------------------------------------

AC_SUBST(ALL_ENABLED, $ALL_ENABLED)
AC_SUBST(ALL_INC, $ALL_INC)
AC_SUBST(ALL_LIB, $ALL_LIB)
AC_SUBST(ALL_STATIC_LIB, $ALL_STATIC_LIB)
AC_SUBST(RPATHS, [$RPATHS])

AC_OUTPUT(Makefile $PHPMS_MAKEFILE mapscript/java/Makefile mapscript/csharp/Makefile)

