dnl Process this file with autoconf to produce a configure script.
dnl ==============================================================================
dnl Instantiation:
dnl ==============================================================================
AC_INIT([TOPCOM],[1.2.0b])
AC_CONFIG_SRCDIR(.)
AC_SUBST([libTOPCOM_version_info], [1:2:0])
AC_SUBST([libCHECKREG_version_info], [1:2:0])

dnl ------------------------------------------------------------------------------
dnl Init automake.
dnl ------------------------------------------------------------------------------
AM_INIT_AUTOMAKE

dnl ------------------------------------------------------------------------------
dnl link tex files from doc into build directory, since the TeX-Clients
dnl are not working well in a multi-directory setting:
dnl ------------------------------------------------------------------------------
AC_CONFIG_LINKS([doc/TOPCOM-manual.tex:doc/TOPCOM-manual.tex
                 doc/TOPCOM_points.pdf:doc/TOPCOM_points.pdf
		 doc/TOPCOM_statistics.pdf:doc/TOPCOM_statistics.pdf
		 doc/TOPCOM_tree_graph.pdf:doc/TOPCOM_tree_graph.pdf])

dnl ==============================================================================
dnl Settings consistent for all Makefiles:
dnl ==============================================================================
dnl ------------------------------------------------------------------------------
dnl Set some compilation switches consistently:
dnl ------------------------------------------------------------------------------
AC_SUBST([AM_CPPFLAGS], ["-DGMPRATIONAL"])
AC_SUBST([AM_CXXFLAGS], ["-pthread"])
AC_SUBST([AM_CFLAGS], ["-pthread"])

dnl ------------------------------------------------------------------------------
dnl Extend user variables to incorporate local builds in external:
dnl (Currently not used)
dnl ------------------------------------------------------------------------------
dnl BUILDDIR=$ac_pwd
dnl LDFLAGS_USER=$LDFLAGS
dnl LDFLAGS="$LDFLAGS_USER -L${BUILDDIR}/external/lib"
dnl CPPFLAGS_USER="$CPPFLAGS"
dnl CPPFLAGS="$CPPFLAGS_USER -I${BUILDDIR}/external/include"

dnl ==============================================================================
dnl Checks about the build system:
dnl ==============================================================================
AX_CXX_COMPILE_STDCXX_17
AC_PROG_INSTALL

dnl ------------------------------------------------------------------------------
dnl Init libtool.
dnl ------------------------------------------------------------------------------
LT_INIT([disable-shared])
LT_LANG([C++])

dnl ==============================================================================
dnl Check for generic programs:
dnl ==============================================================================
dnl ------------------------------------------------------------------------------
dnl Check for latexmk (should be available with most LaTeX distributions):
dnl ------------------------------------------------------------------------------
AC_CHECK_PROG([HAVE_PDFLATEX], [pdflatex], [yes], [no])
AM_CONDITIONAL(USE_PDFLATEX, [test "x$HAVE_PDFLATEX" = "xyes"])

dnl ------------------------------------------------------------------------------
dnl Check for make4ht (should be available with most LaTeX distributions):
dnl ------------------------------------------------------------------------------
AC_CHECK_PROG([HAVE_HTLATEX], [htlatex], [yes], [no])
AM_CONDITIONAL(USE_HTLATEX, [test "x$HAVE_HTLATEX" = "xyes"])

dnl ==============================================================================
dnl Check for libraries:
dnl ==============================================================================
dnl ------------------------------------------------------------------------------
dnl Check for libgmp and libgmpxx with header gmpxx.h needed for the c++ API:
dnl ------------------------------------------------------------------------------
AC_CHECK_LIB([gmpxx], [__gmpz_init], \
[ \
  LIBS="-lgmpxx -lgmp $LIBS" ; \
  AC_MSG_NOTICE([system libgmp is used]) \
], \
[ \
  AC_MSG_NOTICE([libgmp not found on system - building libgmp locally.])
], \
[-lgmp])
AM_CONDITIONAL(USE_LOCAL_GMP, [test "x$ac_cv_lib_gmpxx___gmpz_init" = "xno"])

dnl ------------------------------------------------------------------------------
dnl Check for libcddgmp:
dnl (currently, a system-wide libcddgmp is not used because it is not thread-safe)
dnl ------------------------------------------------------------------------------
dnl AM_CONDITIONAL(USE_LOCAL_CDD, [test "x$ac_cv_lib_cddgmp_dd_ddinit" = "xno"])
dnl AC_CHECK_LIB([cddgmp], [dd_DDInit])
dnl if test "x$ac_cv_lib_cddgmp_dd_DDInit" = "xyes"; then
dnl   AC_MSG_NOTICE([system cddgmp is used])
dnl else
dnl   AC_MSG_NOTICE([cddgmp not found on system - building cddgmp locally ...])
dnl make -C external cdd
dnl fi
dnl ------------------------------------------------------------------------------
dnl Build libcddgmp locally anyway:
dnl ------------------------------------------------------------------------------
AC_MSG_NOTICE([system cddgmp not used (not thread-safe) - build cddlib locally.])
AM_CONDITIONAL(USE_LOCAL_CDD, [true])

