dnl -------------------------------------------------------------------------*
dnl             PDFlib - A library for generating PDF on the fly             |
dnl--------------------------------------------------------------------------+
dnl Copyright (c) 1997-2001 PDFlib GmbH and Thomas Merz. All rights reserved.|
dnl -------------------------------------------------------------------------+
dnl   This software is NOT in the public domain.  It can be used under two   |
dnl   substantially different licensing terms:                               |
dnl                                                                          |
dnl   The commercial license is available for a fee, and allows you to       |
dnl   - ship a commercial product based on PDFlib                            |
dnl   - implement commercial Web services with PDFlib                        |
dnl   - distribute (free or commercial) software when the source code is     |
dnl     not made available                                                   |
dnl   Details can be found in the file PDFlib-license.pdf.                   |
dnl                                                                          |
dnl   The "Aladdin Free Public License" doesn't require any license fee,     |
dnl   and allows you to                                                      |
dnl   - develop and distribute PDFlib-based software for which the complete  |
dnl     source code is made available                                        |
dnl   - redistribute PDFlib non-commercially under certain conditions        |
dnl   - redistribute PDFlib on digital media for a fee if the complete       |
dnl     contents of the media are freely redistributable                     |
dnl   Details can be found in the file aladdin-license.pdf.                  |
dnl                                                                          |
dnl   These conditions extend to ports to other programming languages.       |
dnl   PDFlib is distributed with no warranty of any kind. Commercial users,  |
dnl   however, will receive warranty and support statements in writing.      |
dnl -------------------------------------------------------------------------.

dnl $Id: configure.in,v 1.52.2.4 2001/05/17 16:30:04 rjs Exp $
dnl 
dnl Process this file with autoconf to produce a configure script.
dnl 

# --------------------------------------------------------------------
# 	PDFlib general setup
# --------------------------------------------------------------------

dnl source directory sanity check using an arbitrary source file
AC_INIT(pdflib/pdflib.h)

dnl do not change this (version.sh will do it for you :)
VERSION="4.0.1"
PDFLIB_MAJOR=4
PDFLIB_MINOR=0
PDFLIB_REVISION=1

AC_SUBST(PDFLIB_MAJOR)
AC_SUBST(PDFLIB_MINOR)
AC_SUBST(PDFLIB_REVISION)

dnl Important: Interface numbers are completely independent of release
dnl or version numbers! See doc/readme_unix.txt for details.

PDFLIB_LTCURRENT=2
PDFLIB_LTREVISION=1
PDFLIB_LTAGE=1

LTVERSIONINFO="$PDFLIB_LTCURRENT:$PDFLIB_LTREVISION:$PDFLIB_LTAGE"

AC_SUBST(LTVERSIONINFO)
AC_SUBST(PDFLIB_LTCURRENT)
AC_SUBST(PDFLIB_LTREVISION)
AC_SUBST(PDFLIB_LTAGE)

dnl The name of the library should only be changed in very rare cases,
dnl such as Digital Unix where a (completely different) libpdf already
dnl exists.
dnl Note: do not include any file name suffix here.

PDFLIBNAME=pdf
MATHLIB="-lm"

AC_ARG_WITH(pdflibname,[  --with-pdflibname=name  set file name of generated library [default=pdf]],[PDFLIBNAME="$withval"])

AC_ARG_WITH(libm,[  --with-libm=name        set math library option [default=-lm]],[MATHLIB="$withval"])

AC_SUBST(PDFLIBNAME)

dnl install-sh will be searched (and found) here
AC_CONFIG_AUX_DIR(config)

builtin(include, config/aclocal.m4)

dnl Check the system name
AC_CANONICAL_HOST

AC_MSG_CHECKING(MACHDEP)
if test -z "$MACHDEP"
then
    if test -f /usr/lib/NextStep/software_version; then
	set X `hostinfo | grep 'NeXT Mach.*:' | \
		sed -e 's/://' -e 's/\./_/'` && \
	ac_sys_system=next && ac_sys_release=$4
	MACHDEP="$ac_sys_system$ac_sys_release$ac_sys_cpu"
    else
	ac_sys_system=`uname -s`
	if test "$ac_sys_system" = "AIX" ; then
		ac_sys_release=`uname -v`
	else
		ac_sys_release=`uname -r`
	fi
	ac_md_system=`echo $ac_sys_system |
			   tr -d '[/ ]' | tr '[[A-Z]]' '[[a-z]]'`
	ac_md_release=`echo $ac_sys_release |
			   tr -d '[/ ]' | sed 's/\..*//'`
	MACHDEP="$ac_md_system$ac_md_release"
    fi
    case MACHDEP in
    '')	MACHDEP=unknown;;
    esac
fi

AC_MSG_RESULT($MACHDEP)
PLATFORM="-DPDF_PLATFORM=\\\"\"$ac_sys_system $ac_sys_release\"\\\""

