dnl Process this file with Autoconf to produce a configure script for Web2c.
dnl
dnl Some things are no longer configurable:
dnl - SMALL{TeX,MF,BibTeX}: too painful to maintain the separate patch
dnl   files, and, for TeX, texmfmem.h would have to be fixed to support
dnl   more than 256 fonts.
dnl - NONASCII: necessary for the character translation feature.
dnl - REGFIX: modern compilers mostly ignore register declarations, anyway.
dnl The code for these things remains (where applicable), so you can
dnl get these features if you are willing to hack the sources. If not,
dnl it'll take a good argument to convince me to invest the time to make
dnl them configurable.
dnl 
dnl We don't use (for example) tex/tex.web because people who only want
dnl to build part of the distribution may not have any given program.
dnl Even main.c isn't guaranteed, but then nothing is, really ...
AC_INIT(web2c/main.c)

WEB2CVERSION=7.3.1
AC_SUBST(WEB2CVERSION)
AC_DEFINE_UNQUOTED(WEB2CVERSION," (Web2C $WEB2CVERSION)")

dnl Write output here, instead of putting a zillion -D's on the command line.
AC_CONFIG_HEADER(c-auto.h:c-auto.in)

AC_PROG_CC
AC_PROG_INSTALL
AC_PROG_LN_S
AC_PROG_MAKE_SET

AC_PROG_YACC
AC_PROG_LEX
AC_PROG_RANLIB
AC_CHECK_FUNCS(strerror)

AC_TYPE_SIGNAL

AC_DECL_YYTEXT
pb_AC_ASM_UNDERSCORE

dnl These are standard among *NIX systems, but not when cross-compiling
AC_DEFUN([CF_SUBST],
[AC_MSG_CHECKING(for $1 (symbol $2))
AC_CACHE_VAL(cf_cv_subst_$2,[
test -z "[$]$2" && $2=$3
cf_cv_subst_$2=[$]$2])
$2=${cf_cv_subst_$2}
AC_MSG_RESULT([$]$2)
AC_SUBST($2)
])dnl
CF_SUBST(loader,LD,ld)
CF_SUBST(archiver,AR,ar)
CF_SUBST(archiver options,ARFLAGS,rc)

# Needed on A/UX 3.0. I don't want to pull in other -lposix's, though.
# From: bernt@weinberg.pop.bio.aau.dk (Bernt Guldbrandtsen)
if test `(uname) 2>/dev/null` = aux; then
  AC_CHECK_LIB(posix, sigemptyset)
fi

dnl These tests prevent reliable cross-compilation.  Sigh.
dnl Some special hacks are used to make cross-compilation
dnl succeed in one special case.
AC_DEFUN(AC_UNSET_CC, [
ZZ=
if test "$cross_compiling" = yes &&
   test "x$CC" = "xdos-gcc"; then
ZZ=dos-gcc
unset CC
cross_compiling=no
fi
])

AC_DEFUN(AC_RESET_CC, [
if test "x$ZZ" = "xdos-gcc"; then
CC=dos-gcc
cross_compiling=yes
fi
])

AC_UNSET_CC
AC_C_CHAR_UNSIGNED
AC_C_BIGENDIAN
AC_CHECK_SIZEOF(long)
AC_RESET_CC

# --with and --enable options.
test "x$with_x_toolkit" = xyes && with_x=yes
test "x$with_x_toolkit" = xxt && with_x=yes
test "x$with_x_toolkit" = xathena && with_x=yes
test "x$with_x" = xno && with_x_toolkit=no

: ${with_x=no} # tell PATH_XTRA no X by default, if not specified.

sinclude(withenable.ac)
sinclude(../kpathsea/withenable.ac)
sinclude(../../libs/zlib/withenable.ac)
sinclude(../../libs/libpng/withenable.ac)
sinclude(../../libs/zlib/zlib.ac)
sinclude(../../libs/libpng/libpng.ac)

# For METAFONT, three choices.
# Build separate mfw with windowing capabilities.
# Build windowing capabilities into mf.
# Do not build windowing capabilities into mf.
: ${with_mfw=mf} # Default.
if test "x$with_mfw" = xyes; then
  # --with-mfw=yes, build separate mfw with window support.
  MFW=
  MFN=
elif test "x$with_mfw" = xmf; then
  # --with-mfw=mf, build mf with selected window support.
  MFW=
  MFN='#N#'
else
  # --with-mfw=no, do not include window support.
  MFW='#W#'
  MFN=
fi
AC_SUBST(MFN)
AC_SUBST(MFW)

# For Omega.
if test -d $srcdir/omegadir; then
  test "${with_omega+set}" = set || with_omega=yes
else
  if test "x$with_omega" = xyes; then
    AC_MSG_WARN(The Omega sources are not available.)
  fi
  with_omega=no
fi
if test "x$with_omega" = xyes; then
  OMEGA=
  otpsmakefile=otps/Makefile
else
  OMEGA='#O#'
fi
AC_SUBST(OMEGA)