dnl ------------------------------------------------------------------------------
dnl Check for libz and libbz2 in advance:
dnl (this is needed by both QSOpt_ex and soplex, resp.)
dnl ------------------------------------------------------------------------------
AC_CHECK_LIB([z], [gzopen], [], AC_MSG_NOTICE([compiling without libz (--qsoptex and --soplex cannot be used)]))
AC_CHECK_LIB([bz2], [BZ2_bzopen], [], AC_MSG_NOTICE([compiling without libbz2 (--qsoptex cannot be used)]))

dnl ------------------------------------------------------------------------------
dnl Check for QSOpt_ex:
dnl (check first whether it was enabled, then whether it is available)
dnl ------------------------------------------------------------------------------
AC_MSG_CHECKING(whether QSOpt_ex support was enabled)
AC_ARG_ENABLE([qsoptex],
  AS_HELP_STRING([--enable-qsoptex],
                 [use QSOpt_ex as an LP solver for regularity checks (requires z libraries)])
)
if test "x$enable_qsoptex" = "xyes"; then
  AC_MSG_RESULT(yes)
  if test "x$ac_cv_lib_z_gzopen" = "xno" \
     -o "x$ac_cv_lib_bz2_BZ2_bzopen" = "xno"; then
    AC_MSG_NOTICE([libz/libbz2 not found => --qsoptex not enabled]) 
  else
    AC_CHECK_LIB([qsopt_ex], [QSexact_solver], \
    [ \
      LIBS="-lqsopt_ex $LIBS" ; \
      AC_MSG_NOTICE([system QSOpt_ex is used]) \
    ], \
    [ \
      AC_MSG_NOTICE([QSOpt_ex not found on system - building QSOpt_ex locally]) \
    ], \
    [-lgmp])
    AC_DEFINE(HAVE_LIBQSOPTEX)
  fi
else
  AC_MSG_RESULT(no)
fi
AM_CONDITIONAL(USE_QSOPTEX, [test "x$enable_qsoptex" = "xyes"])
AM_CONDITIONAL(USE_LOCAL_QSO, [test "x$enable_qsoptex" = "xyes" \
                              -a "x$ac_cv_lib_z_gzopen" = "xyes" \
                              -a "x$ac_cv_lib_bz2_BZ2_bzopen" = "xyes" \
                              -a "x$ac_cv_lib_qsopt_ex_QSexact_solver" = "xno"])

dnl ------------------------------------------------------------------------------
dnl Check for soplex:
dnl (check first whether it was enabled, then whether it is available)
dnl ------------------------------------------------------------------------------
AC_MSG_CHECKING(whether soplex support was enabled)
AC_ARG_ENABLE([soplex],
  AS_HELP_STRING([--enable-soplex],
                 [use soplex as an LP solver for regularity checks (requires soplex/mpfr/boost/z libraries)])
)
if test "x$enable_soplex" = "xyes"; then
  AC_MSG_RESULT(yes)
  if test "x$ac_cv_lib_z_gzopen" = "xno"; then
    AC_MSG_NOTICE([libz not found => --soplex not enabled]) 
  else
    AC_CHECK_LIB([soplex], [main],[ \
      LIBS="-lsoplex -lmpfr $LIBS" ; \
      AC_DEFINE(HAVE_LIBSOPLEX) \
    ], \
    [ \
      AC_MSG_NOTICE([soplex not found on system => --soplex not enabled]) \
    ],
    [ \
      -lmpfr -lgmp -lz \
    ])
  fi
else
  AC_MSG_RESULT(no)
fi
AM_CONDITIONAL(USE_SOPLEX, [test "x$enable_soplex" = "xyes" \
			   -a "x$ac_cv_lib_z_gzopen" = "xyes" \
                           -a "x$ac_cv_lib_gmpxx___gmpz_init" = "xyes" \
                           -a "x$ac_cv_lib_soplex_main" = "xyes"])

dnl Check for Permlib:
dnl AC_MSG_CHECKING(whether permlib support was enabled)
dnl AC_ARG_ENABLE([permlib],
dnl   AS_HELP_STRING([--enable-permlib],
dnl                  [support permlib for symmetry checks (requires permlib-includes and boost in ${TOPCOM}/external/include)])
dnl )
dnl AM_CONDITIONAL(USE_PERMLIB, [test "x$enable_permlib" = "xyes"])
dnl if test "x$enable_permlib" = "xyes"; then
dnl   AC_DEFINE(HAVE_LIBPERMLIB)
dnl   AC_MSG_RESULT(yes)
dnl else
dnl   AC_MSG_RESULT(no)
dnl fi

dnl ------------------------------------------------------------------------------
dnl Restore user variables:
dnl ------------------------------------------------------------------------------
dnl LDFLAGS=$LDFLAGS_USER
dnl CPPFLAGS=$CPPFLAGS_USER

AC_CONFIG_FILES([ \
  external/Makefile \
  wrap-gmp-gmpxx/Makefile \
  lib-src/Makefile \
  lib-src-reg/Makefile \
  src/Makefile \
  src-reg/Makefile \
  tests/Makefile \
  examples/Makefile \
  share/Makefile \
  doc/Makefile \
  Makefile])
AC_OUTPUT
