#! /bin/bash

help() {
  echo ""
  echo "mrevan - script for MEGAlib";
  echo "(C) by Andreas Zoglauer"
  echo "";
  echo "Options:";
  echo "    -t <threads>:   Maximum number of all threads (e.g. of all revan calls) on this machine (default: number of cores)"
  echo "    -n <nice>:      The nice level as an integer (default: 0)"
  echo "    -c <file name>: Revan configuration file name (mandatory)"
  echo "    -g <geometry file name>: Overwrites the geometry in the configuration file (optional)"
  echo "    -f <file name>: Long list of sim/evta files - MUST BE LAST"
  echo "";
  echo "Example:";
  echo "    mrevan -c Revan.cfg -m 12 -f RunTest*.sim";
  echo "";
  echo "This script runs multiple revan sessions in parallel"
  echo "";
}

if [ $# -eq 0 ]; then
    echo "Error: This script requires one or more input parameter, not $#"
    exit -1;
fi

CMD=( "$@" )

THREADS=1
NICELEVEL=0
CFG=""
FILES=""
GEOMETRY=""

# Find the default number of threads
if [[ ${OSTYPE} == darwin* ]]; then
  THREADS=`sysctl -n hw.logicalcpu_max`
elif [[ ${OSTYPE} == linux* ]]; then
  THREADS=`grep processor /proc/cpuinfo | wc -l`
fi

# Check if revan exists
if (`test -f ${MEGAlib}/bin/revan`); then
  echo " "
  echo "ERROR: The revan executable does not exist. Try to (re-)compile MEGAlib."
  echo " "
  exit 1;
fi


while [ $# -gt 0 ] ; do
  case $1 in
  -h|--help)
    help;
    exit 0;;
  -t)
    shift;
    if [ $# -eq 0 ]; then
      echo " "
      echo "Error: Options -t need an argument";
      echo "       For details see mrevan --help"
      echo " "
      exit 0;
    fi
    THREADS=$1;
    #echo "Using ${Threads} threads";
    ;;
  -c)
    shift;
    if [ $# -eq 0 ]; then
      echo " "
      echo "Error: Options -c needs an argument";
      echo "       For details see mrevan --help"
      echo " "
      exit 0;
    fi
    CFG=$1;
    #echo "Using configuration file ${CFG}";
    ;;
  -g)
    shift;
    if [ $# -eq 0 ]; then
      echo " "
      echo "Error: Options -g needs an argument";
      echo "       For details see mrevan --help"
      echo " "
      exit 0;
    fi
    GEOMETRY=$1;
    #echo "Using geometry ${GEOMETRY}";
    ;;
  -n)
    shift;
    if [ $# -eq 0 ]; then
      echo " "
      echo "Error: Option -n needs an argument";
      echo "       For details see mrevan --help"
      echo " "
      exit 0;
    fi
    NICELEVEL=$1;
    #echo "Using nice level ${Nice}";
    ;;
  -f)
    shift;
    if [ $# -eq 0 ]; then
      echo " "
      echo "Error: Option -f needs at least one argument";
      echo "       For details see mrevan --help"
      echo " "
      exit 0;
    fi
    while [ $# -ne 0 ]; do
      FILES+=" "$1;
      shift;
    done
    ;;
  *)
    ;;
  esac

  shift
done


if echo ${THREADS} | grep "^[0-9]*$" > /tmp/aux
then
  rm /tmp/aux
else
  echo " "
  echo "Error: The value behind threads must be a number";
  echo "       For details see mrevan --help"
  echo " "
  rm /tmp/aux
  exit 1;
fi


if echo ${NICE} | grep "^[0-9]*$" > /tmp/aux
then
  rm /tmp/aux
else
  echo " "
  echo "Error: The value behind nice must be a number";
  echo "       For details see mrevan --help"
  echo " "
  rm /tmp/aux
  exit 1;
fi

if [[ ! -f ${CFG} ]]; then
  echo " "
  echo "Error: You need to provide an existing revan configuration file"
  echo " "
  rm /tmp/aux
  exit 1;
fi

echo " "
echo "Launching mrevan"
echo " "
echo "Number of threads to use:  ${THREADS}"
echo "Nice level:                ${NICELEVEL}"
if [[ ${GEOMETRY} != "" ]]; then
  echo "Geometry:                ${GEOMETRY}"
fi
echo "Revan configuration file:  ${CFG}"
echo "Sim/evta files:            ${FILES}"


# Now run, revan RUN!
for SIMFILE in ${FILES}; do
  mwait -p=revan -i=${THREADS}
  echo "Launching revan for file ${SIMFILE}"
  CMD="source ${MEGALIB}/bin/source-megalib.sh; revan -c ${CFG} -a -n -f ${SIMFILE}"
  if [[ ${GEOMETRY} != "" ]]; then
    CMD+=" -g ${GEOMETRY}"
  fi
  nohup nice -n ${NICELEVEL} bash -c "${CMD}" > /dev/null &
  sleep 0.5
done


# We always wait until all revan runs have finished
echo "Waiting till all revan runs have finished..."
wait

# Create a concatenation file
NEWFILES=${FILES//.sim/.tra}
NEWFILES=${NEWFILES//.evta/.tra}
mtraconcatter ${NEWFILES}

exit 0;
