#!/bin/sh -eu
# ###########################################################################
#             Thomas Dreibholz's R Simulation Scripts Collection
#                  Copyright (C) 2005-2025 Thomas Dreibholz
#
#               Author: Thomas Dreibholz, thomas.dreibholz@gmail.com
# ###########################################################################
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Contact: thomas.dreibholz@gmail.com

# ====== Check parameters ===================================================
if [ $# -lt 1 ] ; then
   echo >&2 "Usage: $0 [Simulation]"
   exit 1
fi
if [ ! -e "$1".R ] ; then
   echo "ERROR: $1.R does not exist!"
   exit
fi


# shellcheck disable=SC2009
MAKE=$(ps ax | grep "$1" | grep "make -k" | awk '{ print $1 }')
echo "make:            $MAKE"
echo "$MAKE" | xargs -r -n1 kill

# shellcheck disable=SC2009
PERFORM_RUN=$(ps ax | grep "$1" | grep perform-run | awk '{ print $1 }')
echo "perform-run:     $PERFORM_RUN"
echo "$PERFORM_RUN" | xargs -r -n1 kill

# shellcheck disable=SC2009
SSDISTRIBUTE=$(ps ax | grep "$1" | grep ssdistribute | awk '{ print $1 }')
echo "ssdistribute:    $SSDISTRIBUTE"
echo "$SSDISTRIBUTE" | xargs -r -n1 kill

# shellcheck disable=SC2009
SCRIPTINGCLIENTS=$(ps ax | grep "$1" | grep scriptingclient | awk '{ print $1 }')
echo "scriptingclient: "
echo "$SCRIPTINGCLIENTS" | xargs -r -n1 kill
