dnl
dnl   Configure script for dvi2bitmap
dnl
dnl   Copyright 1999, Particle Physics and Astronomy Research Council.
dnl   See file LICENCE for conditions.


AC_REVISION($Revision: 1.23 $)dnl


AC_INIT(dvi2bitmap.cc)

dnl   Use a config file (the list of settings is getting too long)
AC_CONFIG_HEADER(config.h)

dnl   Say where the install-sh, config.guess and config.sub scripts are
AC_CONFIG_AUX_DIR(config)

dnl   Checks for programs.
AC_PROG_CXX

dnl   use C++ to compile test programs
AC_LANG_CPLUSPLUS

dnl   Checks for header files.  I don't actually use these defines within
dnl   the program, so there doesn't seem much point in testing for them. 
dnl   I think everything I use is in invoked in the place it's supposed to
dnl   be according to the C++ standard (with the possible exception of the
dnl   things in GIFBitmap.cc), so if anything goes wrong here, it's the 
dnl   compiler's fault, not mine.
dnl AC_HEADER_STDC
dnl AC_CHECK_HEADERS(fcntl.h unistd.h)
AC_CHECK_HEADERS(assert.h)

dnl   Check if we've got sstream.  If not, we'll assume we have the older
dnl   strstream
AC_CHECK_HEADERS(sstream)

dnl   Checks for library functions (...nor these)
dnl AC_FUNC_VPRINTF
dnl AC_CHECK_FUNCS(strerror)

dnl   What type of system are we on?
dnl   The alpha cxx compiler currently (v6.0) needs to be invoked with 
dnl   __USE_STD_IOSTREAM defined, so that it uses a standards-conforming 
dnl   streams library.  See Digital C++ v6.0 manual, section 7.1.2.
dnl   This is defined by default when the compiler is invoked with
dnl   `-std strict_ansi', but that's a little harder to arrange in here.
AC_CANONICAL_HOST
case "$host" in
  alpha*) test "$CXX" = "cxx" && AC_DEFINE(__USE_STD_IOSTREAM) ;;
esac

dnl   Do we have (standard) <cname> for C libraries, or just <name.h>?
AC_MSG_CHECKING(whether compiler has <cname> instead of just <name.h>)
AC_TRY_COMPILE([#include <cstdio>],,
  AC_MSG_RESULT(yes),
  AC_DEFINE(NO_CSTD_INCLUDE) AC_MSG_RESULT(no))

dnl   Specify support for Kpathsea library
dnl
dnl   The texmfcnf and fake-progname options
dnl   are suppressed if the support for kpathsea is turned off.  This is
dnl   because they largely depend on kpathsea searching being present.
dnl
dnl   Note, however, that font-generation is NOT, now, disabled if kpathsea
dnl   is.  This can create puzzling errors: fonts might be generated even when
dnl   they're not really missing (because the non-kpathsea search mechanism
dnl   can't find them), but the program still can't find them afterwards 
dnl   (because the font-generation method has put them where only kpathsea
dnl   expects to find them).  From experience with user feedback,
dnl   explaining this problem and running the test script in
dnl   test/test-font-gen.sh (`make test'), which explains what
dnl   DVI2BITMAP_PK_PATH should be set to, is less confusing and
dnl   inconvenient than having to rebuild dvi2bitmap with
dnl   font-generation forced on.
dnl
usekpse=false
AC_SUBST(KPATHSEAOBJ)
AC_ARG_WITH(kpathsea,
[  --without-kpathsea      Do not use kpathsea library
  --with-kpathsea[=path]  Use kpathsea, giving optional path to library],
  if test "$withval" = "yes"
  then
    AC_CHECK_HEADER(kpathsea/pathsearch.h,
    AC_CHECK_LIB(kpathsea, main,
      AC_DEFINE(ENABLE_KPATHSEA) 
      KPATHSEAOBJ="kpathsea.o" 
      LIBS="$LIBS -lkpathsea"
      usekpse=true))
  elif test "$withval" != "no"
  then
    safe_CPPFLAGS="$CPPFLAGS"
    CPPFLAGS="$CPPFLAGS -I$withval/include"
    safe_LIBS="$LIBS"
    LIBS="$LIBS -L$withval/lib -lkpathsea"
    AC_CHECK_HEADER(kpathsea/pathsearch.h,
      AC_CHECK_LIB(kpathsea, main,
        AC_DEFINE(ENABLE_KPATHSEA) 
        KPATHSEAOBJ="kpathsea.o" 
        safe_CPPFLAGS="$safe_CPPFLAGS -I$withval/include"
        safe_LIBS="$safe_LIBS -L$withval/lib -lkpathsea"
        usekpse=true))
    CPPFLAGS="$safe_CPPFLAGS"
    LIBS="$safe_LIBS"
  fi,
  AC_CHECK_HEADER(kpathsea/pathsearch.h,
    AC_CHECK_LIB(kpathsea, main,
      AC_DEFINE(ENABLE_KPATHSEA) 
      KPATHSEAOBJ="kpathsea.o" 
      LIBS="$LIBS -lkpathsea"
      usekpse=true)))

dnl   --disable-texmfcnf
if $usekpse; then
AC_ARG_ENABLE(texmfcnf,
[  --disable-texmfcnf      Disable setting of TEXMFCNF in environment],
  test "$enableval" = "yes" && T=`kpsewhich cnf texmf.cnf`
  test -n "$T" && AC_DEFINE_UNQUOTED(DEFAULT_TEXMFCNF, "`dirname $T`"),
  test $usekpse && T=`kpsewhich cnf texmf.cnf`
  test -n "$T" && AC_DEFINE_UNQUOTED(DEFAULT_TEXMFCNF, "`dirname $T`"))
