#!/bin/bash
############################################################
### Script to execute multiple scripts in subdirectories ###
############################################################
set -e
trap 'echo -e "\033[31mError: an issue occurred in the script on line $LINENO! Please review the command and try again.\033[0m"' ERR

############################################################
# Modify here the parameters for the script
###########################################
# Name of the file to execute in all subdirectories
script_name="pem_workflow.sh"
#script_name="kill_pem_workflow.sh"
#script_name="clean.sh"
#script_name="modify_startfi_orbit.sh"

# Name of the template directory to skip
tempdir_name="Template"
############################################################


# Traverse all subdirectories, excluding the template
i=0
for dir in */; do
    if [ "$dir" == "$tempdir_name/" ]; then
        continue
    fi
    if [ -f "$dir/$script_name" ]; then
        echo "Executing $script_name in $dir..."
        pushd "$dir" > /dev/null
        ./$script_name
        popd > /dev/null
        ((i++))
    fi
done
echo "Everything is executed: $i executions in total!"

