#!/usr/bin/env bash
# configure is a part of the PYTHIA event generator.
# Copyright (C) 2018 Torbjorn Sjostrand.
# PYTHIA is licenced under the GNU GPL v2 or later, see COPYING for details.
# Please respect the MCnet Guidelines, see GUIDELINES for details.
# Author: Philip Ilten, October 2014 - November 2017.
#
# This is is the configuration script which should be run prior to building
# PYTHIA. Example usage is:
#     ./configure --with-lhapdf5
# For help please use:
#     ./configure --help
# which will print a full list of configuration options.

################################################################################
# VARIABLES: Global variables not defined via command line arguments.
#     CFG_FILE: The Makefile configuration file.
#     USAGE:    Text printed when the --help option is passed.
#     OPTIONS:  List of valid options that can be passed by the user.
################################################################################
CFG_FILE="Makefile.inc"
read -d "" USAGE << "BLOCKTEXT"
Usage: ./configure [OPTIONS]

The available options are defined below. Defaults for the options are specified
by square brackets in the option description. Arguments to the options are
indicated in caps, i.e. DIR.

Configuration options.
--help          : Print this help message (also -h, --h, and -help).
--enable-debug  : Turn on debugging and disable optimization.
--enable-optdebug : Turn on debugging but allow optimization.
--enable-64bit  : Turn on 64-bit compilation.
--enable-shared : Build the shared PYTHIA library during compilation.
--lcg=PLATFORM  : Specify the LCG platform to use when the
                  --with-PACKAGE-version option, described below, is set for a
                  given optional package [x86_64-slc6-gcc48-opt]. The packages
                  are accessed via the path /afs/cern.ch/sw/lcg/releases on AFS.

Optional external packages.
--with-PACKAGE[=DIR]       : Use the external PACKAGE from the list below with
                             its top directory optionally set by DIR. If the
                             enabled PACKAGE requires other external packages,
                             these packages will also be enabled.
--with-PACKAGE-bin=DIR     : Set the path to the PACKAGE binaries.
--with-PACKAGE-lib=DIR     : Set the path to the PACKAGE libraries.
--with-PACKAGE-include=DIR : Set the path to the PACKAGE headers.
--with-PACKAGE-version=VER : When using an LCG platform, set with the option
                             --lcg above, automatically set the PACKAGE path
                             for the given version VER.
  evtgen   : Particle decays with the EvtGen decay pacakge, requires HEPMC2.
  fastjet3 : Building of jets using the FastJet package, version 3.
  	     (run fastjet-config --prefix to see which path to use.)
  hepmc2   : Export PYTHIA events to the HEPMC format, version 2.
  hepmc3   : Export PYTHIA events to the HEPMC format, version 3.
  lhapdf5  : Support the use of external PDF sets via LHAPDF, version 5.
  lhapdf6  : Support the use of external PDF sets via LHAPDF, version 6.
  powheg   : Hard process production with POWHEGBOX matrix element executables.
  promc    : Export PYTHIA events to the PROMC format.
  root     : Use ROOT trees and histograms with PYTHIA.
             Note: this option automatically invokes DIR/bin/root-config to set 
             the ROOT lib/ and include/ paths. To set your ROOT paths manually
             instead, use --with-root-lib=DIR and --with-root-include=DIR.
  gzip     : Enable reading of GZIPPED files using the libz library.
  boost    : Needed for LHAPDF6 before version LHAPDF6.2.
  python   : Interface to use PYTHIA in Python.

Installation directories.
--prefix=DIR           : Directory to install PYTHIA [$PWD].
--prefix-COMPONENT=DIR : Set the installation directory for each PYTHIA
                         COMPONENT from the following list.
  bin     : Binaries, including the configuration script [PREFIX/bin].
  lib     : Libraries, both shared and static [PREFIX/lib].
  include : Source headers [PREFIX/include/Pythia8].
  share   : Shared data, including configuration, READMEs, documentation, and
            examples [PREFIX/share/Pythia8].

Advanced options.
--arch=ARCH          : Architecture of the system. When not set, this
                       is determined automatically. Options are LINUX for *nix
                       systems and DARWIN for OS X.
