#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_PREREQ(2.59)
AC_INIT( [Tauola C++ Interface], [1.1.8], [tomasz.przedzinski@cern.ch],[], [tauolapp.web.cern.ch] )
AC_LANG_CPLUSPLUS
AC_PREFIX_DEFAULT([`pwd`])
AC_CONFIG_SRCDIR([src/tauolaCInterfaces/Tauola.cxx])
TAUOLA_VERSION_CODE=`echo $(($(echo $PACKAGE_VERSION | sed 's/\./0000+/' | sed 's/\./00+/' | sed 's/\./\+/')))`
AC_SUBST(TAUOLA_VERSION_CODE)


##############################################################################
# Process command line options

echo ""

AC_MSG_CHECKING([if HepMC location defined])
AC_ARG_WITH(
  [hepmc],
  [AS_HELP_STRING([--with-hepmc], [Path to HepMC 2.xx (use --without-hepmc to compile without HepMC)] )],
  [AC_MSG_RESULT([$with_hepmc])],
  [AC_MSG_RESULT([no])]
)

AC_MSG_CHECKING([if HepMC3 location defined])
AC_ARG_WITH(
  [hepmc3],
  [AS_HELP_STRING([--with-hepmc3], [Path to HepMC 3.1+ (use --without-hepmc3 to compile without HepMC3)] )],
  [AC_MSG_RESULT([$with_hepmc3])],
  [AC_MSG_RESULT([no])]
)

AC_MSG_CHECKING([if MC-TESTER location defined])
AC_ARG_WITH(
  [mc-tester],
    [AS_HELP_STRING([--with-mc-tester], [Path to MC-TESTER 1.23 or later (used by examples only)] )],
  [AC_MSG_RESULT([$with_mc_tester])],
  [AC_MSG_RESULT([no])]
)

AC_MSG_CHECKING([if Pythia 8 location defined])
AC_ARG_WITH(
  [pythia8],
    [AS_HELP_STRING([--with-pythia8], [Path to Pythia 8.1 or later (used by examples only)] )],
  [AC_MSG_RESULT([$with_pythia8])],
  [AC_MSG_RESULT([no])]
)

echo ""

AC_MSG_CHECKING([if TauSpinner package is included])
AC_ARG_WITH(
  [tau-spinner],
  [AS_HELP_STRING([--with-tau-spinner], [Compile Tauola with TauSpinner library. Path to LHAPDF is required (to provide, use --with-lhapdf=<PATH>)] )],
  [AC_MSG_RESULT([$with_tau_spinner])],
  [AC_MSG_RESULT([no])]
)

AC_MSG_CHECKING([if LHAPDF location defined])
AC_ARG_WITH(
  [lhapdf],
  [AS_HELP_STRING([--with-lhapdf], [Path to LHAPDF (required for TauSpinner)] )],
  [AC_MSG_RESULT([$with_lhapdf])],
  [AC_MSG_RESULT([no])]
)

# Check for debug flags
AC_MSG_CHECKING([if debug flags are enabled])
AC_ARG_ENABLE(
  [debug],
  [AS_HELP_STRING([--enable-debug],[Enable debug flags])],
  [
    AC_MSG_RESULT([$enable_debug]) ;
    FFLAGS="-g3 -O0" ;
    CXXFLAGS="-g3 -O0"
  ],
  [
    AC_MSG_RESULT([no]) ;
    FFLAGS="-O2" ;
    CXXFLAGS="-O2"
  ]
)

echo ""

##############################################################################
# Check for programs
AC_PROG_MAKE_SET
AC_PROG_LN_S
AC_PROG_CC
AC_PROG_CXX
AC_PROG_RANLIB

# here is a trick to preffer
#   gfortran for gcc4 and higher
#   and g77 for gcc3 and lower
flist="gfortran g77 f77"
if [[[ "$(gcc -dumpversion | cut -d . -f 1)" < "4" ]]] ; then
  flist="g77 f77 gfortran"
