#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
AC_PREREQ(2.59)
AC_INIT( [Tauola C++ Interface - examples], [1.1.8], [tomasz.przedzinski@cern.ch],[], [tauolapp.web.cern.ch] )
AC_LANG_CPLUSPLUS

##############################################################################
# This must be done before we load paths from configure.paths.sh!
env_mc_tester=$MCTESTERLOCATION
env_pythia8data=$PYTHIA8DATA

hepmclocation=$HEPMCLOCATION
hepmc3location=$HEPMC3LOCATION
tauolalocation=$TAUOLALOCATION
mctesterlocation=$MCTESTERLOCATION
pythialocation=$PYTHIALOCATION
lhapdflocation=$LHAPDFLOCATION
##############################################################################
# Find tauola source code location

echo ""

AC_MSG_CHECKING([if Tauola location defined])
AC_ARG_WITH(
  [tauola],
    [AS_HELP_STRING([--with-tauola], [Path to Tauola installation (needed if examples are separated from Tauola source code)] )],
  [AC_MSG_RESULT($with_tauola)],
  [
    with_tauola=`(cd ..; pwd)`
    AC_MSG_RESULT($with_tauola (automatic))
  ]
)

### Load configure.paths.sh

if test -e "$with_tauola/configure.paths.sh"; then
  source $with_tauola/configure.paths.sh
elif test -e "$tauolalocation/configure.paths.sh"; then
  source $tauolalocation/configure.paths.sh
  with_tauola=$tauolalocation
elif test -z "$with_tauola"; then
  echo "ERROR: Cannot find Tauola location."
  echo "       Execute './configure --with-tauola=<path>' "
  exit 0
fi

##############################################################################
### Check if other locations are defined

### Prefer configure.paths.sh than environment paths

if test ! -z "$HEPMCLOCATION"; then
  hepmclocation=$HEPMCLOCATION
fi

if test ! -z "$HEPMC3LOCATION"; then
  hepmc3location=$HEPMC3LOCATION
fi

if test ! -z "$MCTESTERLOCATION"; then
  mctesterlocation=$MCTESTERLOCATION
fi

if test ! -z "$PYTHIALOCATION"; then
  pythialocation=$PYTHIALOCATION
fi

if test ! -z "$LHAPDFLOCATION"; then
  lhapdflocation=$LHAPDFLOCATION
fi

### Force all paths if AFS_PATHS was defined

if test "$AFS_PATHS" == "yes"; then
  with_hepmc=$HEPMCLOCATION
  with_hepmc3=$HEPMC3LOCATION
  with_mc_tester=$MCTESTERLOCATION
  with_pythia8=$PYTHIALOCATION
  with_lhapdf=$LHAPDFLOCATION
fi

##############################################################################
# Process rest of the command line options

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] )],,
  [
    with_mc_tester=$mctesterlocation
    if test -z "$with_mc_tester"; then
       with_mc_tester=no
    fi
  ]
)
AC_MSG_RESULT([$with_mc_tester])

AC_MSG_CHECKING([if Pythia 8 location defined])
AC_ARG_WITH(
  [pythia8],
    [AS_HELP_STRING([--with-pythia8], [Path to Pythia 8.1 or later] )],,
  [
    with_pythia8=$pythialocation
    if test -z "$with_pythia8"; then
      with_pythia8=no
    fi
  ]
)
AC_MSG_RESULT([$with_pythia8])

AC_MSG_CHECKING([if HepMC location is defined])
AC_ARG_WITH(
  [hepmc],
    [AS_HELP_STRING([--with-hepmc], [Path to HepMC 2.xx] )],,
  [with_hepmc=$hepmclocation]
)
AC_MSG_RESULT([$with_hepmc])

AC_MSG_CHECKING([if HepMC3 location is defined])
AC_ARG_WITH(
  [hepmc3],
    [AS_HELP_STRING([--with-hepmc3], [Path to HepMC 3.xx] )],,
  [with_hepmc3=$hepmc3location]
)

AC_MSG_RESULT([$with_hepmc3])
echo ""
##############################################################################
# Check for HepMC header files
CPPFLAGS="-I$with_hepmc/include $CPPFLAGS"

AC_CHECK_HEADERS([HepMC/Version.h],
  HAS_HEPMC=1,
[ HAS_HEPMC=0 &&
  echo "" &&
  echo "INFO: HepMC not found. To provide location use:" &&
  echo "      './configure --with-hepmc=<path_to_HepMC>' "
  echo "      (Consider using 'source platform/afs.paths.sh' (or .csh) )"
  echo ""
])

##############################################################################
# Check for Tauola
CPPFLAGS="-I$with_tauola/include $CPPFLAGS"

AC_CHECK_HEADER([Tauola/Tauola.h], ,
[ echo "" &&
  echo "ERROR: Compile Tauola C++ Interface first."
  echo ""
  exit 0
])

##############################################################################
# Check for ROOT

AC_CHECK_PROG(HAS_ROOT_CONFIG,root-config,[yes],[no])

if test "$HAS_ROOT_CONFIG" == "no"; then
	HAS_ROOT=0
	echo ""
	echo "INFO: root-config not found. ROOT is not installed or path"
        echo "      to ROOT bin folder not provied via \$PATH variable. Use:"
        echo "      export PATH=\$PATH:<path_to_root>/bin"
        echo "      export LD_LIBRARY_PATH=\$LD_LIBRARY_PATH:\`root-config --libdir\`"
        echo "     (consider using 'source platform/afs.paths.sh' (or .csh) )"
	echo ""