--cxx=COMPILER       : Use this C++ compiler [g++].
--cxx-common='FLAGS' : Set the common C++ flags.
--cxx-shared='FLAGS' : Set the shared C++ flags.
--cxx-soname='FLAGS' : Use these C++ flags when setting shared library names.
--lib-suffix=SUF     : Shared library name suffix: [.so] for Linux and [.dylib]
                       for OS X.
BLOCKTEXT
OPTIONS="-h --h -help --help --enable-debug --enable-optdebug --enable-64bit"
OPTIONS+=" --enable-shared --lcg"
OPTIONS+=" --prefix --prefix-bin --prefix-lib --prefix-include --prefix-share"
for PKG in "evtgen" "fastjet3" "hepmc2" "hepmc3" "lhapdf5" "lhapdf6" "powheg"\
    "promc" "root" "gzip" "boost" "python"; do
    OPTIONS+=" --with-$PKG --with-$PKG-bin --with-$PKG-lib --with-$PKG-include"
    OPTIONS+=" --with-$PKG-version"; done
OPTIONS+=" --arch --cxx --cxx-common --cxx-shared --cxx-soname --lib-suffix"

################################################################################
# FUNCTION: Configure a package.
#     configure_package <package> <LCG path> <relative bin path>
#                       <relative include path> <relative library path>
#                       <bin deps> <header deps> <lib deps> <package deps>
# The following array structure stores the package configuration to the global
# variable <upper-case package name>_CFG.
#     0: Use the package.       5: Header dependencies.
#     1: Binary directory.	6: Library dependencies.
#     2: Include directory.     7: Package dependencies.
#     3: Library directory.     8: Dependency messages (empty if success).
#     4: Binary dependencies.
# Note that the global variable CXX must be defined prior to calling this
# function.
################################################################################
function configure_package() {

    # Check global package variable and set the local package variables.
    local PKG=$(echo $1 | awk '{print toupper($0)}')
    eval local PKG_CFG=\${${PKG}_CFG[0]}
    if [ "${PKG_CFG[0]}" != true ]; then PKG_CFG[0]=false; fi
    PKG_CFG[4]=$6; PKG_CFG[5]=$7; PKG_CFG[6]=$8; PKG_CFG[7]=$9; PKG_CFG[8]=""
    local LCG_DIR=$2; local PKG_BIN=$3; local PKG_INC=$4; local PKG_LIB=$5;
    local WARN="$FGwarn$BGwarn WARNING: Disabling $PKG $FGBGdef -"
    # Initialise BIN, INCLUDE, and LIB paths to user values (if specified)
    eval PKG_CFG[1]=\$${PKG}_BIN; eval PKG_CFG[2]=\$${PKG}_INCLUDE
    eval PKG_CFG[3]=\$${PKG}_LIB
    eval local PKG_DIR=\$$PKG;    eval local PKG_SET=\$${PKG}_SET
    eval local PKG_VER=\$${PKG}_VERSION
    if [ "$PKG_SET" = true ] || [ -n "${PKG_CFG[1]}" ] || \
	[ -n "${PKG_CFG[2]}" ] || [ -n "${PKG_CFG[3]}" ] || \
	[ -n "${PKG_VER}" ]; then PKG_CFG[0]=true; fi
    if [ -z "${PKG_CFG[2]}" ]; then PKG_CFG[2]="./"; fi
    if [ -z "${PKG_CFG[3]}" ]; then PKG_CFG[3]="./"; fi

    # Set package path for LCG if available and requested.
    if [ -n "$PKG_VER" ] && [ -n "$LCG_DIR" ]; then
	if [ ! -d $LCG_DIR/$PKG_VER ]; then
	    echo "$WARN version $PKG_VER not available via LCG."
	elif [ ! -d $LCG_DIR/$PKG_VER/$LCG ]; then
	    echo "$WARN platform $LCG not available via LCG."
	else
	    PKG_DIR=$LCG_DIR/$PKG_VER/$LCG;
	fi
    elif [ -n "$PKG_VER" ]; then
	echo -e "$WARN package not available via LCG.";
    fi
    
    # Set binary, include, and library paths from specified package path.
    if [ -n "$PKG_DIR" ]; then
	if [ -z "${PKG_CFG[1]}" ]; then
	    PKG_CFG[1]=$PKG_DIR/$PKG_BIN;
	fi;
	if [ "${PKG_CFG[2]}" = "./" ]; then
	    # Use root-config to set ROOT paths. (Check if exists.)
	    if [ "$PKG" = "ROOT" ]; then
		if type "${PKG_CFG[1]}/${PKG_CFG[4]}" &> /dev/null; then
		    PKG_CFG[2]=`${PKG_CFG[1]}/${PKG_CFG[4]} --incdir`
		fi
	    else 
		PKG_CFG[2]=$PKG_DIR/$PKG_INC;
	    fi;
	fi;
	if [ "${PKG_CFG[3]}" = "./" ]; then
	    # Use root-config to set ROOT paths. (Check if exists.)
	    if [ "$PKG" = "ROOT" ]; then
		if type "${PKG_CFG[1]}/${PKG_CFG[4]}" &> /dev/null; then
		    PKG_CFG[3]=`${PKG_CFG[1]}/${PKG_CFG[4]} --libdir`
		fi
	    else 
		PKG_CFG[3]=$PKG_DIR/$PKG_LIB;
	    fi;
	fi;
    fi

    # Check header and library dependencies and store warnings.
    IFS=","
    for BIN in ${PKG_CFG[4]}; do
	if [ -n "${PKG_CFG[1]}" ]; then
	    if ! type "${PKG_CFG[1]}/$BIN" &> /dev/null; then
		PKG_CFG[8]="${PKG_CFG[8]},$WARN binary $BIN missing.";
	    fi;
	elif ! type "$BIN" &> /dev/null; then
	    PKG_CFG[8]="${PKG_CFG[8]},$WARN binary $BIN missing.";
	fi;
    done
    if [ -z "${PKG_CFG[2]}" ]; then
	PKG_CFG[2]="./";
    fi;
    for INC in ${PKG_CFG[5]}; do
	local TEST=$(echo "#include \"$INC\"" | $CXX -o /dev/null -x c - \
	    -I${PKG_CFG[2]} 2>&1 | grep -e "$INC: No such file" \
	    -e "'$INC' file not found")
	if [ -n "$TEST" ]; then
	    PKG_CFG[8]="${PKG_CFG[8]},$WARN header $INC missing.";
	fi;
    done
    if [ -z "${PKG_CFG[3]}" ]; then
	PKG_CFG[1]="./";
    fi;
    for LIB in ${PKG_CFG[6]}; do
	local TEST=$(echo "int main(){}" | $CXX -o /dev/null -x c - \
	    -L${PKG_CFG[3]} -l$LIB 2>&1 | grep "\-l$LIB")
	if [ -n "$TEST" ]; then
	    PKG_CFG[8]="${PKG_CFG[8]},$WARN library $LIB missing.";
	fi;
    done
    IFS=" "

    # Export local package to global variable and update global package list.
    for IDX in ${!PKG_CFG[@]}; do
	eval ${PKG}_CFG[$IDX]=\"${PKG_CFG[$IDX]}\";
    done
    PKG_LIST+=($PKG)

    # Flag inter-package dependencies (if this package available).
    if [ "${PKG_CFG[0]}" = true ] && [ -z "${PKG_CFG[8]}" ]; then
	IFS=","; for PKG in ${PKG_CFG[7]}; do
		     eval ${PKG}_CFG[0]=true;
		 done;
	IFS=" "; fi
}

