AC_INIT(README.postgis)
AC_CONFIG_HEADER(config.h)

dnl AC_ARG_ENABLE(autoconf, [  --enable-autoconf       really use autoconf],
dnl  	AC_MSG_RESULT(Using autoconf),
dnl  	AC_MSG_ERROR([
dnl  
dnl  Autoconf-based configuration is not widely tested.
dnl  If you want to use it anyway specify --enable-autoconf.
dnl  ])
dnl  )

dnl
dnl Compilers
dnl
AC_PROG_CC
AC_PROG_CXX
AC_PROG_LN_S


dnl
dnl iconv support (might fail in case of non-default iconv installation)
dnl
AC_SUBST(USE_ICONV)
AC_SUBST(ICONV_LDFLAGS)
ICONV_LDFLAGS=
USE_ICONV=1
AC_ARG_WITH(iconv, 
[  --without-iconv         disable unicode support],
	if test "$with_iconv" = "no"; then
		USE_ICONV=0
	fi
)

if test "$USE_ICONV" = 1; then
	AC_CHECK_LIB(iconv, iconv_open,
		USE_ICONV=1
		ICONV_LDFLAGS=-liconv
		,
		AC_CHECK_LIB(c, iconv_open,
			USE_ICONV=1
			,
			USE_ICONV=0
		)
	)
fi

AC_CHECK_HEADERS(iconv.h)



dnl
dnl host operating system
dnl
AC_CANONICAL_HOST

dnl
dnl Search for pg_config and setup pgsql stuff
dnl
AC_SUBST(PGCONFIG)
PGCONFIG=
AC_ARG_WITH(pgsql, 
[  --with-pgsql[[=ARG]]      build for a specific pgsql version
                          [[ARG=path to pg_config]]],
	if test "$with_pgsql" != "no" -a "$with_pgsql" != "yes"; then
		if ! test -x "$with_pgsql"; then
			AC_MSG_ERROR([$with_pgsql is not an executable file]);
		fi
		PGCONFIG="$with_pgsql"
	fi
)
if test -z "$PGCONFIG"; then
	AC_PATH_PROG([PGCONFIG], [pg_config])
fi
if test -z "$PGCONFIG"; then
	AC_MSG_ERROR([PostgreSQL is required by PostGIS]);
fi
AC_SUBST(PGFEINCLUDES)
AC_SUBST(PGBEINCLUDES)
AC_SUBST(PGFELIBS)
AC_SUBST(PGBELIBS)
AC_SUBST(PGBIN)
AC_SUBST(PGTEMPLATE)
AC_SUBST(LPATH)
PGFEINCLUDES="-I`${PGCONFIG} --includedir`"
PGFELIBS="-L`${PGCONFIG} --libdir` -lpq"
PGBELIBS="-L$( echo $(${PGCONFIG} --libdir)) -lpostgres"
PGBEINCLUDES="-I`${PGCONFIG} --includedir-server`"
PGBIN="$(${PGCONFIG} --bindir)"
AC_MSG_CHECKING([which template to use])
PGTEMPLATE="$(${PGCONFIG} --configure | \
             awk 'BEGIN {RS = " "; FS = "=";} \
		          { \
			        if (match($1,"--with-template")) \
					{ \
						gsub("'\''$", "", $2); \
						print $2; \
					} \
				}' \
			)"
template=$PGTEMPLATE

case $host_os in
     aix*) template=aix ;;
    beos*) template=beos ;;
    bsdi*) template=bsdi ;;
  cygwin*) template=cygwin ;;
  darwin*) template=darwin ;;
    dgux*) template=dgux ;;
 freebsd*) template=freebsd ;;
    hpux*) template=hpux ;;
    irix*) template=irix ;;
 linux*|gnu*|k*bsd*-gnu)
           template=linux ;;
   mingw*) template=win32 ;;
  netbsd*) template=netbsd ;;
nextstep*) template=nextstep ;;
 openbsd*) template=openbsd ;;
     osf*) template=osf ;;
     qnx*) template=qnx4 ;;
     sco*) template=sco ;;
 solaris*) template=solaris ;;
   sunos*) template=sunos4 ;;
 sysv4.2*)
        case $host_vendor in
          univel) template=univel ;;
        esac ;;
   sysv4*) template=svr4 ;;
   sysv5*) template=unixware ;;
  ultrix*) template=ultrix4 ;;