dnl choose compiler
AC_PROG_CC
AC_PROG_CPP
AC_PROG_INSTALL
dnl common stuff, unlikely to be changed
SHELL="/bin/sh"
RM="rm -rf"

dnl Default defines -- some systems may need special handling here

INCLUDES=

case $ac_sys_system in
    dnl Our TIFF code currently requires special handling on AIX
    aix*|AIX*)
	DEFINES="$DEFINES -DAIX";;

    cyg*|CYG*) DEFINES="$DEFINES -U_WIN32 -UWIN32 -U__WIN32__ -U_Windows";;

    dnl HP needs option for ANSI mode if the HP compiler is used instead of gcc
    hp*|HP*)
	if test "$GCC" = ""; then 
	    DEFINES="$DEFINES -Aa"
	fi;;

    dnl The OSF JNI requires libraries to be built thread-safe, which is true
    dnl for PDFlib
    osf1*|OSF1*) DEFINES="$DEFINES -pthread";;

    dnl MacOS X doesn't seem to like -lm, and doesn't actually need it;
    dnl libtool only works if --with-gnu-ld=yes is supplied, so we try
    dnl to set it here already.
    rhapsody|Rhapsody*)
	with_gnu_ld=yes
	MATHLIB="";;

    darwin|Darwin*)
	with_gnu_ld=yes
	MATHLIB="";;

    dnl OS/390 specials
    *OS/390*)
	DEFINES="$DEFINES -DOS390"
	CC=c89
	LD=c89;;

    *)	;;
esac

GENERATED_FILES=""

dnl select debugging configuration
WITH_DEBUG=no

dnl We hope to build shared libraries on this platform
WITH_SHARED=yes

WITH_PROFILE=no

AC_ARG_ENABLE(debug, [  --enable-debug          turn on debugging],
if test "$enableval" = "yes" ; then
    WITH_DEBUG=yes
fi)

AC_ARG_ENABLE(profile, [  --enable-profile        turn on profiling],
if test "$enableval" = "yes" ; then
    WITH_PROFILE=yes
fi)

AC_EXEEXT
AC_OBJEXT

dnl Prepare libtool
dnl Required for dlopening our libraries, as done in the language interpreters.

AC_LIBTOOL_DLOPEN
AC_LIBTOOL_WIN32_DLL
AM_PROG_LIBTOOL

AC_SUBST(SHELL)
AC_SUBST(EXEEXT)
AC_SUBST(OBJEXT)
AC_SUBST(VERSION)
AC_SUBST(CC)
AC_SUBST(RM)

# --------------------------------------------------------------------
# 	PDFlib Java binding (JNI)
# --------------------------------------------------------------------

WITH_JAVA=yes
JAVAINCLUDE=""
JAVAPACKAGE=""

AC_ARG_WITH(java,[  --with-java=path        full path of Java Developers Kit, or no],[
case $withval in
    no) WITH_JAVA=no ;;
    yes) WITH_JAVA=yes ;;
    *) JAVAPACKAGE=$withval 
    ;;
esac
])