################################################################################
# System-agnostic definition of colour codes for enhanced terminal output
################################################################################

FGerr=""
BGerr=""
FGwarn=""
BGwarn=""
FGcode=""
BGcode=""
FGbold=""
FGBGdef=""
# Use tput (if available) to define colour codes in terminal-agnostic way
if type "tput" &> /dev/null; then
    # White-on-red for errors (black-on-red not enough contrast on some
    # systems and yellow-on-red not RG-colourblind safe.)
    FGerr=$(tput setaf 7)
    BGerr=$(tput setab 1)
    # Black-on-yellow for warnings
    FGwarn=$(tput setaf 0)
    BGwarn=$(tput setab 3)
    # White-on-black for code examples
    FGcode=$(tput setaf 7)
    BGcode=$(tput setab 0)
    # Bold text
    FGbold=$(tput bold)
    # Revert to standard
    FGBGdef=$(tput sgr0)
fi

################################################################################
# MAIN: The main execution of the configuration script.
################################################################################

echo
echo "Running PYTHIA configuration script. Now is `date`"

# Check if help requested.
for VAR in "$@"; do
    if [ "$VAR" = "-h" ] || [ "$VAR" = "--h" ] || [ "$VAR" = "-help" ] \
	   || [ "$VAR" = "--help" ]; then
	echo "$USAGE";
	exit;
    fi;