fi

AC_PROG_F77([$flist])
AC_PROG_CPP
AC_CHECK_PROG(DIRECT_CPP,cpp,cpp,$CPP)

# Checks for typedefs, structures, and compiler characteristics.
AC_C_CONST
AC_C_INLINE
AC_CHECK_TYPES([ptrdiff_t])

# Checks for library functions.
AC_FUNC_STRFTIME
AC_CHECK_FUNCS([atexit getcwd pow sqrt], ,
[ echo "" &&
  echo "ERROR: Basic functions not found. Something's wrong with paths." &&
  exit -1
])

##############################################################################
# Check for HepMC header files

# If user specified --without-hepmc, skip hepmc check
if ! test "$with_hepmc" == "no"; then

AC_SUBST(with_hepmc)
CPPFLAGS="-I$with_hepmc/include $CPPFLAGS"

AC_CHECK_HEADERS([HepMC/Version.h], ,
[ echo "" &&
  echo "ERROR: HepMC not found. To provide location use:" &&
  echo "         --with-hepmc=/path/to/HepMC" ;
  echo "       or use";
  echo "         --without-hepmc";
  echo "       (Consider using 'source platform/afs.paths.sh' (or .csh) )"
  echo ""
  exit -1
])

AC_CHECK_HEADERS([HepMC/Units.h], ,
[ echo "" &&
  echo "WARNING: HepMC Version lower than 2.04! " &&
  echo "         The interface will be converted" &&
  echo "         To HepMC 2.03                  " &&
  echo "" &&
  ./platform/to-HepMC-2.03.sh
])

else
  with_hepmc=""
  AC_SUBST(with_hepmc)
fi

##############################################################################
# Check for HepMC3 header files

# If user specified --without-hepmc3, skip hepmc check
if ! test "$with_hepmc3" == "no"; then

AC_SUBST(with_hepmc3)
CPPFLAGS="-I$with_hepmc3/include $CPPFLAGS"

AC_CHECK_HEADERS([HepMC3/Version.h], ,
[ echo "" &&
  echo "ERROR: HepMC3 not found. To provide location use:" &&
  echo "         --with-hepmc3=/path/to/HepMC" ;
  echo "       or use";
  echo "         --without-hepmc3";
  echo "       (Consider using 'source platform/afs.paths.sh' (or .csh) )"
  echo ""
  exit -1
])

else
  with_hepmc3=""
  AC_SUBST(with_hepmc3)
fi

##############################################################################
# Check if we are compiling TauSpinner
# Check LHAPDF header files

# If user specified --with-tau-spinner
if test "$with_tau_spinner" == "yes"; then

CPPFLAGS="-I$with_lhapdf/include $CPPFLAGS"

AC_CHECK_HEADERS([LHAPDF/LHAPDF.h], ,
[ if test "x$with_lhapdf" == "x"; then
  echo ""
  echo "ERROR: LHAPDF not found. To provide location use:"
  echo "         --with-lhapdf=/path/to/LHAPDF" ;
  echo "       or compile without flag:";
  echo "         --with-tau-spinner" ;
  echo ""
  exit -1
fi
])

AC_SUBST(with_tau_spinner)
AC_SUBST(with_lhapdf)
fi

##############################################################################
with_tauola=`pwd`

if test "$prefix" != "NONE"; then
  with_tauola=$prefix
fi

pythia8data=$PYTHIA8DATA

ln -sf platform/make.inc make.inc

AC_SUBST(with_tauola)
AC_SUBST(with_hepmc)
AC_SUBST(with_hepmc3)
AC_SUBST(pythia8data)
AC_SUBST(with_pythia8)
AC_SUBST(with_mc_tester)
AC_CONFIG_FILES([platform/make.inc configure.paths.sh configure.paths.csh src/tauolaCInterfaces/Version.h ])
AC_OUTPUT