else
	AC_CHECK_PROG(HAS_ROOT_EXE,root,[yes],[no])
	if test "$HAS_ROOT_EXE" == "no"; then
		HAS_ROOT=0;
		echo ""
		echo "INFO: root-config exists but root not found."
	else
		ROOTLIB=`root-config --glibs`
		ROOTINC=`root-config --cflags`
		CPPFLAGS="$ROOTLIB $ROOTINC $CPPFLAGS"
		AC_CHECK_HEADER([TROOT.h],
		  HAS_ROOT=1,
		[ HAS_ROOT=0 &&
		  echo "" &&
		  echo "INFO: ROOT header file cannot be compiled."
		])
	fi
	if test $HAS_ROOT = 0; then
		echo "      This indicates faulty or unusual ROOT installation."
		echo "      Verify that location of both 'root' and 'root-config' "
		echo "      is provided via \$PATH."
		echo "      MC-Tester will not be used."
		echo ""
	fi
fi

##############################################################################
# Check for MC-Tester
CPPFLAGS="-I$with_mc_tester/include $CPPFLAGS"

if test $HAS_ROOT = 1; then
	AC_CHECK_HEADER([Generate.h],
	  HAS_MC_TESTER=1,
	[ HAS_MC_TESTER=0 &&
	  echo "" &&
	  echo "INFO: MC-Tester not found. To provide location use:" &&
	  echo "      './configure --with-mc-tester=<path_to_MC_Tester>' "
	  echo ""
	])
	if test $HAS_MC_TESTER = 1; then
		AC_CHECK_HEADER([HepMCParticle.H], ,
		[ HAS_MC_TESTER=0 &&
		  echo "" &&
		  echo "INFO: MC-Tester has been compiled without the required" &&
		  echo "      library 'libHepMCEvent'. MC-Tester will not be used." &&
		  echo ""
		])
	fi
	echo "checking for MCTESTERLOCATION: $env_mc_tester"
	if test $HAS_MC_TESTER = 1 && test -z "$env_mc_tester"; then
		echo ""
		echo "WARNING: MC-Tester location is valid, however MCTESTERLOCATION"
		echo "         variable required by MC-Tester scripts is not set."
		echo "         Without it, examples will compile and execute correctly,"
		echo "         but scripts in examples/testing/* won't work. "
		echo "         Use 'source ../configure.paths.sh' (or .csh) to set this variable."
		echo ""
	fi
else
	HAS_MC_TESTER=0
fi

##############################################################################
# Check for Pythia
CPPFLAGS="-I$with_pythia8/include $CPPFLAGS"

AC_CHECK_HEADER( [Pythia8/Pythia.h],
    [ HAS_PYTHIA=1 ],
    [ HAS_PYTHIA=0 &&
      echo "" &&
      echo "INFO: Pythia8.2XX not found. To provide location use:" &&
      echo "      './configure --with-pythia8=<path_to_Pythia8>' "
      echo ""
    ])

pythia8data=$env_pythia8data

if test $HAS_PYTHIA = 1 && test "x$env_pythia8data" == "x"; then
    echo ""
    echo "WARNING: PYTHIA8DATA not set"
    echo "         Make sure that PYTHIA8DATA is set correctly. Consider:"
    echo "         export PYTHIA8DATA=$with_pythia8/xmldoc"
    echo ""
fi

if test $HAS_HEPMC = 0;     then unset with_hepmc;     fi
if test $HAS_PYTHIA = 0;    then unset with_pythia8;   fi
if test $HAS_MC_TESTER = 0; then unset with_mc_tester; fi

##############################################################################
AC_SUBST(with_tauola)
AC_SUBST(with_hepmc)
AC_SUBST(with_hepmc3)
AC_SUBST(pythia8data)
AC_SUBST(with_pythia8)
AC_SUBST(with_mc_tester)

with_lhapdf=$lhapdflocation
AC_SUBST(with_lhapdf)

AC_CONFIG_FILES([make.inc])

### Save paths (only if configure.paths.sh exists and is writable)
if test -e "$with_tauola/configure.paths.sh" && test -w "$with_tauola/configure.paths.sh"; then
  AC_CONFIG_FILES([$with_tauola/configure.paths.sh $with_tauola/configure.paths.csh])
fi

AC_OUTPUT

##############################################################################
echo "Finished!"
echo "---------"
echo ""

if test -e "$with_tauola/configure.paths.sh"; then
  echo "Consider using 'source $with_tauola/configure.paths.sh'"
  echo "(or .csh) for setting up several paths used by examples."
fi

if test $HAS_PYTHIA = 0 || test $HAS_MC_TESTER = 0; then
	echo ""
	echo "Not all examples will be available."
fi

if test $HAS_PYTHIA = 0; then
	echo "Consider './configure --with-pythia8=<path_to_Pythia8>' "
fi

if test $HAS_MC_TESTER = 0; then
	unset with_mc_tester
	echo "Consider './configure --with-mc-tester=<path_to_MC_Tester>' "
fi

echo ""