if test "$WITH_JAVA" != "no" ; then
    WITH_JAVA=no
    AC_MSG_CHECKING(for Java header files)

    # try without any precautions
    AC_TRY_CPP([#include <jni.h>], WITH_JAVA=yes,
	[ dirs="$prefix /usr/lib/java /usr/java /usr/java/jdk1.3 /usr/include/java /usr/local/java /usr/local/include/kaffe /usr/lib/jdk1.3 /opt/java1.3 /usr/lpp/java/J1.3 /usr/java/jdk1.2 /usr/lib/jdk1.2.2 /usr/lib/jdk1.2.1 /usr/jdk122 /usr/lib/jdk122 /usr/lib/jdk1.2.1 /opt/java1.2 /usr/lpp/java/J1.2 /usr/lib/jdk1.1.8 /usr/jdk118 /usr/lib/jdk118 /usr/lpp/java/J1.1 /opt/java1.1 /usr /opt/local /jdk1.3 /jdk1.2.3 /jdk1.2.2 /jdk1.2.1 /System/Library/Frameworks/JavaVM.framework/Headers /usr/jdk_base/include"

	if test "$JAVAPACKAGE" != ""; then
	    dirs="$JAVAPACKAGE $dirs"
	fi

	# now try to find jni.h
	for i in $dirs ; do
	    if test -r $i/jni.h; then
		JAVAINCLUDE="$i"
		break
	    elif test -r $i/include/jni.h; then
		JAVAINCLUDE="$i/include"
		break
	    fi
	done
    ])

    # now try with an include path
    if test "$JAVAINCLUDE" != ""; then
	OCPPFLAGS="$CPPFLAGS"
	CPPFLAGS="-I$JAVAINCLUDE $OCPPFLAGS"
	AC_TRY_CPP([#include <jni.h>], [
		WITH_JAVA=yes
		JAVAINCLUDE="-I$JAVAINCLUDE"
	],)
	CPPFLAGS="$OCPPFLAGS"
    fi

    # if the above didn't work probably jni_md.h is missing
    if test "WITH_JAVA" != "yes" ; then
	if test "$JAVAINCLUDE" != ""; then
	    subdirs="$JAVAINCLUDE $JAVAINCLUDE/$MACHDEP $JAVAINCLUDE/$ac_md_system $JAVAINCLUDE/solaris $JAVAINCLUDE/genunix $JAVAINCLUDE/alpha $JAVAINCLUDE/win32 $JAVAINCLUDE/mvs"
	    for i in $subdirs ; do
		if test -r $i/jni_md.h; then
		    JAVAINCLUDE="-I$JAVAINCLUDE -I$i"
		    WITH_JAVA=yes
		    break
		fi
	    done
	fi
    fi

    if test "$WITH_JAVA" = "no"; then
	AC_MSG_RESULT(not found)
    else
	GENERATED_FILES="$GENERATED_FILES bind/java/Makefile"
	AC_MSG_RESULT($JAVAINCLUDE)
	AC_SUBST(JAVAINCLUDE)
    fi

fi

# ----------------------------------------------------------------
#  PDFlib Perl binding
# ----------------------------------------------------------------

WITH_PERL=yes
PERLBIN=nope
PERLINCLUDE=nope

AC_ARG_WITH(perl,[  --with-perl=path        full path of Perl executable, or no],[PERLBIN="$withval"],[PERLBIN=nope])

if test "$PERLBIN" = "no" ; then
    WITH_PERL=no
fi

AC_ARG_WITH(perlincl,[  --with-perlincl=path    full path of Perl include directory],[PERLINCLUDE="$withval"],[PERLINCLUDE=nope])

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

    WITH_PERL=no
    dnl First figure out what the name of Perl is
    if test "$PERLBIN" = "nope"; then
	AC_CHECK_PROGS(PERL, perl perl5.7.0 perl5.6.0 perl5.005 perl5.004 perl5.003 perl5.002 perl5.001 perl5, nope)
    else
	PERL="$PERLBIN"
    fi
    AC_MSG_CHECKING(for Perl header file)
    if test "$PERL" != "nope"; then
	PERL5DIR=`($PERL -e 'use Config; print $Config{archlib};') 2>/dev/null`
	if test "$PERL5DIR" != ""; then
	    if test "$PERLINCLUDE" = "nope"; then
		if test -r "$PERL5DIR/perl.h"; then
		    AC_MSG_RESULT(PERL5DIR/perl.h)
		    WITH_PERL=yes
		    PERLINCLUDE="$PERL5DIR"
		elif test -r "$PERL5DIR/CORE/perl.h"; then
		    AC_MSG_RESULT($PERL5DIR/CORE/perl.h)
		    WITH_PERL=yes
		    PERLINCLUDE="$PERL5DIR/CORE"
		fi
	    else
		WITH_PERL=yes
		AC_MSG_RESULT($PERLINCLUDE)
	    fi
	    if test "$PERLINCLUDE" = "nope"; then
		AC_MSG_RESULT(could not locate perl.h...)
		WITH_PERL=no
	    fi	
	else
	    AC_MSG_RESULT(unable to determine Perl configuration)
	    WITH_PERL=no
	fi

	PERLLIBDIR=`($PERL -e 'use Config; print $Config{sitearchexp};') 2>/dev/null`
	if test -z "$PERLLIBDIR" -o ! -d "$PERLLIBDIR"; then
	    AC_MSG_RESULT(         unable to determine perl shared library directory)
	fi

	PERLMODDIR=`($PERL -e 'use Config; print $Config{sitelib};') 2>/dev/null`
	if test -z "$PERLMODDIR" -o ! -d "$PERLMODDIR"; then
	    AC_MSG_RESULT(         unable to determine perl module directory)
	    PERLMODDIR="/usr/local/lib/perl5/site_perl/5.6.0"
	fi
    else
	AC_MSG_RESULT(could not figure out how to run perl)
	PERLINCLUDE="/usr/local/lib/perl5/5.6.0/archname/CORE"
    fi

    dnl The Perl include files contain special handling for the bool type
    dnl on NeXT (sigh). We'll run into trouble if we don't work around this.

    case $ac_sys_system in
	    NeXT|next) PERLBOOLDEF="";;
	    *)	PERLBOOLDEF="-Dbool=char -DHAS_BOOL";;
    esac

    dnl Some platforms require linking the perl library directly.
    dnl We use $PERLINCLUDE for this since it both contains the Perl library
    dnl _and_ the include files. Other systems don't explicitly link the
    dnl Perl library, so we put only $MATHLIB in PERLLINK.
    dnl $MATHLIB is not required on all systems/perl builds, but those
    dnl who need it choke fatally without it. HP-UX, on the other hand,
    dnl chokes when we supply -lm.

    PERLLINK="$MATHLIB";

    case $ac_sys_system/$ac_sys_release in
	hp*|HP*)     PERLLINK="";;
	cyg*|CYG*)   PERLLINK="-L$PERLINCLUDE -lperl $PERLLINK";;
	aix*|AIX*)   PERLLINK="-L$PERLINCLUDE -lperl $PERLLINK";;
	osf1*|OSF1*) PERLLINK="-L$PERLINCLUDE -lperl $PERLLINK";;
	*) ;;
    esac

    if test "$WITH_PERL" = "yes" ; then
	GENERATED_FILES="$GENERATED_FILES bind/perl/Makefile"
    fi

    PERLINCLUDE="-I$PERLINCLUDE"
    AC_SUBST(PERLINCLUDE)
    AC_SUBST(PERLLIBDIR)
    AC_SUBST(PERLMODDIR)
    AC_SUBST(PERLBOOLDEF)
    AC_SUBST(PERLLINK)
    AC_SUBST(PERL)
