dnl
dnl Copyright (C) 2000, Matias Atria
dnl 
dnl This program is free software; you can redistribute it and/or modify
dnl it under the terms of the GNU General Public License as published by
dnl the Free Software Foundation; either version 2 of the License, or
dnl (at your option) any later version.
dnl 
dnl This program is distributed in the hope that it will be useful,
dnl but WITHOUT ANY WARRANTY; without even the implied warranty of
dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
dnl GNU General Public License for more details.
dnl 
dnl You should have received a copy of the GNU General Public License
dnl along with this program; if not, write to the Free Software
dnl Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
dnl

AC_INIT(./config/VERSION)
AC_CONFIG_AUX_DIR(./config)
AC_CONFIG_HEADER(config.h)

. $srcdir/config/VERSION
AC_DEFINE_UNQUOTED(PACKAGE, "$PACKAGE",
  [Name of this package (don't change)])
AC_DEFINE_UNQUOTED(VERSION, "$VERSION",
  [Version of this package (don't change)])
AC_SUBST(PACKAGE)
AC_SUBST(MAJOR)
AC_SUBST(MINOR)
AC_SUBST(PATCH)
AC_SUBST(VERSION)

TOPDIR=`pwd`
TOPSRCDIR=`cd $srcdir && pwd`
AC_SUBST(TOPDIR)
AC_SUBST(TOPSRCDIR)

dnl Checks for programs.
AC_PROG_CC
AC_PROG_INSTALL
AC_PROG_MAKE_SET
AC_PATH_PROG(MD5SUM, md5sum, :)
AC_SUBST(MD5SUM)

ALL_LINGUAS="es"

AM_GNU_GETTEXT

dnl Checks for libraries.
AC_CHECK_LIB(m, floor)

dnl Checks for header files.
AC_PATH_XTRA
AC_HEADER_STDC
AC_CHECK_HEADERS(strings.h fcntl.h unistd.h locale.h)
dnl Checks for typedefs, structures, and compiler characteristics.
AC_C_CONST
AC_C_INLINE
AC_TYPE_SIZE_T
AC_CHECK_SIZEOF(long, 4)
AC_CHECK_SIZEOF(int, 4)
AC_CHECK_SIZEOF(short, 2)
AC_CHECK_SIZEOF(void *, 4)
AC_CHECK_BYTE_ORDER

dnl Checks for library functions.
AC_FUNC_MEMCMP
AC_FUNC_VPRINTF
AC_CHECK_FUNCS(strchr memcpy getopt getopt_long)

AC_ARG_ENABLE_BOOL(regex-specials, yes, 
  [disable regular expressions in \\special handlers])
AC_ARG_WITH_BOOL(omega, yes, 
  [disable Omega-specific file formats])
AC_ARG_WITH_BOOL(type1-fonts, no, 
  [add support for type1 fonts (requires t1lib)])
AC_ARG_WITH_BOOL(truetype-fonts, no,
  [add support for TrueType fonts (required Freetype 1.x)])
AC_ARG_ENABLE_BOOL(cheap-trick, yes,
  [disable cheap X transparency trick])
AC_ARG_ENABLE_BOOL(afm-files, yes,
  [disable support for AFM metric files])
AC_ARG_WITHX(frontends,
  [frontends to compile (only \`x11' for now)], , , [=LIST])
AC_CHECK_TEXMF

dnl#################################################################
dnl Now examine the --with and --enable options
dnl#################################################################

if test "$ac_cv_func_getopt" = no; then
  LIBOBJS="$LIBOBJS getopt.o"
fi
if test "$ac_cv_func_getopt_long" = no; then
  LIBOBJS="$LIBOBJS getopt1.o"
fi

if test -n "$LIBOBJS"; then
  SUPPORTLIB='$(TOPDIR)/support/support.a'
  SUPPORTHDR='-I$(TOPDIR)/support'
fi
AC_SUBST(SUPPORTLIB)
AC_SUBST(SUPPORTHDR)

if test "$USE_INCLUDED_LIBINTL" = yes; then
  INTLSUB=intl
fi
AC_SUBST(INTLSUB)

if test "$enable_regex_specials" = yes; then
  AC_CHECK_HEADERS(regex.h)
  AC_CHECK_FUNCS(regexec)
  if test "$ac_cv_func_regexec" = no; then
    AC_MSG_WARN([support for POSIX regular expressions disabled])
    enable_regex_specials=no
  else
    AC_DEFINE(WITH_REGEX_SPECIALS, 1, 
      [Define to use POSIX regular expressions to match TeX \\specials])
  fi
fi

if test "$texmf_is_web2c" = yes; then
  AC_PATH_PROG(MKTEXLSR, mktexlsr, :)
  MDVIMAPDIR="$texmf/mdvi"
  POST_INSTALL='echo "Updating ls-R database..."; $(MKTEXLSR)'
else
  AC_MSG_WARN([\
kpathsea does not seem to be installed properly.
Continue at your own risk])
  MDVIMAPDIR='${datadir}/mdvi'
  POST_INSTALL=:
fi
AC_SUBST(MDVIMAPDIR)
AC_SUBST(MKTEXLSR)
AC_SUBST(POST_INSTALL)

# Check front ends
. $srcdir/config/FRONTENDS
AC_MSG_CHECKING([which frontends to compile])
case "$with_frontends" in
  yes) user_frontends="$ALL_FRONTENDS" ;;
  no)  user_frontends= ;;
  *)   user_frontends=`echo $with_frontends | sed -e 's/,/ /g'` ;;
esac
FRONTENDS=
DISABLED=
for i in $user_frontends; do
  if echo "$ALL_FRONTENDS" | grep $i >/dev/null 2>&1; then
    # Don't add it more than once
    FRONTENDS="`echo $FRONTENDS | sed -e 's/[[ \t]]*'$i'[[ \t]]*//'` $i"
  else
    DISABLED="$DISABLED
  $i"
  fi
done
user_frontends=
AC_MSG_RESULT($FRONTENDS)
AC_SUBST(ALL_FRONTENDS)
AC_SUBST(FRONTENDS)
if test -n "$DISABLED"; then
  AC_MSG_WARN([\
The following unknown frontends have been disabled: $DISABLED])
fi  

if test "$enable_cheap_trick" = yes; then
  AC_DEFINE(CHEAP_X_TRICK, 1,
    [Define to simulate transparent glyphs on white backgrounds])
fi

if test "$enable_afm_files" = yes; then
  AC_DEFINE(WITH_AFM_FILES, 1,
    [Define to add support for AFM files])
fi

if test "$with_omega" = yes; then
  AC_DEFINE(WITH_OMEGA, 1,
    [Define to add support for Omega-specific file formats])
fi

if test "$with_truetype_fonts" = yes; then
  AC_CHECK_LIBOPTIONS(FreeType, ttf, 
    [#include <freetype.h>], [TT_Init_FreeType(0)])
dnl This code is for freetype 2.x
dnl
dnl We only use the freetype-config script if the user did not specify
dnl the location of the headers or the library itself.
dnl  if test -z "$with_freetype_headers" -a -z "$with_freetype_library"; then
dnl    if test "$cross_compiling" = yes; then
dnl      AC_MSG_WARN([\
dnl You must use --with-freetype-headers and --with-freetype-library 
dnl when cross-compiling. I will pretend that you said --without-freetype])
dnl      have_freetype_library=no
dnl      FREETYPE_LDFLAGS="$FREETYPE_LDFLAGS $FREETYPE_LIBS"
dnl      FREETYPE_LIBS=
dnl    else
dnl      AC_PATH_PROG(FREETYPE, freetype-config, no)
dnl      if test "$ac_cv_prog_freetype_config" = no; then
dnl        have_freetype_library=no
dnl        FREETYPE_CFLAGS=
dnl        FREETYEP_LDFLAGS=
dnl      else
dnl        FREETYPE_CFLAGS=`$FREETYPE --cflags`
dnl        FREETYPE_LDFLAGS=`$FREETYPE --libs`
dnl        AC_MSG_CHECKING([if the FreeType configuration works])
dnl        AC_CHANGE_FLAGS([$FREETYPE_CFLAGS], [$FREETYPE_LDFLAGS])
dnl        AC_TRY_COMPILE([#include <freetype/freetype.h>], 
dnl          [FT_Init_FreeType(0)],
dnl          [have_freetype_headers=yes; have_freetype_library=yes],
dnl          [have_freetype_library=no])
dnl        AC_RESTORE_FLAGS
dnl        AC_MSG_RESULT($have_freetype_library)
dnl      fi
dnl    fi
dnl  fi
fi

if test "$with_truetype_fonts" = yes; then
  case "${have_freetype_headers}-${have_freetype_library}" in
    yes-yes)
       FREETYPE_LIBS="$FRETYPE_LDFLAGS $FREETYPE_LIBS"
       AC_DEFINE(WITH_TRUETYPE_FONTS, 1, 
         [Define to add support for TrueType fonts])
       ;;
    *) AC_MSG_WARN([TrueType font support disabled])
       with_truetype_fonts=no
       ;;
  esac
fi

if test "$with_type1_fonts" = yes; then
  AC_CHECK_LIBOPTIONS(t1lib, t1, [#include <t1lib.h>], [T1_InitLib(0)])
  case "${have_t1lib_headers}-${have_t1lib_library}" in
    yes-yes)
       AC_MSG_CHECKING([if we can compile a trivial t1lib program])
       AC_CHANGE_FLAGS([$T1LIB_CFLAGS], [$T1LIB_LDFLAGS], [-lt1])
       AC_TRY_RUN([#include <stdio.h>
         #include <t1lib.h>
         int main() {
           FILE *f;
           f = fopen("conftestval", "w");
           if(!f) exit(1);
           fprintf(f, "%s\n", T1_GetLibIdent());
           fclose(f);
           exit(0);
         }], t1version=`cat conftestval`, t1version=, t1version=unknown)
       AC_RESTORE_FLAGS
       if test -z "$t1version"; then
         AC_MSG_RESULT(no)
       else
         AC_MSG_RESULT(yes)
       fi
       if test -z "$t1version"; then
         AC_MSG_WARN([Type1 font support disabled])
         with_type1_fonts=no
       else
         AC_DEFINE(WITH_TYPE1_FONTS, 1, 
           [Define to add support for Type1 fonts])
         AC_DEFINE_UNQUOTED(T1LIB_VERSION, "$t1version", 
           [Version of T1lib used to build MDVI])
       fi
       ;;
    [*]) AC_MSG_WARN([Type1 font support disabled])
       with_type1_fonts=no
       ;;
  esac
fi

AC_CHECK_LIBOPTIONS(kpathsea, kpathsea,
  [#include <stdio.h>
  #include <kpathsea/progname.h>],
  [kpse_set_program_name("conftest", "conftest")])

if test "$have_kpathsea_headers" = no -o "$have_kpathsea_library" = no; then
  AC_MSG_ERROR([I could not find the kpathsea library on this system.
This library is required to build this package. Please install it and use
the --with-kpathsea-headers and --with-kpathsea-library options to tell
configure the location of its header files and the library itself.])
  exit 1
fi

AC_MSG_CHECKING([if we can compile a trivial kpathsea program])
AC_CHANGE_FLAGS([$KPATHSEA_CFLAGS], [$KPATHSEA_LDFLAGS], [-lkpathsea])
AC_TRY_RUN([#include <stdio.h>
  #include <kpathsea/c-auto.h>
  extern char *kpathsea_version_string;
  int main() {
    FILE *f;
    f = fopen("conftestval", "w");
    if(!f) exit(1);
    fprintf(f, "%s\n", kpathsea_version_string);
    fclose(f);
    exit(0);
  }], kpseversion=`cat conftestval`, kpseversion=, kpseversion=unknown)
AC_RESTORE_FLAGS
if test -z "$kpseversion"; then
  AC_MSG_RESULT(no)
else
  AC_MSG_RESULT(yes)
fi
if test -z "$kpseversion"; then
  AC_MSG_ERROR([Could not compile a simple kpathsea program -- check your installation])
fi
AC_DEFINE_UNQUOTED(KPSE_VERSION, "$kpseversion", 
  [Version of kpathsea used to build MDVI])

dnl if X is disabled, let the makefile know
if test "$no_x" = yes; then
  X11LIB=
else
  X11LIB=-lX11
fi
AC_SUBST(X11LIB)

AC_OUTPUT(Makefile \
	lib/Makefile \
	lib/po/Makefile \
	support/Makefile \
	x11/Makefile \
	version.h \
	config.mk:config/config.mk.in \
	nls.mk:config/nls.mk.in \
	intl/Makefile)

if test -z "$FRONTENDS"; then
  FRONTENDS="NONE!"
fi

cat <<EOF

Selected options:

   Support for Type1 fonts              : $with_type1_fonts
   Support for TrueType fonts           : $with_truetype_fonts
   Support for Omega-specific formats   : $with_omega
   Support for AFM files                : $enable_afm_files
   Support for internationalization     : $USE_NLS
   Apply regular expressions to specials: $enable_regex_specials

   Frontends that will be compiled:
     $FRONTENDS
   Installation directories (prefix is \`$prefix'):
     Programs               : $bindir
     Fontmaps and encodings : $MDVIMAPDIR

To change these parameters, edit the files \`config.h' and \`config.mk'
You may also want to edit \`include/defaults.h' and the Makefiles.
Otherwise, just type \`make'.

EOF