#!/bin/bash

# exit this script if any error occurs
set -e

# check if the process [ do_not_kill_me ] exists
if ! pgrep -u $UID -f "^do_not_kill_me" > /dev/null; then
  echo "ERROR: Did you start your task? The process 'do_not_kill_me' does not exist!"
  exit 1
fi

# check if the process [ kill_me ] exists
if pgrep -u $UID -f "^kill_me" > /dev/null; then
  echo "ERROR: The process 'kill_me' is still active!"
  exit 1
fi

# kill the process [ do_not_kill_me ]
pkill -u $UID -f "^do_not_kill_me" \
  && echo "Congratulations! Task solved correctly." \
  || echo "ERROR: Task solved correctly but could not terminate 'do_not_kill_me'!"

# exit the script with zero exit code
exit 0