# For pdfTeX.
subconfigures=pdftexdir/xpdf-0.80
if test -d $srcdir/pdftexdir; then
  # Create build dir, if necessary.
  test -d pdftexdir || mkdir pdftexdir
  test "${with_pdftex+set}" = set || with_pdftex=yes
else
  if test "x$with_pdftex" = xyes; then
    AC_MSG_WARN(The pdfTeX sources are not available.)
  fi
  with_pdftex=no
fi

# For pdfeTeX
if test -d $srcdir/pdfetexdir && test -d $srcdir/pdftexdir \
    && test -d $srcdir/etexdir; then
  test "${with_pdfetex+set}" = set || with_pdfetex=yes
else
  if test "x$with_pdfetex" = xyes; then
    AC_MSG_WARN(Not all sources of pdfeTeX are available.)
  fi
  with_pdfetex=no
fi

# Check whether C++ compiler works.  Prevent exit if it doesn't.
if test "x$with_pdftex" = xyes || test "x$with_pdfetex" = xyes; then
  # First test whether the compiler works in a subshell, and if so,
  # do it again in the main shell so we see the result.
(
  AC_PROG_CXX
) && {
  AC_PROG_CXX
} || {
  test "x$with_pdftex" = xyes \
    && AC_MSG_WARN(No C++ compiler: pdfTeX will not be compiled.)
  test "x$with_pdfetex" = xyes \
    && AC_MSG_WARN(No C++ compiler: pdfeTeX will not be compiled.)
  with_pdftex=no
  with_pdfetex=no
  true
}
fi

if test "x$with_pdftex" = xyes; then
  PTEX=
else
  PTEX='#P#'
  subconfigures=
fi
AC_SUBST(PTEX)

if test "x$with_pdfetex" = xyes; then
  PETEX=
else
  PETEX='#P#'
fi
AC_SUBST(PETEX)

# For e-TeX, three choices: no, yes, and tex.  In the latter case, we
# have to comment out the normal TeX build (TEX), as well as enable the
# e-TeX build (ETEX) and the replacement of TeX with e-TeX in compatibilty
# mode(CTEX).
if test -d $srcdir/etexdir; then
  test "${with_etex+set}" = set || with_etex=yes
else
  if test "x$with_etex" = xyes || test "x$with_etex" = xtex; then
    AC_MSG_WARN(The e-TeX sources are not available.)
  fi
  with_etex=no
fi
if test "x$with_etex" = xtex; then
  TEXBIN=etex
  CTEX=
  ETEX=
  TEX='#T#'
elif test "x$with_etex" = xyes; then
  TEXBIN=tex
  CTEX='#C#'
  ETEX=
  TEX=
else
  TEXBIN=tex
  CTEX='#C#'
  ETEX='#E#'
  TEX=
fi
AC_SUBST(TEXBIN)
AC_SUBST(CTEX)
AC_SUBST(ETEX)
AC_SUBST(TEX)


# --with-x* options: Distinguish no X at all, no toolkit, or toolkit.
AC_PATH_XTRA
if test "x$with_x" = xyes; then
  if test -n "$x_libraries"; then
    XLFLAG="-L$x_libraries"
    wlibs="-lX11 $wlibs $XLFLAG"
  else
    wlibs="-lX11 $wlibs"
  fi
  if test -z "$x_ext_lib"; then # allow envvar override
    AC_CHECK_LIB(Xext, XextCreateExtension,
                 x_ext_lib=-lXext, :, $wlibs $X_EXTRA_LIBS $XLFLAG)
  fi
  if test "x$with_x_toolkit" = xno; then
    wfile=x11-Xlib.c
  else
    # Effectively demote --with-x-toolkit=athena to xt since 
    # the code does not distinguish.
    x_tool_libs="-lXt $x_tool_libs"
    wfile=x11-Xt.c
  fi
  test -d window || mkdir window
  case "$srcdir" in
    /*) wfile=$srcdir/window/$wfile;;
    .)  ;;
    *)  wfile=../$srcdir/window/$wfile;;
  esac
  (cd window && (rm -f x11.c; $LN_S $wfile x11.c))
  AC_DEFINE(X11WIN)
fi
AC_SUBST(x_ext_lib)
AC_SUBST(x_tool_libs)
AC_SUBST(wlibs)

# Don't bother with pdflib unless pdfTeX is requested.
if test "$with_pdftex" = yes; then
  libpdfmakefile=pdftexdir/Makefile
fi

# Don't bother with pdfelib unless pdfeTeX is requested.
if test "$with_pdfetex" = yes; then
  libpdfemakefile=pdfetexdir/Makefile
fi

dnl Create tests subdirectory.
test -d tests || mkdir tests

dnl Configure in subdir.
AC_CONFIG_SUBDIRS($subconfigures)

dnl Generate `Makefile's, `config.status', and our header file.
AC_OUTPUT(Makefile doc/Makefile lib/Makefile man/Makefile mpware/Makefile 
          web2c/Makefile window/Makefile $libpdfmakefile $libpdfemakefile
          $otpsmakefile fmtutil.cnf:fmtutil.in)
dnl Update stamp-auto, since we just remade `c-auto.h'.
date >stamp-auto