fi

# ----------------------------------------------------------------
#  PDFlib Python binding
# ----------------------------------------------------------------

WITH_PYTHON=yes
PYINCLUDE=nope
PYPACKAGE=nope

AC_ARG_WITH(py,[  --with-py=path          full path of Python root directory, or no],[PYPACKAGE="$withval"], [PYPACKAGE=nope])

if test "$PYPACKAGE" = "no" ; then
    WITH_PYTHON=no
fi

AC_ARG_WITH(pyincl,[  --with-pyincl=path      full path of Python include directory],[PYINCLUDE="$withval"], [PYINCLUDE=nope])

if test "$PYINCLUDE" = "nope" -a "$PYPACKAGE" != "no"; then
	PYINCLUDE="$PYPACKAGE/include"
fi

if test "$WITH_PYTHON" != "no" ; then
    WITH_PYTHON=no
    AC_MSG_CHECKING(for Python header files)

    dirs="$PYINCLUDE 			\
    $PYINCLUDE/python2.0 		\
    $prefix/include/python2.0 		\
    /usr/local/include/python2.0 	\
    /usr/include/python2.0 		\
    $prefix/include/python2.0		\
    $PYINCLUDE/python1.6		\
    $prefix/include/python1.6		\
    /usr/local/include/python1.6	\
    /usr/include/python1.6		\
    $prefix/include/python1.6		\
    $PYINCLUDE/python1.5		\
    $prefix/include/python1.5		\
    /usr/local/include/python1.5	\
    /usr/include/python1.5		\
    $prefix/include/python1.5		\
    $PYINCLUDE/python1.4		\
    $prefix/include/python1.4		\
    /usr/local/include/python1.4	\
    /usr/include/python1.4		\
    $prefix/include/python1.4"
    for i in $dirs ; do
	    if test -r $i/Python.h; then
		    AC_MSG_RESULT($i)
		    PYINCLUDE="-I$i"
		    WITH_PYTHON=yes
		    break
	    fi
    done
    if test "$PYINCLUDE" = "nope" -o "$PYINCLUDE" = "$PYPACKAGE/include"; then
	AC_MSG_RESULT(not found)
	WITH_PYTHON=no
    fi

    AC_SUBST(PYINCLUDE)

    AC_CHECK_PROGS(PYTHONBIN, python, nope)

    if test "$PYTHONBIN" = "nope" ; then
	WITH_PYTHON=no
    fi

    if test "$WITH_PYTHON" = "yes" ; then
	AC_MSG_CHECKING(for Python shared library path)

	if test "$PYTHONBIN" = "nope" ; then
	    WITH_PYTHON=no
	else
	    PYTHONLIBDIR=`cat << EOF | python
import sys
import string
print string.join(filter(lambda x: -1 != string.find(x, 'dynload'), sys.path))
EOF
`
	    if test "$PYTHONLIBDIR" = "" ; then
		PYTHONLIBDIR=/usr/lib
	    fi
	    AC_SUBST(PYTHONLIBDIR)

	fi

	GENERATED_FILES="$GENERATED_FILES bind/python/Makefile"

	AC_MSG_RESULT($PYTHONLIBDIR)
    fi
fi

# --------------------------------------------------------------------
# 	PDFlib Tcl binding
# --------------------------------------------------------------------

WITH_TCL=yes
TCLINCLUDE=nope
TCLPACKAGEDIR=nope
TCLBIN=nope

AC_ARG_WITH(tcl,[  --with-tcl=path         full path of Tcl executable, or no],[TCLBIN="$withval"])

if test "$TCLBIN" = "no" ; then
    WITH_TCL=no
fi

