#!/usr/bin/env bash
# Copyright (C) 2025 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.

# Script to run all the mainNN test programs. Example usage is:
#     ./runmains --skip="01 02"
# where the first two examples, main01 and main02, will be skipped. It
# is also possible to run with multiple threads:
#     ./runmains --threads=4
# or to print the help:
#     ./runmains --help

################################################################################
# VARIABLES: Global variables not defined via command line arguments.
#     USAGE:   Text printed when the --help option is passed.
#     OPTIONS: Available user options.
#     LOGFILE: Global log file for running parallel jobs.
#     mainXXX: Custom default main commands.
################################################################################
read -d "" USAGE << "BLOCKTEXT"
Usage: ./runmains [OPTION]

The available options are defined below. Arguments to the options are
indicated in caps, i.e. SKIP.

Configuration options.
--help      : Print this message (also -h, --h, and -help).
--threads=N : Number of threads to run the examples.
--skip=SKIP : Examples to skip passed as a string, e.g. "101 102 103".
--run=MAIN  : Opposite of --skip, only run these mains, e.g. "101 102 103".
--pre=PRE   : Insert a command before running each main, e.g. a debugger call.
BLOCKTEXT
OPTIONS="-h --h -help --threads --skip --run --pre"
main132="-c main132.cmnd -o main132.hepmc"
main133="-c main133.cmnd -o main133.hepmc"
main134="-c main134.cmnd -o main134.hepmc"
main136="-c main136.cmnd -i ttbar.hdf5 -o main136.hepmc"
main144="-c main144.cmnd"
main161="-c main161.cmnd -i w+_production_lhc_0.lhe -o histout161.dat"
main162="-c main162ckkwl.cmnd"
main164="-c main164ckkwl.cmnd"
main224="-n 50 -s \"WeakSingleBoson:ffbar2gmZ=on\""
main231="-c main231.cmnd"
main364="-d /hep/evtgen/share/EvtGen/DECAY_2010.DEC"
main364+=" -p /hep/evtgen/share/EvtGen/evt.pdl"
main364+=" -x /hep/evtgen/share/Pythia8/xmldoc/ -e"

################################################################################
# FUNCTION: Print warning message.
################################################################################
function warn() {
    if ! type "tput" &> /dev/null; then echo -e $@
    else echo -e $(tput setaf 0)$(tput setab 3)WARNING: $@$(tput sgr0); fi
}

################################################################################
# FUNCTION: Compile and run a set of examples using a common lockfile.
################################################################################
function runmains() {
    for MAIN in "${MAINS[@]}"; do
	MAIN=${MAIN%.cc}
	if { set -C; 2>/dev/null > "$MAIN.log"; }; then
    	    USE=true
    	    for N in $SKIP; do
    	        if [ "$MAIN" = "main$N" ]; then USE=false; break; fi;
    	    done
    	    if [ ! -z "$RUN" ]; then USE=false; fi
    	    for N in $RUN; do
    	        if [ "$MAIN" = "main$N" ]; then USE=true; break; fi;
    	    done
    	    if [ "$USE" = false ]; then continue; fi
    	    eval ARGS=\$${MAIN}
    	    set +e # Continue on error.
	    set +C # Allow writing to the log file.
    	    make $MAIN
    	    { time $PRE ./$MAIN $ARGS ; } > $MAIN.log 2>&1
    	    set -e # Stop on error.
	else
	    sleep 0.1
	fi
    done
}

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

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

# Parse the user arguments and evaluate each as a global variable.
for VAR in "$@"; do
    if ! [[ $OPTIONS =~ (^| )${VAR%%=*}($| ) ]]; then
	warn "Ignoring invalid option \"${VAR%=*}\".";
	continue;
    fi
    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

# Build the parallel runs.
MAINS=(main[0-9][0-9][0-9].cc); THREAD=0
if [ -z $THREADS ]; then THREADS=1; fi
while ((THREAD < THREADS)); do
    ((THREAD++))
    runmains &
done
wait
