#!/usr/bin/env bash
#
# orthologer - Pipeline for deducing orthology from fasta files.
# Copyright (C) 2005-2023 E. Zdobnov lab: F. Tegenfeldt
# <fredrik.tegenfeldt@unige.ch> based on code by E. Kriventseva
#
# This file is part of orthologer.
#
# orthologer 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.
#
# orthologer 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 orthologer.  If not, see <http://www.gnu.org/licenses/>.
#
set -u
# make sure root is not trying to run this!
uid=`id -u`
if [ $uid -eq 0 ]; then
    echo "error: cannot run as root!" 1>&2
    exit 255
fi

#--- check if environment is ok -----------------
[ "${LOADED_COMMON_PIPELINE_SH+1}" == "" ] && echo "ERROR $0 : main config has not been loaded" 1>&2 && exit 255
#------------------------------------------------

username=`whoami`
njobs=`ps -u $username | grep "submit_job" | wc -l`

if [ $njobs -eq 0 ]; then
    echo "no jobs found"
    exit 0
fi
echo "found $njobs job(s)"
declare -u answer="N"
read -p "Proceed (y/N)? " answer
if [ "$answer" != "Y" ]; then
    echo "Aborting cleanup"
    exit 0
fi
echo "killing all submit_job.sh"
killall -u $username submit_job.sh