AC_ARG_WITH(tclpkg,[  --with-tclpkg=path      full path of Tcl package install directory],[TCLPACKAGEDIR="$withval"],[TCLPACKAGEDIR=nope])

AC_ARG_WITH(tclincl,[  --with-tclincl=path     full path of Tcl include directory],[TCLINCLUDE="$withval"],[TCLINCLUDE=nope])

if test "$WITH_TCL" = "yes" ; then
    WITH_TCL=no
    AC_MSG_CHECKING(for Tcl header files)

    if test "$TCLINCLUDE" = "nope"; then
	AC_TRY_CPP([#include <tcl.h>], , TCLINCLUDE="nope")
	if test "$TCLINCLUDE" = "nope"; then
	    dirs="$prefix/include /usr/local/include /usr/include /opt/local/include /home/sci/local/include /usr/pkg/include /System/Library/Frameworks/Tcl.framework/Versions/8.3/Headers /System/Library/Frameworks/Tcl.framework/Versions/8.2/Headers"
	    for i in $dirs ; do
		if test -r $i/tcl.h; then
		    AC_MSG_RESULT($i)
		    TCLINCLUDE="$i"
		    WITH_TCL=yes
		    break
		fi
	    done
	fi

	if test "$TCLINCLUDE" = "nope"; then
	    AC_MSG_RESULT(not found)
	    WITH_TCL=no
	fi
    else
	AC_MSG_RESULT($TCLINCLUDE)
    fi

    dnl We need at least Tcl 8.0 because of its support for binary strings
    dnl and the object interface.

    if test "$TCLINCLUDE" != "nope" -a "$TCLBIN" = "nope"; then
	AC_CHECK_PROGS(TCL, tclsh tclsh8.4 tclsh8.3 tclsh8.2 tclsh8.1 tclsh8.0, nope)
	TCLBIN=$TCL
    fi

    if test "$TCLINCLUDE" = "nope" -o "$TCLBIN" = "nope"; then
	WITH_TCL=no
    else
	dnl Check the installed Tcl version -- must be 8.0 or higher
	TCLVERSION=`echo "puts [[info tclversion]]" | $TCLBIN`
	if test  "$TCLVERSION" = "7.6" -o "$TCLVERSION" = "7.5" -o  "$TCLVERSION" = "7.4" -o "$TCLVERSION" = "7.3" -o "$TCLVERSION" = "7.1" -o "$TCLVERSION" = "7.0"; then
	    AC_MSG_RESULT([         Tcl version 8.0 or newer is required for PDFlib.])
	    AC_MSG_RESULT([         PDFlib support for Tcl will not be built.])
	    WITH_TCL=no
	else
	    if test  "$TCLVERSION" = "8.0" -o "$TCLVERSION" = "8.1"; then
		AC_MSG_RESULT([         The PDFlib build process requires Tcl 8.2 or above. Please read])
		AC_MSG_RESULT([         bind/tcl/readme.txt for instructions on manually building PDFlib])
		AC_MSG_RESULT([         for Tcl 8.0 or 8.1.])
	    fi
	
	    if test "$TCLPACKAGEDIR" = "nope"; then
	    dnl Let tcl decide where to install the package
		TCLPACKAGEDIR=`(echo "puts \\$auto_path" | "$TCLBIN" | awk '{print $1}') 2>/dev/null`
	    fi

	    if test "$TCLPACKAGEDIR" = "nope" -o -z "$TCLPACKAGEDIR" -o ! -d "$TCLPACKAGEDIR"; then
		AC_MSG_RESULT(unable to determine Tcl package directory)
		WITH_TCL=no

	    else

		TCLPACKAGEDIR=$TCLPACKAGEDIR/pdflib
		WITH_TCL=yes
	    fi
	fi
    fi

    if test "$WITH_TCL" = "yes" ; then
	GENERATED_FILES="$GENERATED_FILES bind/tcl/Makefile"
    fi
fi

TCLINCLUDE="-I$TCLINCLUDE"
AC_SUBST(TCLINCLUDE)
AC_SUBST(TCLPACKAGEDIR)
AC_SUBST(TCLBIN)

# ----------------------------------------------------------------
#  PDFlib auxiliary library: TIFFlib
#  We bring our own copy of tifflib but the client may choose to
#  use an installed one instead. This is only support for C/C++ clients.
# ----------------------------------------------------------------

# The list of Makefile targets for our internal libraries
LIBTARGETS=""
INTERNALLIBS=""

WITH_INTERNALTIFFLIB="yes"
TIFFLIBINC=""
EXTERNALLIBS="$MATHLIB"

AC_ARG_WITH(tifflib,
[  --with-tifflib          use installed tifflib instead of internal (not rec.)],
[
    AC_CHECK_LIB(tiff, TIFFOpen, [EXTERNALLIBS="$EXTERNALLIBS -ltiff"],
	AC_MSG_ERROR([No usable tifflib library found.]))

    AC_CHECK_HEADER(tiff.h, , 
	AC_MSG_ERROR([No usable tifflib include file found.]))

    AC_MSG_RESULT([         Note: user-supplied tifflib - language bindings disabled])
    WITH_INTERNALTIFFLIB="no"
],[
    AC_C_BIGENDIAN()

    TIFFLIBINC="-I../tiff"
    INTERNALLIBS="$INTERNALLIBS \$(top_builddir)/tiff/libtiff.la"
    LIBTARGETS="$LIBTARGETS libtiff"
    GENERATED_FILES="$GENERATED_FILES tiff/Makefile"
])

AC_SUBST(TIFFLIBINC)

# ----------------------------------------------------------------------
#  PDFlib auxiliary library: Zlib
#  We bring our own copy of zlib but the client may choose to
#  use an installed one instead. This is only support for C/C++ clients.
# ----------------------------------------------------------------------

ZLIBINC=""
WITH_INTERNALZLIB="yes"

AC_ARG_WITH(zlib,
[  --with-zlib             use installed zlib instead of internal (not rec.)],
[
    AC_CHECK_LIB(z, deflateEnd, [EXTERNALLIBS="$EXTERNALLIBS -lz"],
	AC_MSG_ERROR([No usable zlib compression library found.]))

    AC_CHECK_HEADER(zlib.h, , 
	AC_MSG_ERROR([No usable zlib include file found.]))

    AC_MSG_RESULT([         Note: user-supplied zlib - language bindings disabled])
    WITH_INTERNALZLIB="no"
],[
    ZLIBINC="-I../flate"
    INTERNALLIBS="$INTERNALLIBS \$(top_builddir)/flate/libz.la"
    LIBTARGETS="$LIBTARGETS libz"
    GENERATED_FILES="$GENERATED_FILES flate/Makefile"
])

AC_SUBST(ZLIBINC)

# ---------------------------------------------------------------------
#  PDFlib auxiliary library: libpng
#  We bring our own copy of pnglib but the client may choose to
#  use an installed one instead. This is only support for C/C++ clients.
# ---------------------------------------------------------------------

PNGLIBINC=""
WITH_INTERNALPNGLIB=$WITH_INTERNALZLIB

AC_ARG_WITH(pnglib,
[  --with-pnglib           use installed pnglib instead of internal (not rec.)],
[
    if test "$WITH_INTERNALZLIB" = "yes"; then
	AC_MSG_ERROR([--with-pnglib requires --with-zlib])
    fi

    dnl Make sure libpng sits in front of libz
    AC_CHECK_LIB(png, png_create_read_struct,
	[EXTERNALLIBS="-lpng $EXTERNALLIBS"],
	AC_MSG_ERROR([No usable pnglib library found.]))

    AC_CHECK_HEADER(png.h, , 
	AC_MSG_ERROR([No usable pnglib include file found.]))

    AC_MSG_RESULT([         Note: user-supplied pnglib - language bindings disabled])
    WITH_INTERNALPNGLIB="no"
],[
    if test "$WITH_INTERNALZLIB" = "no"; then
	AC_MSG_ERROR([--with-zlib requires --with-pnglib])
    fi

    PNGLIBINC="-I../png"
    dnl PNG must be in front because it needs zlib
    INTERNALLIBS="\$(top_builddir)/png/libpng.la $INTERNALLIBS"
    LIBTARGETS="$LIBTARGETS libpng"
    GENERATED_FILES="$GENERATED_FILES png/Makefile"
])

AC_SUBST(PNGLIBINC)

# ----------------------------------------------------------------
#  PDFlib auxiliary library: PDI (PDF import library)
# ----------------------------------------------------------------

WITH_PDI=no

AC_MSG_CHECKING(for PDF import library PDI)

if test -f pdi/pdi.h ; then
    dnl PDI must be in front because it needs zlib
    INTERNALLIBS="\$(top_builddir)/pdi/libpdi.la $INTERNALLIBS"
    DEFINES="$DEFINES -DHAVE_PDI"
    WITH_PDI=yes
    LIBTARGETS="$LIBTARGETS libpdi"
    GENERATED_FILES="$GENERATED_FILES pdi/Makefile"
    AC_MSG_RESULT(found)
else
    AC_MSG_RESULT(not found)
fi

if test -f dist.mak.in ; then
    GENERATED_FILES="$GENERATED_FILES dist.mak"
    PDFPWD=`pwd`
    AC_SUBST(PDFPWD)
fi

# ----------------------------------------------------------------
#  PDFlib PHP binding
# ----------------------------------------------------------------

WITH_PHP=yes

AC_ARG_ENABLE(php,[  --enable-php            enable PHP language binding [default=yes]], WITH_PHP=$enableval)

if test "$WITH_PDI" = "yes" -a "$WITH_PHP" = "yes"; then
    WITH_PHP=""
    AC_CHECK_PROG(WITH_PHP, phpize, yes, no)
    if test "$WITH_PHP" = ""; then
	WITH_PHP="no"
    fi
    if test "$WITH_PHP" = "yes"; then
	GENERATED_FILES="$GENERATED_FILES bind/php/Makefile.in"
    fi
else
    if test "$WITH_PDI" = "no"; then
	WITH_PHP=yes
    else
	WITH_PHP=no
    fi
fi

# If at least one library has been substituted we disable most
# language bindings.

if test "$EXTERNALLIBS" != "$MATHLIB"; then
    WITH_JAVA=no
    WITH_PERL=no
    WITH_PYTHON=no
    WITH_TCL=no
    WITH_PHP=no
fi

# --------------------------------------------------------------------
# 	PDFlib C++ language binding
# --------------------------------------------------------------------

dnl C++ mode is disabled by default since it seems impossible to come up
dnl with a portable solution for linking the stdc++ library.
dnl If we have PDI, we check C++ mode, too.

if test "$WITH_PDI" = "yes"; then
    WITH_CXX=yes
else
    WITH_CXX=no
fi

STDCPP=""

AC_ARG_ENABLE(cxx,[  --enable-cxx            enable C++ language binding [default=no]], WITH_CXX=$enableval)

if test "$WITH_CXX" = "yes"; then
    AC_LANG_CPLUSPLUS
    AC_PROG_CXX
    AC_MSG_CHECKING(whether the previously found C++ compiler works)
    AC_TRY_COMPILE(, [class a { int b; } ], WITH_CXX=yes, WITH_CXX=no)
    AC_LANG_C

    case $ac_sys_system/$ac_sys_release in
	osf1*|OSF1*) STDCPP="";;

	*)	dnl don't change $WITH_CXX
		;;
    esac

    dnl If working with gcc add the name of the stdc++ library
    if test "x$GCC" != "x"; then
	STDCPP="-lstdc++"
    fi

    if test "$WITH_CXX" = "yes"; then
	AC_SUBST(STDCPP)
	AC_SUBST(CXX)
	AC_MSG_RESULT(yes)
	GENERATED_FILES="$GENERATED_FILES bind/cpp/Makefile"
    else
	AC_MSG_RESULT(no)
    fi