done

# Parse the user arguments and evaluate each as a global variable.
echo "# PYTHIA configuration file." > $CFG_FILE
echo "# Generated on" `date` "with the user supplied options:" >> $CFG_FILE
for VAR in "$@"; do
    if [[ $OPTIONS =~ (^| )${VAR%%=*}($| ) ]]; then
	echo "# $VAR" >> $CFG_FILE;
    else
	echo "WARNING: Ignoring invalid option \"${VAR%=*}\".";
	continue;
    fi;
    VAR=${VAR#--with-}; VAR=${VAR#--};
    KEY=${VAR%%=*}; VAL=${VAR#$KEY}; VAL=${VAL#*=}; KEY=${KEY//"-"/"_"};
    KEY=$(echo $KEY | awk '{print toupper($0)}');  VAL=$(eval echo $VAL)
    eval $KEY=\"$VAL\"; eval ${KEY}_SET=true; done

# Set the compilation flags and options (order matters).
if [ "$LCG_SET" != true ]; then LCG=x86_64-slc6-gcc48-opt; fi
[ "$ENABLE_DEBUG_SET"  = true ] && ENABLE_DEBUG="-g "   || ENABLE_DEBUG="-O2 "
[ "$ENABLE_OPTDEBUG_SET"  = true ] && ENABLE_DEBUG="-g -O "
[ "$ENABLE_64BIT_SET"  = true ] && ENABLE_64BIT="-m64 " || ENABLE_64BIT=""
[ "$ENABLE_SHARED_SET" = true ] && ENABLE_SHARED="true" || ENABLE_SHARED="false"
if [ -z "$ARCH" ]; then
    ARCH=$(uname | grep -i -o -e Linux -e Darwin);
fi
ARCH=$(echo $ARCH | awk '{print toupper($0)}')
if [ "$ARCH" != "LINUX" ] && [ "$ARCH" != "DARWIN" ]; then
    echo "WARNING: Unknown architecture $ARCH, set as LINUX."; ARCH="LINUX"; fi
# If CXX environment variable not defined, default to g++
if [ -z "$CXX" ]; then
    echo "WARNING: setting default C++ compiler CXX=g++ (CXX environment variable not set)"
    CXX="g++";
fi
if [ -z "$CXX_COMMON" ]; then
    CXX_COMMON="${ENABLE_DEBUG}${ENABLE_64BIT}"
    CXX_COMMON="$CXX_COMMON -pedantic -W -Wall -Wshadow -fPIC"; fi
if [ -z "$CXX_SHARED" ]; then
    if [ "$ARCH" = "LINUX" ];  then CXX_SHARED="-shared"; fi
    if [ "$ARCH" = "DARWIN" ]; then CXX_SHARED="-dynamiclib"; fi; fi
if [ -z "$CXX_SONAME" ]; then
    if [ "$ARCH" = "LINUX" ];  then CXX_SONAME="-Wl,-soname,"; fi
    if [ "$ARCH" = "DARWIN" ]; then CXX_SONAME="-Wl,-dylib_install_name,@rpath/"
    fi; fi
if [ -z "$LIB_SUFFIX" ]; then
    if [ "$ARCH" = "LINUX" ];  then LIB_SUFFIX=".so"; fi
    if [ "$ARCH" = "DARWIN" ]; then LIB_SUFFIX=".dylib"; fi; fi

# Configure the installation directories (order matters).
if [ "$PREFIX_SET" != true ]; then PREFIX=$PWD; fi
if [ -z "$PREFIX_BIN" ];      then PREFIX_BIN=$PREFIX/bin; fi
if [ -z "$PREFIX_INCLUDE" ];  then PREFIX_INCLUDE=$PREFIX/include; fi
if [ -z "$PREFIX_LIB" ];      then PREFIX_LIB=$PREFIX/lib; fi
if [ -z "$PREFIX_SHARE" ];    then PREFIX_SHARE=$PREFIX/share/Pythia8; fi

# Configure the packages (order does not matter).
configure_package "EVTGEN"\
    "/afs/cern.ch/sw/lcg/releases/MCGenerators/evtgen/1.3.0-9000e"\
    "bin" "include" "lib" "" "EvtGen/EvtGen.hh" "EvtGen" "HEPMC2"
configure_package "FASTJET3" "/afs/cern.ch/sw/lcg/releases/fastjet"\
    "bin" "include" "lib" "" "fastjet/config.h" "fastjet" ""
configure_package "HEPMC2" "/afs/cern.ch/sw/lcg/releases/HepMC"\
    "bin" "include" "lib" "" "HepMC/GenEvent.h" "HepMC" ""
configure_package "HEPMC3" "" \
    "bin" "include" "lib" "" "HepMC/GenEvent.h" "HepMC" ""
configure_package "LHAPDF5" "/afs/cern.ch/sw/lcg/releases/MCGenerators/lhapdf"\
    "bin" "include" "lib" "" "" "LHAPDF" ""
configure_package "LHAPDF6" "/afs/cern.ch/sw/lcg/releases/MCGenerators/lhapdf6"\
    "bin" "include" "lib" "" "LHAPDF/LHAPDF.h" "LHAPDF" ""
configure_package "POWHEG" "" "bin" "" "lib" "hvq" "" "" ""
configure_package "PROMC" ""\
     "bin" "" "lib" "" "src/ProMCBook.h" "promc" "GZIP"
configure_package "ROOT" "/afs/cern.ch/sw/lcg/releases/ROOT"\
		  "bin" "include" "lib" "root-config" "TROOT.h" \
		  "Core,Gpad,Graf,Hist,MathCore,Matrix,Thread,Tree" ""
configure_package "GZIP" "" "" "include" "lib" "" "zlib.h" "z" ""
configure_package "BOOST" "/afs/cern.ch/sw/lcg/releases/Boost"\
    "bin" "include" "lib" "" "boost/shared_ptr.hpp" "" ""
configure_package "PYTHON" "/afs/cern.ch/sw/lcg/external/Python"\
    "bin" "include" "lib" "python" "Python.h" "" ""

# Check package dependencies (order does not matter).
for IDX in ${!PKG_LIST[@]}; do 
    PKG=${PKG_LIST[$IDX]}
    eval PKG_USE=\${${PKG}_CFG[0]}; eval PKG_WRN=\${${PKG}_CFG[8]}
    if [ "$PKG_USE" = true ] && [ -n "$PKG_WRN" ]; then
	eval ${PKG}_CFG[0]=false; IFS=","
	echo ""
	for WRN in $PKG_WRN; do 
	    if [ -n "$WRN" ]; then 
	       echo -e $WRN; 
	    fi;
	done;
	IFS=" "; 
    fi; 
done

# Check package inter-dependencies (order does not matter).
for IDX in ${!PKG_LIST[@]}; do PKG=${PKG_LIST[$IDX]}
    eval PKG_USE=\${${PKG}_CFG[0]}; eval PKG_DPS=\${${PKG}_CFG[7]}
    if [ "$PKG_USE" = true ]; then
	IFS=","
	for DEP in $PKG_DPS; do
	    eval DEP_USE=\${${DEP}_CFG[0]}
	    if [ "$DEP_USE" != true ]; then
		eval ${PKG}_CFG[0]=false
		echo "WARNING: Disabling $PKG - requires $DEP.";
	    fi;
	done;
	IFS=" ";
    fi;
done

# Check whether to add package-dependent compilation flags (for ROOT)
eval ROOT_USE=\${ROOT_CFG[0]}
if [ "$ROOT_USE" = true ] && [ "$ROOT_SET" = true ] ; then
    # Get ROOT-specific flags from root-config --cflags
    # Get ROOT-specific libraries from root-config --glibs
    if type "${ROOT_CFG[1]}/${ROOT_CFG[4]}" &> /dev/null; then
	ROOT_FLAGS=`\${ROOT_CFG[1]}/\${ROOT_CFG[4]} --cflags`;
	ROOT_LIBS=`\${ROOT_CFG[1]}/\${ROOT_CFG[4]} --glibs`;
    fi
    # Combine with common flags but remove --pedantic option
    CXX_ROOT=`echo \${CXX_COMMON} ${ROOT_FLAGS} | sed s/"-pedantic"/""/`
fi

# Write the configuation to screen and file, update the pythia8-config script.
sed 's|^CFG_FILE=.*|CFG_FILE='"$PREFIX_SHARE"'/examples/Makefile.inc|;n' \
    bin/pythia8-config > bin/tmp
chmod a+x bin/tmp; mv bin/tmp bin/pythia8-config
echo "---------------------------------------------------------------------"
echo "|                    PYTHIA Configuration Summary                   |"
echo "---------------------------------------------------------------------"
echo "  Architecture                = "$ARCH 
echo "  C++ Compiler     CXX        = "$CXX
echo "  C++ Flags        CXX_COMMON = "$CXX_COMMON
echo "  C++ Flags (.so)  CXX_SHARED = "$CXX_SHARED
echo -en "  Further options             = "
if [ "$ENABLE_DEBUG_SET" = true ];  then echo -en "--enable-debug"; fi
if [ "$ENABLE_OPTDEBUG_SET" = true ];  then echo -en "--enable-optdebug"; fi
if [ "$ENABLE_64BIT_SET" = true ];  then echo -en "--enable-64bit"; fi
if [ "$ENABLE_SHARED_SET" = true ]; then echo -en "--enable-shared"; fi
if [ "$LCG_SET" = true ];           then echo -en "--lcg=$LCG"; fi
echo ""
cat >> $CFG_FILE << BLOCKTEXT

# Install directory prefixes.
PREFIX_BIN=$PREFIX_BIN
PREFIX_INCLUDE=$PREFIX_INCLUDE
PREFIX_LIB=$PREFIX_LIB
PREFIX_SHARE=$PREFIX_SHARE

# Compilation flags (see ./configure --help for further documentation).
ENABLE_SHARED=$ENABLE_SHARED
CXX=$CXX
CXX_COMMON=$CXX_COMMON
CXX_SHARED=$CXX_SHARED
CXX_SONAME=$CXX_SONAME
LIB_SUFFIX=$LIB_SUFFIX
BLOCKTEXT
echo ""
echo "The following optional external packages will be used:"
for IDX in ${!PKG_LIST[@]}; do
    PKG=${PKG_LIST[$IDX]}
    eval PKG_USE=\${${PKG}_CFG[0]}
    if [ "$PKG_USE" = true ]; then
	eval PKG_BIN=\${${PKG}_CFG[1]}; eval PKG_INC=\${${PKG}_CFG[2]};
	eval PKG_LIB=\${${PKG}_CFG[3]};
	# GZIP libraries are included as a compiler option, not in a directory
	if [ $PKG = "GZIP" ]; then
        echo -e "$FGbold + $PKG$FGBGdef"
	else
	    echo -e "$FGbold + $PKG$FGBGdef (in "$PKG_LIB")"
	fi
    else PKG_BIN=""; PKG_INC=""; PKG_LIB=""; fi
    cat >> $CFG_FILE << BLOCKTEXT

# $PKG configuration.
${PKG}_USE=$PKG_USE
${PKG}_BIN=$PKG_BIN
BLOCKTEXT
# ROOT: explicit list of libraries and compiler flags (e.g., ROOT uses C++11)
if [ "$PKG" = "ROOT" ]; then
    cat >> $CFG_FILE << BLOCKTEXT
${PKG}_LIBS=$ROOT_LIBS
CXX_${PKG}=$CXX_ROOT
BLOCKTEXT
fi
# All other packages, just put include/ and lib/ paths
cat >> $CFG_FILE << BLOCKTEXT
${PKG}_INCLUDE=$PKG_INC
${PKG}_LIB=$PKG_LIB
BLOCKTEXT
done
echo ""
