dnl Process this file with autoconf to produce a configure script.
AC_INIT(geotiff.h)
AC_CONFIG_HEADER(geo_config.h)

VERSION_MAJOR=1
VERSION_MINOR=1
VERSION_POINT=4
AC_SUBST(VERSION_MAJOR,$VERSION_MAJOR)
AC_SUBST(VERSION_MINOR,$VERSION_MINOR)
AC_SUBST(VERSION_POINT,$VERSION_POINT)

dnl Checks for programs.
AC_PROG_CC
AC_COMPILER_WFLAGS

AC_PROG_INSTALL
AC_PROG_LN_S
AC_PROG_RANLIB
AC_COMPILER_PIC
AC_LD_SHARED

#
#	If we have shared library support define that we want to build
# 	the libgeotiff.so

GEOTIFF_SO=
if test "$LD_SHARED" != "/bin/true" ; then
  GEOTIFF_SO=libgeotiff.so.$VERSION_MAJOR.$VERSION_MINOR.$VERSION_POINT
fi
AC_SUBST(GEOTIFF_SO,$GEOTIFF_SO)


#
# We always want to check /usr/local for stuff, since things like libproj
# are often there. 
LIBS="$LIBS -L/usr/local/lib"
CFLAGS="$CFLAGS -I/usr/local/include"
CPPFLAGS="$CPPFLAGS -I/usr/local/include"

dnl Checks for libraries.
AC_CHECK_LIB(m,exp,,,)

dnl We check for headers
AC_HEADER_STDC
AC_CHECK_HEADERS(string.h strings.h stdlib.h)
 
dnl
dnl Add option to include libz in library list for use of libtiff.
dnl
AC_ARG_WITH(zip, [  --with-zip=dir          Use ZIP library in directory dir],[ZIP=$withval],[ZIP=yes])
if test "$ZIP" = "yes" ; then
  AC_CHECK_LIB(z,inflateInit_,,,-lm)
  AC_CHECK_HEADERS(zlib.h,,)
  AC_DEFINE(HAVE_ZIP)
else
  if test "$ZIP" != "no" ; then 
    LIBS="${LIBS} -L${ZIP}/lib -lz"       
    ZIP_INC="-I${ZIP}/include"            
    AC_SUBST(ZIP_INC,$ZIP_INC)
    AC_DEFINE(HAVE_ZIP)
    echo "Using user supplied libzip directory: "$ZIP
  fi
fi

dnl
dnl Add option to include libjpeg in library list for use of libtiff.
dnl
AC_ARG_WITH(zip, [  --with-jpeg=dir         Use JPEG library in directory dir],[JPEG=$withval],[JPEG=yes])
if test "$JPEG" = "yes" ; then
  AC_CHECK_LIB(jpeg,jinit_compress_master,,,-lm)
  AC_DEFINE(HAVE_JPEG)
else
  if test "$JPEG" != "no" ; then 
    LIBS="${LIBS} -L${JPEG} -ljpeg"       
    AC_DEFINE(HAVE_JPEG)
    echo "Using user supplied libjpeg directory: "$JPEG
  fi
fi

dnl
dnl Special logic to try and find a user specified libtiff tree.  If not
dnl available check if the system directories have the library, _and_ the
dnl required private include files.  Normally these private include files
dnl (tiffiop.h) are not installed with the public interface.
dnl
AC_ARG_WITH(libtiff, [  --with-libtiff=dir      Use libtiff in directory dir],,)

if test "$with_libtiff" != "" ; then
  TIFF_LIB="-L$TIFF_HOME -ltiff"
  LIBS="-L$with_libtiff -ltiff $LIBS"

  TIFF_INC=-I$with_libtiff
else
  AC_CHECK_LIB(tiff,TIFFOpen,,
               AC_MSG_ERROR([You will need to substantially rewrite libxtiff to
build libgeotiff without libtiff]),-lm)
  TIFF_INC=
  AC_CHECK_HEADERS(tiffiop.h,,TIFF_INC=-Ilibtiff_private)
  if test ! -z "$TIFF_INC" ; then
    AC_MSG_WARN([Using local copy of libtiff private include files.])
    AC_MSG_WARN([It would be safer to install libtiff private include])
    AC_MSG_WARN([files with the install-private target of libtiff, or])
    AC_MSG_WARN([use --with-libtiff when configuring libgeotiff to build])
    AC_MSG_WARN([against a libtiff build tree.])
  fi
fi

AC_SUBST(TIFF_INC,$TIFF_INC)

dnl
dnl Try and find libproj.a, and projects.h
dnl

AC_ARG_WITH(proj, [  --with-proj[=ARG]       Use PROJ.4 library (ARG=no or path)],[PROJ_HOME=$withval],[PROJ_HOME=])

if test "$PROJ_HOME" != "" -a "$PROJ_HOME" != "no" ; then
  if test -d $PROJ_HOME/src ; then
    PROJ_HOME=$PROJ_HOME/src
  fi

  dnl Old PROJ.4 build tree, without automake
  if test -f $PROJ_HOME/libproj.a -a -f $PROJ_HOME/projects.h ; then
    PROJ_LIB=$PROJ_HOME/libproj.a
    PROJ_INC=$PROJ_HOME

  dnl New PROJ.4 build tree, with automake
  elif test -f $PROJ_HOME/.libs/libproj.a -a -f $PROJ_HOME/projects.h ; then
    PROJ_LIB=$PROJ_HOME/.libs/libproj.a
    PROJ_INC=$PROJ_HOME

  dnl Installed tree in an odd place
  elif test -f $PROJ_HOME/lib/libproj.a -a -f $PROJ_HOME/include/projects.h ; then
    PROJ_LIB="-L$PROJ_HOME/lib -lproj"
    PROJ_INC=$PROJ_HOME/include 
  else
    echo "PROJ_HOME=$PROJ_HOME, but either projects.h or libproj.a not found!"
  fi
fi

if test "$PROJ_HOME" = "no" ; then
  echo "PROJ.4 support disabled."

elif test "$PROJ_LIB" != "" -a "$PROJ_INC" != "" ; then
  echo "Found $PROJ_LIB"
  echo "Found $PROJ_INC/projects.h"

  LIBS="$PROJ_LIB $LIBS"
  PROJ_INC=-I$PROJ_INC

  AC_SUBST(PROJ_INC,$PROJ_INC)
  AC_DEFINE(HAVE_PROJECTS_H)
  AC_DEFINE(HAVE_LIBPROJ)
else
  AC_CHECK_LIB(proj,pj_init,,,-lm)
  AC_CHECK_HEADERS(projects.h,,)
fi

dnl
dnl Has the user requested "incode" EPSG tables, overriding the
dnl default use of EPSG tables in csv files?
dnl

AC_ARG_ENABLE(incode-epsg, [  --enable-incode-epsg	  Use C code EPSG tables],, enable_incode_epsg=no)

if test "$enable_incode_epsg" != "yes" ; then
  echo "EPSG tables will be read from .csv files."
  CSV_SOURCE="\$(CSV_FILE_OBJS)"
else
  echo "EPSG tables will be linked into library."
  CSV_SOURCE="\$(CSV_INCODE_OBJS)"
fi
AC_SUBST(CSV_SOURCE,$CSV_SOURCE)



AC_OUTPUT(Makefile bin/Makefile)
