#!/bin/sh

# Usage: terminate pattern
#    for the first .pid file in the current directory
#    that matches pattern,
#    kills the corresponding program (started e.g. with run)

test $# -gt 0 || exec sed -n 's/^#  //p' "$0"

FILE=`ls -1 $1.pid | head -1`
#test -n "$FILE" || exec echo "no match found for $1"

argument() {
  shift $1
  echo -n $1
}

HP=`cat $FILE`
H=`argument 1 $HP`
P=`argument 2 $HP`
echo "kill process $P on $H"
kill -KILL $P

rm "$FILE"
echo "removed $FILE"