esac

  if test x"$template" = x"" ; then
    AC_MSG_ERROR([[
*******************************************************************
PostgreSQL has apparently not been ported to your platform yet.
To try a manual configuration, look into the src/template directory
for a similar platform and use the '--with-template=' option.

Please also contact <pgsql-ports@postgresql.org> to see about
rectifying this.  Include the above 'checking host system type...'
line.
*******************************************************************
]])
  fi

AC_MSG_RESULT([$template])

PORTNAME=$template
AC_SUBST(PORTNAME)


dnl
dnl Test for GEOS
dnl

AC_SUBST(USE_GEOS)
AC_SUBST(GEOS_DIR)
AC_SUBST(USE_GEOS_CAPI)
USE_GEOS=0
USE_GEOS_CAPI=0
GEOS_DIR=

AC_PATH_PROG([GEOSCONFIG], [geos-config])
if test -n "$GEOSCONFIG"; then
	USE_GEOS=1
fi

AC_ARG_WITH(geos, 
[  --with-geos[[=ARG]]       enable spatial predicates and operators using GEOS
                          [[ARG=path to geos-config]]],
	if test "$with_geos" = "no"; then
		USE_GEOS=0
	else
		if test "$with_geos" != "yes"; then
			if ! test -e "$with_geos"; then
	AC_MSG_ERROR([ARG of --with-geos is not an (executable) file]);
			else
				GEOSCONFIG="$with_geos"
				USE_GEOS=1
			fi
		fi
	fi
)

if test $USE_GEOS -gt 0; then
	GEOS_DIR=`$GEOSCONFIG --prefix`
	GEOS_MAJOR=`$GEOSCONFIG --version | cut -d. -f1`
	if test "$GEOS_MAJOR" = "@GEOS_VERSION@"; then
		GEOS_MAJOR=1
		GEOS_MINOR=0
	else
		GEOS_MINOR=`$GEOSCONFIG --version | cut -d. -f2`
	fi
	if test $GEOS_MAJOR -gt 2; then
		USE_GEOS_CAPI=1
	elif test $GEOS_MAJOR -eq 2 -a $GEOS_MINOR -ge 2; then
		USE_GEOS_CAPI=1
	fi
fi

dnl AC_ARG_WITH(geos-capi, 
dnl [  --with-geos-capi        enable use of GEOS C API ],
dnl 	if test "$with_geos_capi" != "no"; then
dnl 		USE_GEOS_CAPI=1
dnl 	fi
dnl )

AC_SUBST(USE_PROJ)
AC_SUBST(PROJ_DIR)
USE_PROJ=0
PROJ_DIR=
AC_PATH_PROG([PROJ], [proj])
if test -n "$PROJ"; then
	USE_PROJ=1
	PROJ_DIR=`dirname $PROJ`/..