fi

dnl   Do we enable automatic font-generation?
dnl   If kpathsea is enabled, then the default is yes, otherwise no.
dnl
dnl   Well, not really.  I can't make my mind up about this, but I think
dnl   it's best, in general, if font-generation is always on by default,
dnl   as long as there's enough warning of the problems about this in
dnl   the documentation.
AC_ARG_ENABLE(fontgen,
[  --enable-fontgen[=mode,res]
                          Enable automatic font gen., opt. giving mode,res],
  if test "$enableval" = "yes"
    then AC_DEFINE(ENABLE_FONT_GEN,1)
  elif test "$enableval" = "no"
    then AC_DEFINE(ENABLE_FONT_GEN,0)
  else
    [eval `echo $enableval | awk -F, '{printf "GENMODE=%s GENRES=%s",$1,$2}'`]
    AC_DEFINE(ENABLE_FONT_GEN,1)
    AC_DEFINE_UNQUOTED(DEFAULT_MFMODE,"$GENMODE")
    test -n "$GENRES" && AC_DEFINE_UNQUOTED(DEFAULT_RESOLUTION,$GENRES)
  fi,
  AC_DEFINE(ENABLE_FONT_GEN,1))

dnl   --enable-mktexpk.  See also --enable-fontgen.  If
dnl   ENABLE_FONT_GEN is not set then these values will not be used.
AC_PATH_PROG(MKTEXPK_PATH, mktexpk)
AC_ARG_ENABLE(mktexpk,
[  --enable-mktexpk[=prog] Enable mktexpk (default), giving optional program],
  if test "$enableval" = "no"
  then unset MKTEXPK_PATH
  elif test "$enableval" != "yes" && -n "$KPATHSEAOBJ"
  then MKTEXPK_PATH=$enableval
  fi
  test -n "$MKTEXPK_PATH" && AC_DEFINE_UNQUOTED(MKTEXPK, "${MKTEXPK_PATH}"),
  test -n "$MKTEXPK_PATH" && AC_DEFINE_UNQUOTED(MKTEXPK, "${MKTEXPK_PATH}"))

dnl   Use MakeTeXPK.  Don't use this if mktexpk was found
AC_PATH_PROG(MAKETEXPK_PATH, MakeTeXPK)
AC_ARG_ENABLE(maketexpk,
[  --enable-maketexpk[=prog] Enable MakeTeXPK, giving optional alternative],
  if test "$enableval" = "no"
  then unset MAKETEXPK_PATH
  elif test "$enableval" != "yes"
  then MAKETEXPK_PATH=$enableval
  fi
  test -z "$MKTEXPK_PATH" && test -n "$MAKETEXPK_PATH" && AC_DEFINE_UNQUOTED(MAKETEXPK, "${MAKETEXPK_PATH}"),
  test -z "$MKTEXPK_PATH" && test -n "$MAKETEXPK_PATH" && AC_DEFINE_UNQUOTED(MAKETEXPK, "${MAKETEXPK_PATH}"))

dnl   Do we want to use the fake_progname feature in kpathsea.cc?
if $usekpse; then
AC_PATH_PROG(XDVI_PATH, xdvi)
AC_ARG_ENABLE(fake-progname,
[  --enable-fake-progname[=prog]
                          Have dvi2bitmap lie about its name (see full docs)],
  if test "$enableval" = "yes"
  then AC_DEFINE_UNQUOTED(FAKE_PROGNAME, "${XDVI_PATH}")
  elif test "$enableval" != "no"
  then AC_DEFINE_UNQUOTED(FAKE_PROGNAME, "${enableval}")
  fi)
fi

dnl   Specify support for PNG output.  Test library for function
dnl   png_libpng_ver.  At present, PNGBitmap.cc is
dnl   written to use a palette rather than a greyscale (but see the
dnl   comments about GREYSCALE_BITMAP in that module).  If we were to
dnl   revert to using the colour model PNG_COLOR_TYPE_GRAY, then we
dnl   need the function png_set_write_user_transform_fn (which means later
dnl   than version 0.96).
dnl
dnl   Test this by default (ie, unless the option --disable-png is given).
AC_SUBST(PNGOBJ)
AC_ARG_ENABLE(png,
[  --enable-png            Enable output of PNG files],
  test "$enableval" = "yes" && AC_CHECK_HEADER(png.h,
    AC_CHECK_LIB(png, png_libpng_ver,
        AC_DEFINE(ENABLE_PNG)
        PNGOBJ="PNGBitmap.o"
        LIBS="$LIBS -lpng -lz",
      echo "png library not found or too old -- not including PNG support",
      -lz)),
  AC_CHECK_HEADER(png.h,
    AC_CHECK_LIB(png, png_libpng_ver,
        AC_DEFINE(ENABLE_PNG)
        PNGOBJ="PNGBitmap.o"
        LIBS="$LIBS -lpng -lz",
      echo "png library not found or too old -- not including PNG support",
      -lz)))

dnl   Specify support for GIF output
AC_SUBST(GIFOBJ)
AC_ARG_ENABLE(gif, 
[  --enable-gif            Enable output of GIF files],
  AC_DEFINE(ENABLE_GIF) GIFOBJ="GIFBitmap.o")

AC_OUTPUT(Makefile)