fi

# ----------------------------------------------------------------
#  PDFlib wrapup
# ----------------------------------------------------------------

dnl The following variables are used to tell the Makefile what to
dnl actually do, depending on the available language bindings.
dnl PDFlib itself will be built and installed by default, regardless
dnl of the settings below.

BINDINGS="c"
BINDINGS_INSTALL=""
BINDINGS_UNINSTALL=""
BINDINGS_CLEAN="c_clean"
BINDINGS_TEST="c_test"

if ${CONFIG_SHELL-/bin/sh} ./libtool --features | grep "disable shared" >/dev/null; then
    WITH_SHARED=no
fi

dnl Building the language wrappers based on a shared PDFlib is no longer supported!
if test "$WITH_SHARED" = "no"; then
    WITH_JAVA=no
    WITH_PERL=no
    WITH_PYTHON=no
    WITH_TCL=no
    WITH_PHP=no
fi

if test "$WITH_CXX" = "yes" ; then
    BINDINGS="$BINDINGS cpp"
    BINDINGS_TEST="$BINDINGS_TEST cpp_test"
    BINDINGS_CLEAN="$BINDINGS_CLEAN cpp_clean"
fi
if test "$WITH_JAVA" = "yes" ; then
    BINDINGS="$BINDINGS java"
    BINDINGS_INSTALL="$BINDINGS_INSTALL java_install"
    BINDINGS_UNINSTALL="$BINDINGS_UNINSTALL java_uninstall"
    BINDINGS_TEST="$BINDINGS_TEST java_test"
    BINDINGS_CLEAN="$BINDINGS_CLEAN java_clean"
