#!/bin/bash

# This file is part of MEGAlib.
# Copyright (C) by Andreas Zoglauer.
#
# Please see the MEGAlib software license and documentation for more informations.


# Initial sanity checks
if [[ ${MEGALIB} == "" ]]; then
  echo ""
  echo "ERROR: MEGAlib environment variable not set."
  echo ""
  exit 1
fi

if [[ ! -f ${MEGALIB}/config/AllowedGeant4Versions.txt ]]; then
  echo ""
  echo "ERROR: File not found: \${MEGALIB}/config/AllowedGeant4Versions.txt"
  echo ""
  exit 1
fi


# Allowed versions
VERSIONS=`cat ${MEGALIB}/config/AllowedGeant4Versions.txt` 
Geant4VersionMin=`echo ${VERSIONS} | awk -F" " '{ print $1 }'`
Geant4VersionMax=`echo ${VERSIONS} | awk -F" " '{ print $NF }'`

Sep=$((${#Geant4VersionMin}-1))
if [ ${Geant4VersionMin:0:${Sep}} -ge 10 ]; then 
  Geant4VersionMinString="${Geant4VersionMin:0:${Sep}}.0${Geant4VersionMin:${Sep}:1}"
else 
  Geant4VersionMinString="${Geant4VersionMin:0:${Sep}}.${Geant4VersionMin:${Sep}:1}"
fi
Sep=$((${#Geant4VersionMax}-1))
if [ ${Geant4VersionMax:0:${Sep}} -ge 10 ]; then 
  Geant4VersionMaxString="${Geant4VersionMax:0:${Sep}}.0${Geant4VersionMax:${Sep}:1}"
else
  Geant4VersionMaxString="${Geant4VersionMax:0:${Sep}}.${Geant4VersionMax:${Sep}:1}"
fi

FOLDER=${MEGALIB}
REPONAME="MEGAlib"


ShowSuccess=1;

case $1 in
-s) 
    ShowSuccess=0;;
-t) 
    echo "You need a Geant4 version from ${Geant4VersionMinString} to ${Geant4VersionMaxString} for this version of MEGAlib.";
    exit 0;;
esac


# Check for version of Geant4
GEANT4CONFIG=`type -P geant4-config`
if (`test -f ${G4INSTALL}/source/global/management/include/G4Version.hh`); then
  rv=`grep -r "\#define G4VERSION_NUMBER" ${G4INSTALL}/source/global/management/include/G4Version.hh`; 
  version=`echo $rv | awk -F" " '{ print $3 }'`;
  Geant4Version=`echo $((${version} / 10)) | awk -F"." '{ print $1 }'`
elif ([ "${GEANT4CONFIG}" != "" ]); then
  version=`geant4-config --version | sed 's|\.||g'`
  Geant4Version=`echo $((${version} / 10)) | awk -F"." '{ print $1 }'`
else
  # If we don't have Geant4, we do not do a version check, since Geant4 is not required for ${REPONAME} to run
  exit 0;
fi

#Geant4VersionString="${Geant4Version:(-2):1}.${Geant4Version:(-1):2}"
Sep=$((${#Geant4Version}-1))
if [ ${Geant4Version:0:${Sep}} -ge 10 ]; then
  Geant4VersionString="${Geant4Version:0:${Sep}}.0${Geant4Version:${Sep}:1}"
else
  Geant4VersionString="${Geant4Version:0:${Sep}}.${Geant4Version:${Sep}:1}"
fi


#echo $Geant4Version
if ([ ${Geant4Version} -ge ${Geant4VersionMin} ] && [ ${Geant4Version} -le ${Geant4VersionMax} ]);
then
    if [ $ShowSuccess -eq 0 ]; then
        echo "Found Geant4 version: ${Geant4VersionString} (minimum: ${Geant4VersionMinString}, maximum: ${Geant4VersionMaxString})"
    fi
else
    echo ""
    echo "No acceptable Geant4 version found!"
    echo "-> For this version of ${REPONAME} you require a Geant4 version ${Geant4VersionMinString} up to version ${Geant4VersionMaxString}, but not ${Geant4VersionString}!!"
    if ([ ${Geant4Version} -gt ${Geant4VersionMax} ]);
    then
        echo "You seem to use a Geant4 version which is more current than the ones tested with ${REPONAME}."
        echo "This Geant4 version might or might not work."
        echo "If you are adventurous open the file ${FOLDER}/config/AllowedGeant4Versions.txt,"
        echo "and add ${Geant4Version} to the end of the first line. Then retry configuring ${REPONAME}."
    fi
    exit 1;
fi;

exit 0
