dnl this file has to be processed by autoconf
dnl run ./buildconf.sh to get a working ./configure script

dnl {{{ AX_PREFIX_CONFIG_H
AC_DEFUN([AX_PREFIX_CONFIG_H],[dnl
AC_BEFORE([AC_CONFIG_HEADERS],[$0])dnl
AC_CONFIG_COMMANDS([ifelse($1,,$PACKAGE-config.h,$1)],[dnl
AS_VAR_PUSHDEF([_OUT],[ac_prefix_conf_OUT])dnl
AS_VAR_PUSHDEF([_DEF],[ac_prefix_conf_DEF])dnl
AS_VAR_PUSHDEF([_PKG],[ac_prefix_conf_PKG])dnl
AS_VAR_PUSHDEF([_LOW],[ac_prefix_conf_LOW])dnl
AS_VAR_PUSHDEF([_UPP],[ac_prefix_conf_UPP])dnl
AS_VAR_PUSHDEF([_INP],[ac_prefix_conf_INP])dnl
m4_pushdef([_script],[conftest.prefix])dnl
m4_pushdef([_symbol],[m4_cr_Letters[]m4_cr_digits[]_])dnl
_OUT=`echo ifelse($1, , $PACKAGE-config.h, $1)`
_DEF=`echo _$_OUT | sed -e "y:m4_cr_letters:m4_cr_LETTERS[]:" -e "s/@<:@^m4_cr_Letters@:>@/_/g"`
_PKG=`echo ifelse($2, , $PACKAGE, $2)`
_LOW=`echo _$_PKG | sed -e "y:m4_cr_LETTERS-:m4_cr_letters[]_:"`
_UPP=`echo $_PKG | sed -e "y:m4_cr_letters-:m4_cr_LETTERS[]_:"  -e "/^@<:@m4_cr_digits@:>@/s/^/_/"`
_INP=`echo "ifelse($3,,,$3)" | sed -e 's/ *//'`
if test ".$_INP" = "."; then
   for ac_file in : $CONFIG_HEADERS; do test "_$ac_file" = _: && continue
     case "$ac_file" in
        *.h) _INP=$ac_file ;;
        *)
     esac
     test ".$_INP" != "." && break
   done
fi
if test ".$_INP" = "."; then
   case "$_OUT" in
      */*) _INP=`basename "$_OUT"`
      ;;
      *-*) _INP=`echo "$_OUT" | sed -e "s/@<:@_symbol@:>@*-//"`
      ;;
      *) _INP=config.h
      ;;
   esac
fi
if test -z "$_PKG" ; then
   AC_MSG_ERROR([no prefix for _PREFIX_PKG_CONFIG_H])
else
  if test ! -f "$_INP" ; then if test -f "$srcdir/$_INP" ; then
     _INP="$srcdir/$_INP"
  fi fi
  AC_MSG_NOTICE(creating: $_OUT: prefix $_UPP for $_INP defines)
  if test -f $_INP ; then
    echo "s/^@%:@undef  *\\(@<:@m4_cr_LETTERS[]_@:>@\\)/@%:@undef $_UPP""_\\1/" > _script
    echo "s/^@%:@undef  *\\(@<:@m4_cr_letters@:>@\\)/@%:@undef $_LOW""_\\1/" >> _script
    echo "s/^@%:@def[]ine  *\\(@<:@m4_cr_LETTERS[]_@:>@@<:@_symbol@:>@*\\)\\(.*\\)/@%:@ifndef $_UPP""_\\1 \\" >> _script
    echo "@%:@def[]ine $_UPP""_\\1 \\2 \\" >> _script
    echo "@%:@endif/" >>_script
    echo "s/^@%:@def[]ine  *\\(@<:@m4_cr_letters@:>@@<:@_symbol@:>@*\\)\\(.*\\)/@%:@ifndef $_LOW""_\\1 \\" >> _script
    echo "@%:@define $_LOW""_\\1 \\2 \\" >> _script
    echo "@%:@endif/" >> _script
    # now executing _script on _DEF input to create _OUT output file
    echo "@%:@ifndef $_DEF"      >$tmp/pconfig.h
    echo "@%:@def[]ine $_DEF 1" >>$tmp/pconfig.h
    echo ' ' >>$tmp/pconfig.h
    echo /'*' $_OUT. Generated automatically at end of configure. '*'/ >>$tmp/pconfig.h

    sed -f _script $_INP >>$tmp/pconfig.h
    echo ' ' >>$tmp/pconfig.h
    echo '/* once:' $_DEF '*/' >>$tmp/pconfig.h
    echo "@%:@endif" >>$tmp/pconfig.h
    if cmp -s $_OUT $tmp/pconfig.h 2>/dev/null; then
      rm -f $tmp/pconfig.h
      AC_MSG_NOTICE([unchanged $_OUT])
    else
      ac_dir=`AS_DIRNAME(["$_OUT"])`
      AS_MKDIR_P(["$ac_dir"])
      rm -f "$_OUT"
      mv $tmp/pconfig.h "$_OUT"
    fi
    cp _script _configs.sed
  else
    AC_MSG_ERROR([input file $_INP does not exist - skip generating $_OUT])
  fi
  rm -f conftest.*
fi
m4_popdef([_symbol])dnl
m4_popdef([_script])dnl
AS_VAR_POPDEF([_INP])dnl
AS_VAR_POPDEF([_UPP])dnl
AS_VAR_POPDEF([_LOW])dnl
AS_VAR_POPDEF([_PKG])dnl
AS_VAR_POPDEF([_DEF])dnl
AS_VAR_POPDEF([_OUT])dnl
],[PACKAGE="$PACKAGE"])])
dnl }}}