fi
if test "$WITH_PERL" = "yes" ; then
    BINDINGS="$BINDINGS perl"
    BINDINGS_INSTALL="$BINDINGS_INSTALL perl_install"
    BINDINGS_UNINSTALL="$BINDINGS_UNINSTALL perl_uninstall"
    BINDINGS_TEST="$BINDINGS_TEST perl_test"
    BINDINGS_CLEAN="$BINDINGS_CLEAN perl_clean"
fi
if test "$WITH_PYTHON" = "yes" ; then
    BINDINGS="$BINDINGS python"
    BINDINGS_INSTALL="$BINDINGS_INSTALL python_install"
    BINDINGS_UNINSTALL="$BINDINGS_UNINSTALL python_uninstall"
    BINDINGS_TEST="$BINDINGS_TEST python_test"
    BINDINGS_CLEAN="$BINDINGS_CLEAN python_clean"
fi
if test "$WITH_TCL" = "yes" ; then
    BINDINGS="$BINDINGS tcl"
    BINDINGS_INSTALL="$BINDINGS_INSTALL tcl_install"
    BINDINGS_UNINSTALL="$BINDINGS_UNINSTALL tcl_uninstall"
    BINDINGS_TEST="$BINDINGS_TEST tcl_test"
    BINDINGS_CLEAN="$BINDINGS_CLEAN tcl_clean"