fi
AC_ARG_WITH(proj, 
	[  --with-proj[[=DIR]]       enable reprojection support],
	if test "$with_proj" = "no"; then
		USE_PROJ=0
	else
		USE_PROJ=1
		if test "$with_proj" != "yes"; then
			PROJ_DIR=$with_proj
		fi
	fi
)
if test $USE_PROJ -gt 0; then
	if ! test -e $PROJ_DIR/include/projects.h; then
		AC_MSG_ERROR([Can't find proj dir.]);
	fi
fi

AC_SUBST(USE_JTS)
AC_SUBST(JTS_INCLUDES)
AC_SUBST(JTS_LIBDIR)
USE_JTS=0
JTS_INCLUDES=
JTS_INCLUDES=
AC_ARG_WITH(jts, 
[  --with-jts[[=ARG]]        use JTS for spatial predicates and operators
                          [[ARG=path to jts-config]]],
	if test "$with_jts" != "no"; then
		USE_JTS=1
		if test "$with_jts" != "yes"; then
			if ! test -e "$with_jts"; then
	AC_MSG_ERROR([ARG of --with-jts is not an (executable) file]);
			else
				JTSCONFIG=$with_jts
			fi
		fi
	fi
)
if test $USE_JTS -gt 0; then
	if test -z "$JTSCONFIG"; then
		AC_PATH_PROG([JTSCONFIG], [jts-config])
		if test -z "$JTSCONFIG"; then
			AC_MSG_ERROR([Can't find jts-config]);
		fi
	fi
	JTS_INCLUDES=`$JTSCONFIG --includedir`
	JTS_LIBDIR=`$JTSCONFIG --libdir`
fi

dnl
dnl Search flex and yacc
dnl
AC_SUBST(FLEX)
AC_PATH_PROG([FLEX], [flex])
if test -z "$FLEX"; then
	AC_MSG_ERROR(Can't find 'flex');
fi

AC_SUBST(DLLWRAP)
AC_PATH_PROG([DLLWRAP], [dllwrap])
AC_SUBST(DLLTOOL)
AC_PATH_PROG([DLLTOOL], [dlltool])

AC_SUBST(YACC)
AC_PATH_PROG([YACC], [yacc])
if test -z "$YACC"; then
	AC_MSG_ERROR(Can't find 'yacc');
fi

dnl
dnl Find 'install'
dnl
dnl AC_PATH_PROG([INSTALL], [install])
dnl if test -z "$INSTALL"; then
dnl 	AC_MSG_ERROR(Can't find 'install');
dnl fi
AC_SUBST(INSTALL)
INSTALL=$( echo ${PWD} )/install-sh

dnl
dnl Search perl
dnl
AC_SUBST(PERL)
AC_PATH_PROG([PERL], [perl])
if test -z "$PERL"; then
	AC_MSG_ERROR(Can't find 'perl');
fi

AC_SUBST(docdir)
docdir=''
AC_ARG_WITH(docdir,
[  --with-docdir=DIR       install the documentation in DIR [[PREFIX/doc]]],
	if test "$with_docdir" = "yes"; then
		AC_MSG_ERROR([option --with-docdir requires an argument])
	fi
	if test "$with_docdir" = "no"; then
        	docdir=""
	else
		docdir="$with_docdir"
	fi
)

AC_SUBST(datadir)
datadir=''
AC_ARG_WITH(datadir,
[  --with-datadir=DIR       install the scripts in DIR [[PREFIX/data]]],
	if test "$with_datadir" = "yes"; then
		AC_MSG_ERROR([option --with-datadir requires an argument])
	fi
	if test "$with_datadir" = "no"; then
        	datadir=""
	else
		datadir="$with_datadir"
	fi
)


dnl
dnl Find 'html/docbook.xsl'
dnl
AC_SUBST(XSLBASE)
XSLBASE=
SEARCHPATH="
	/usr/share/sgml/docbook/xsl-stylesheets 
	/usr/share/xml/docbook/stylesheet/nwalsh
	/usr/share/sgml/docbook/stylesheet/xsl/nwalsh
"
for p in ${SEARCHPATH}; do
	if test -r ${p}/html/docbook.xsl; then
		XSLBASE=${p}
		break
	fi
done

dnl
dnl Search for other binaries needed to build docs
dnl
AC_PATH_PROG([XSLTPROC], [xsltproc])
AC_PATH_PROG([PDFXMLTEX], [pdfxmltex])
AC_PATH_PROG([JW], [jw])
AC_SUBST(XSLTPROC)
AC_SUBST(PDFXMLTEX)
AC_SUBST(JW)

DOC_RULE=requirements_not_met
if test -n "${XSLBASE}"; then
	if test -n "${XSLTPROC}"; then
		DOC_RULE=html/postgis.html
	else
		if test -n "${JW}"; then
			DOC_RULE=jw
		fi
	fi
fi
AC_SUBST(DOC_RULE)


if test $USE_JTS -gt 0; then
dnl	AC_MSG_RESULT([ GEOS disabled, using JTS.])
	USE_GEOS=0
fi

if test "${prefix}" = "NONE"; then
 	prefix=/usr/local
 	for v in `${PGCONFIG} --configure`; do
 		strip=`echo $v | sed "s/'//g"`
 		var=`echo "$strip" | cut -d= -f1`
 		if test "$var" = "--prefix"; then
 			prefix=`echo "$strip" | cut -d= -f2`
 		fi
 	done
 	exec_prefix='${prefix}'
	if test "${docdir}" = ""; then
 		docdir="$(${PGCONFIG} --configure | \
             awk 'BEGIN {RS = " "; FS = "=";} \
		          { \
			        if (match($1,"--docdir")) \
					{ \
						gsub("'\''$", "", $2); \
						print $2; \
					} \
			        if (match($1,"--with-docdir")) \
					{ \
						gsub("'\''$", "", $2); \
						print $2; \
					} \
				}' \
			)"
	fi
	if test "${docdir}" = ""; then
		docdir='${prefix}/share/doc'
	elif test -n "`echo ${docdir} | grep postgresql`"; then
		docdir=`dirname ${docdir}`
	fi
	if test "${datadir}" = ""; then
 		datadir="$(${PGCONFIG} --configure | \
             awk 'BEGIN {RS = " "; FS = "=";} \
		          { \
			        if (match($1,"--datadir")) \
					{ \
						gsub("'\''$", "", $2); \
						print $2"/contrib"; \
					} \
				}' \
			)"
	fi
	if test "${datadir}" = ""; then
	 	datadir='${prefix}/share/postgresql/contrib'
	fi
	if test "${mandir}" = ""; then
 		mandir="$(${PGCONFIG} --configure | \
             awk 'BEGIN {RS = " "; FS = "=";} \
		          { \
			        if (match($1,"--mandir")) \
					{ \
						gsub("'\''$", "", $2); \
						print $2; \
					} \
				}' \
			)"
	fi
 	bindir="$(${PGCONFIG} --bindir)"
 	libdir="`${PGCONFIG} --pkglibdir`"
 	LPATH='\$$libdir'
else
 	docdir=$docdir/postgis
 	datadir=$datadir/postgis
 	libdir=$libexecdir
 	LPATH=$libexecdir
fi

#
# OS-specific settings
#
AC_SUBST(DLSUFFIX)
AC_SUBST(DLFLAGS)
AC_SUBST(EXE)
AC_SUBST(HOST_OS)

#host_os=`echo $ac_cv_host | sed 's/^\([^-]*\)-\([^-]*\)-\(.*\)$/\3/'`

HOST_OS=$host_os
DLSUFFIX=.so
DLFLAGS=-shared
EXE=
case $host_os in
	*mingw*)
		PGBEINCLUDES="$PGBEINCLUDES $PGBEINCLUDES/port/win32"
		PGFELIBS="$(echo $(${PGCONFIG} --libdir))/libpq.dll"
		DLSUFFIX=.dll
		DLFLAGS='${PGBELIBS}'
		EXE=.exe
		;;
	*darwin*)
		DLFLAGS="-no-cpp-precomp -flat_namespace -undefined suppress -multiply_defined suppress"
		;;
esac

# Assume system is ELF if it predefines __ELF__ as 1,
# otherwise believe host_os based default.
case $host_os in
    freebsd1*|freebsd2*) elf=no;;
    freebsd3*|freebsd4*) elf=yes;;
esac

AC_EGREP_CPP(yes,
[#if __ELF__
  yes
#endif
],
[ELF_SYS=true],
[if test "X$elf" = "Xyes" ; then
  ELF_SYS=true
else
  ELF_SYS=
fi])
AC_SUBST(ELF_SYS)



AC_OUTPUT(Makefile.config)

AC_MSG_RESULT([ ])
AC_MSG_RESULT([ SUMMARY])
AC_MSG_RESULT([ -------------------------------------------------------])
AC_MSG_RESULT([ ])
AC_MSG_RESULT([ HOST_OS: $host_os])
AC_MSG_RESULT([ ])
AC_MSG_RESULT([   PGSQL: $PGCONFIG])

if test $USE_JTS -gt 0; then
	AC_MSG_RESULT([     JTS: includedir=$JTS_INCLUDES libdir=$JTS_LIBDIR])
fi
if test $USE_GEOS -gt 0; then
	if test $USE_GEOS_CAPI -gt 0; then
		AC_MSG_RESULT([    GEOS: $GEOSCONFIG (with C-API)])
	else
		AC_MSG_RESULT([    GEOS: $GEOSCONFIG])
	fi
fi
if test $USE_PROJ -gt 0; then
	AC_MSG_RESULT([    PROJ: dir=$PROJ_DIR])
fi
AC_MSG_RESULT([   ICONV: $USE_ICONV $ICONV_LDFLAGS])
dnl AC_MSG_RESULT([  FLEX:  path=$FLEX])
dnl AC_MSG_RESULT([  YACC:  path=$YACC])

AC_MSG_RESULT([  ])
AC_MSG_RESULT([ PORTNAME: $PORTNAME])
AC_MSG_RESULT([   PREFIX: $prefix])
AC_MSG_RESULT([  EPREFIX: $exec_prefix])
AC_MSG_RESULT([      DOC: $docdir])
AC_MSG_RESULT([     DATA: $datadir])
AC_MSG_RESULT([      MAN: $mandir])
AC_MSG_RESULT([      BIN: $bindir])
if test "$MODULE_INSTALLDIR" = "$LPATH"; then
	AC_MSG_RESULT([      EXT: $libdir])
else
	AC_MSG_RESULT([      EXT: $libdir ($LPATH)])
fi

AC_MSG_RESULT([ -------------------------------------------------------])
AC_MSG_RESULT([ ])

