#!/bin/bash

SHERPA_VERSION=1.1.2

LHAPDF_VERSION=5.4.0

curdir=`pwd`

wget --no-check-certificate https://www.hepforge.org/archive/sherpa/Sherpa-beta.1.1.2.tar.gz

SRC_TGZ=$curdir/Sherpa-beta.1.1.2.tar.gz

#
if test "x$FC" = "x"       ; then export FC=`which g77`           ; fi
if test "x$CFLAGS" = "x"   ; then export CFLAGS="-g -O0 -fPIC "   ; fi
if test "x$CXXFLAGS" = "x" ; then export CXXFLAGS="-g -O0 -fPIC " ; fi
if test "x$FFLAGS" = "x"   ; then export FFLAGS="-g -O0 -fPIC "   ; fi

if test "x$LCG_PLATFORM" = "x" ; then
	LCG_PLATFORM=slc4_ia32_gcc34
fi
#
INSTALL_DIR=$curdir/${SHERPA_VERSION}-lhapdf-${LHAPDF_VERSION}/$LCG_PLATFORM

BUILD_DIR=$curdir/build-${SHERPA_VERSION}-lhapdf-${LHAPDF_VERSION}

#
CERN_EXTERNAL=/afs/cern.ch/sw/lcg/external

CLHEP_VERSION=2.0.3.3
HEPMC_VERSION=2.04.00
ROOT_VERSION=5.19.04

CLHEP_INSTALL_DIR=${CERN_EXTERNAL}/clhep/${CLHEP_VERSION}/${LCG_PLATFORM}
HEPMC_INSTALL_DIR=${CERN_EXTERNAL}/HepMC/${HEPMC_VERSION}/${LCG_PLATFORM}

if test "x$LHAPDF_VERSION" != "xnull" ; then
	if test "x$LHAPDF_INSTALL_DIR" = "x" ; then
		# Can't be compiled with gcc3 against lhapdf 5.4.0 installed on /afs/cern.ch due to
		# #include "LHAPDF/FortranWrappers.h" directive in LHAPDFfw.h:
		#
		#LHAPDF_INSTALL_DIR=${CERN_EXTERNAL}/MCGenerators/lhapdf/${LHAPDF_VERSION}/${LCG_PLATFORM}
		#
		# I used a corrected LHAPDF 5.4.0 installation:
		LHAPDF_INSTALL_DIR=/afs/.cern.ch/sw/lcg/external/MCGenerators/.work/external/MCGenerators/lhapdf/${LHAPDF_VERSION}/${LCG_PLATFORM}
	fi
fi

ROOTSYS=${CERN_EXTERNAL}/root/${ROOT_VERSION}/${LCG_PLATFORM}/root

export LD_LIBRARY_PATH=${ROOTSYS}/lib
#

mkdir -p $BUILD_DIR ; rm -fr $BUILD_DIR/*

cd $BUILD_DIR    && \
tar zxf $SRC_TGZ && \
cd SHERPA-MC-${SHERPA_VERSION} || exit
#

CONFIGURE_ARGS="--prefix=${INSTALL_DIR} --datadir=${INSTALL_DIR}/../share --enable-shared --enable-static --enable-hepmc2=${HEPMC_INSTALL_DIR} --enable-clhep=${CLHEP_INSTALL_DIR} --enable-root=${ROOTSYS}"
if test "x$LHAPDF_VERSION" != "xnull" ; then
	CONFIGURE_ARGS="$CONFIGURE_ARGS  --enable-lhapdf=${LHAPDF_INSTALL_DIR} "
fi

rm -f ./configure libtool 

autoreconf --install --force  && \
./configure ${CONFIGURE_ARGS} && \
make                          && \
make install
	
exit