fi

if test "$WITH_DEBUG" = "yes"; then
    CFLAGS="$CFLAGS -g -DDEBUG"
fi

if test "$WITH_PROFILE" = "yes"; then
    CFLAGS="$CFLAGS -pg"
    LDFLAGS="$LDFLAGS -pg"
fi

DEFINES="$CFLAGS $DEFINES $INCLUDES $PLATFORM"
EXTERNALLIBS="$LDFLAGS $EXTERNALLIBS"

AC_SUBST(BINDINGS)
AC_SUBST(WITH_PDI)
AC_SUBST(LDFLAGS)
AC_SUBST(LIBTARGETS)
AC_SUBST(BINDINGS_INSTALL)
AC_SUBST(BINDINGS_UNINSTALL)
AC_SUBST(BINDINGS_TEST)
AC_SUBST(BINDINGS_CLEAN)

dnl PDFLIBCONVENIENT contains all convenient libs to build shared libs
dnl		     like PDFlib, and the wrapper libraries.
dnl PDFLIBLINKSELF   contains the flags needed to link PDFlib itself
dnl PDFLIBLINK       contains the files needed to link a program against PDFlib

if test "$WITH_SHARED" = "no"; then
    dnl We can generate a static lib only, but not a shared
    RPATH=""
else
    dnl we generate both static and shared libs
    RPATH="-rpath \$(libdir) -version-info $LTVERSIONINFO"

fi

PDFLIBCONVENIENT="\$(top_builddir)/pdflib/libpdf_.la $INTERNALLIBS"
PDFLIBLINKSELF="$RPATH $PDFLIBCONVENIENT"
PDFLIBLINK="\$(top_builddir)/pdflib/libpdf.la $EXTERNALLIBS"

AC_SUBST(EXTERNALLIBS)
AC_SUBST(PDFLIBCONVENIENT)
AC_SUBST(PDFLIBLINKSELF)
AC_SUBST(PDFLIBLINK)
AC_SUBST(INTERNALLIBS)
AC_SUBST(INCLUDES)
AC_SUBST(DEFINES)
AC_SUBST(DEFS)
AC_SUBST(WITH_SHARED)

dnl ---------- Generate the Makefiles

GENERATED_FILES="$GENERATED_FILES Makefile pdflib-config pdflib/Makefile test/Makefile clients/Makefile util/Makefile bind/c/Makefile"

AC_OUTPUT([$GENERATED_FILES],[
    chmod +x pdflib-config
])

if test "$WITH_SHARED" = "no" ; then
    AC_MSG_WARN([Can't create shared PDFlib libraries on this platform.])
    AC_MSG_WARN([PDFlib will work when linked to a C or C++ program,])
    AC_MSG_WARN([but other language bindings will be unavailable.])
fi

AC_MSG_RESULT([_____________________________________________________________])
AC_MSG_RESULT([For your convenience, here's a summary of configure's results:])
AC_MSG_RESULT([])
AC_MSG_RESULT([Support for shared libraries:           $WITH_SHARED])
AC_MSG_RESULT([C++ language binding for PDFlib:        $WITH_CXX])
AC_MSG_RESULT([Java language binding for PDFlib:       $WITH_JAVA])
AC_MSG_RESULT([Perl language binding for PDFlib:       $WITH_PERL])
AC_MSG_RESULT([Python language binding for PDFlib:     $WITH_PYTHON])
AC_MSG_RESULT([Tcl language binding for PDFlib:        $WITH_TCL])

if test "$WITH_PHP" = "yes" ; then
    AC_MSG_RESULT([PHP language binding for PDFlib:        yes (you must rebuild PHP)])
else
    AC_MSG_RESULT([PHP language binding for PDFlib:        no])
fi

AC_MSG_RESULT([PDF import library (PDI):               $WITH_PDI])
if test "$WITH_PDI" = "no" ; then
    AC_MSG_RESULT([     Note: if you purchase the additional PDF import library (PDI)])
    AC_MSG_RESULT([     you can also manipulate existing PDF documents with PDFlib.])
    AC_MSG_RESULT([     See http://www.pdflib.com for details.])
fi

AC_MSG_RESULT([])
AC_MSG_RESULT([Please observe the licensing terms for commercial PDFlib usage.])
AC_MSG_RESULT([PDFlib license agreement and purchase order can be found in the doc directory.])
AC_MSG_RESULT([])