AC_PREREQ(2.60)

AC_INIT([libhpdf],[2.3.0-dev])
AC_CONFIG_SRCDIR([README])
AC_CONFIG_SRCDIR(include/hpdf.h)
AC_CONFIG_HEADER(include/config.h)
AX_PREFIX_CONFIG_H(include/hpdf_config.h)
AM_INIT_AUTOMAKE([no-define])
AM_MAINTAINER_MODE

dnl Check for programs
AC_PROG_CC
AC_PROG_LD
AM_PROG_LIBTOOL
AC_PROG_INSTALL
AC_PROG_EGREP

dnl Check for size_t
AC_TYPE_SIZE_T

dnl Check for header files
AC_CHECK_HEADERS(string.h strings.h unistd.h stdint.h)

AC_CHECK_LIB([m], [floor], [LIBS="$LIBS -lm"], [AC_MSG_ERROR([can't continue without libm])])

DEFAULT_INSTALL_PREFIX="/usr/local"
STANDARD_PREFIXES="/usr /usr/local /opt /local"

HPDF_MAJOR_VERSION=2
HPDF_MINOR_VERSION=3
HPDF_BUGFIX_VERSION=0
HPDF_EXTRA_VERSION="-dev"

HPDF_VERSION_TEXT="${HPDF_MAJOR_VERSION}.${HPDF_MINOR_VERSION}.${HPDF_BUGFIX_VERSION}${HPDF_EXTRA_VERSION}"
HPDF_VERSION_ID=`expr [$]HPDF_MAJOR_VERSION \* 10000 + [$]HPDF_MINOR_VERSION \* 100 + [$]HPDF_BUGFIX_VERSION`

dnl generate hpdf_version.h {{{
echo "/* automatically generated by configure */" > hpdf_version.h.new
echo "/* edit configure.in to change version number */" >> hpdf_version.h.new
echo "#define HPDF_MAJOR_VERSION $HPDF_MAJOR_VERSION" >> hpdf_version.h.new
echo "#define HPDF_MINOR_VERSION $HPDF_MINOR_VERSION" >> hpdf_version.h.new
echo "#define HPDF_BUGFIX_VERSION $HPDF_BUGFIX_VERSION" >> hpdf_version.h.new
echo "#define HPDF_EXTRA_VERSION \"$HPDF_EXTRA_VERSION\"" >> hpdf_version.h.new
echo "#define HPDF_VERSION_TEXT \"$HPDF_VERSION_TEXT\"" >> hpdf_version.h.new
echo "#define HPDF_VERSION_ID $HPDF_VERSION_ID" >> hpdf_version.h.new
cmp hpdf_version.h.new $srcdir/include/hpdf_version.h >/dev/null 2>&1
if test $? -ne 0 ; then
  rm -f $srcdir/include/hpdf_version.h && mv hpdf_version.h.new $srcdir/include/hpdf_version.h && \
  echo 'Updated include/hpdf_version.h'
else
  rm -f hpdf_version.h.new
fi
dnl }}}

dnl {{{ --enable-debug
AC_ARG_ENABLE(debug,
  [AS_HELP_STRING([--enable-debug],[enable debugging symbols and compile flags])
  ],
  [
    if test x"$enableval" = xyes ; then
      debug="yes"
    else
      debug="no"
    fi
  ]
)

if test x"$debug" = xyes ; then
  AC_DEFINE([DEBUG], [], [debug build])

  if test x"$GCC" = xyes; then

    dnl Remove any optimization flags from CFLAGS
    changequote({,})
    CFLAGS=`echo "$CFLAGS" | /usr/bin/sed -e 's/-O[0-9s]*//g'`
    CFLAGS=`echo "$CFLAGS" | /usr/bin/sed -e 's/-g[0-2]\? //g'`
    changequote([,])
    CFLAGS="$CFLAGS -g3 -Wall -O0"

  fi

  dnl Do not strip symbols from developer object files.
  INSTALL_STRIP_FLAG=""
else
  dnl Make sure to strip symbols from non-developer object files.
  INSTALL_STRIP_FLAG="-s"
fi
dnl }}}

dnl {{{ --enable-debug-trace
AC_ARG_ENABLE(debug-trace,
  [AS_HELP_STRING([--enable-debug-trace],[enable debug trace (only available along with --enable-debug)])
  ],
  [
    if test x"$enableval" = xyes && test x"$debug" = xyes; then
      debug_trace="yes"
    else
      debug_trace="no"
    fi
  ]
)

if test x"$debug_trace" = xyes ; then
  AC_DEFINE([DEBUG_TRACE], [], [debug trace enabled])
fi
dnl }}}

dnl {{{ --with-libdir
AC_ARG_WITH(libdir,
  [AS_HELP_STRING([--with-libdir],[look for libraries in .../NAME rather than .../lib])
  ],
  [LIBDIR=$with_libdir],
  [LIBDIR=lib]
)
dnl }}}

dnl {{{ --with-zlib
AC_ARG_WITH(zlib,
  [AS_HELP_STRING([--with-zlib],[specify Zlib install prefix])
  ],
  [ ],
  [with_zlib=yes]
)

if test "x$with_zlib" = "xno"; then
  AC_DEFINE([HAVE_NOZLIB], [], [zlib is not available])
else
  AC_MSG_CHECKING([Zlib install prefix])

  if test "x$with_zlib" = "xyes"; then
    for i in `echo "$STANDARD_PREFIXES"`; do
      if test -f "$i/include/zlib.h"; then
        ZLIB_DIR="$i"
        break;
      fi
    done
  else
    if test -f "$with_zlib/include/zlib.h"; then
      ZLIB_DIR="$with_zlib"
      break;
    else
      AC_MSG_ERROR([Can't find Zlib headers under $with_zlib directory]);
    fi
  fi

  if test "x$ZLIB_DIR" = "x"; then
    AC_MSG_ERROR([Unable to locate Zlib headers, please use --with-zlib=<DIR>]);
  fi

  AC_MSG_RESULT([$ZLIB_DIR])
  LDFLAGS="$LDFLAGS -L$ZLIB_DIR/$LIBDIR"
  CFLAGS="$CFLAGS -I$ZLIB_DIR/include"
  LIBS="$LIBS -lz"

  AC_CHECK_LIB([z], [deflate], [], [
    AC_MSG_ERROR([deflate() is missing, check config.log for more details])
  ])

  HAVE_ZLIB=yes
fi
dnl }}}

dnl {{{ --with-png
AC_ARG_WITH(png,
  [AS_HELP_STRING([--with-png],[specify libpng install prefix])
  ],
  [ ],
  [with_png=yes]
)

if test "x$with_png" = "xno"; then
  AC_DEFINE([HAVE_NOPNGLIB], [], [libpng is not available])
else

  if test "x$HAVE_ZLIB" != "xyes"; then
    AC_MSG_ERROR([PNG support requires Zlib, but it's not enabled. Either enable Zlib or disable PNG support.]);
  fi

  AC_MSG_CHECKING([libpng install prefix])

  if test "x$with_png" = "xyes"; then
    for i in `echo "$STANDARD_PREFIXES"`; do
      if test -f "$i/include/png.h"; then
        LIBPNG_DIR="$i"
        break;
      fi
    done
  else
    if test -f "$with_png/include/png.h"; then
      LIBPNG_DIR="$with_png"
      break;
    else
      AC_MSG_ERROR([Can't find libpng headers under $with_png directory]);
    fi
  fi

  if test "x$LIBPNG_DIR" = "x"; then
    AC_MSG_ERROR([Unable to locate libpng headers, please use --with-png=<DIR>]);
  fi

  AC_MSG_RESULT([$LIBPNG_DIR])
  LDFLAGS="$LDFLAGS -L$LIBPNG_DIR/$LIBDIR"
  CFLAGS="$CFLAGS -I$LIBPNG_DIR/include"
  LIBS="$LIBS -lpng"

  AC_CHECK_LIB([png], [png_init_io], [], [
    AC_MSG_ERROR([png_init_io() is missing, check config.log for more details])
  ])
fi
dnl }}}

case "$host_alias" in
  *cygwin*[)]
    CFLAGS="$CFLAGS -mno-cygwin -DHPDF_DLL_MAKE_CDECL"
    ;;
esac

if test "$GCC" = yes ; then
  AC_MSG_CHECKING([whether GCC supports -fexceptions])
  echo 'void somefunc() { };' > conftest.c
  cmd='$CC -fexceptions -c conftest.c'
  if eval $cmd 2>&1 | $EGREP -e $1 >/dev/null ; then
    AC_MSG_RESULT([no])
  else
    AC_MSG_RESULT([yes])
    CFLAGS="$CFLAGS -fexceptions"
  fi
  rm -f conftest.*
fi

dnl link against libm
LIBS="$LIBS -lm"
AC_SUBST(INSTALL_STRIP_FLAG)

AC_CONFIG_FILES([Makefile src/Makefile include/Makefile])
AC_OUTPUT

# vim600: expandtab sw=2 ts=2 sts=2 fdm=marker
