# !/bin/bash

host=$(echo $HOSTNAME | gawk -F. '{print $1}')
log_file=$SKYD/log/skyd_$host.log
cf_file=$SKYD/cf/skyd_$host.cf

grunts=$(cat $cf_file | grep grunts)
pid=$(cat $cf_file | grep pid | gawk -F= '{print $2}')

if [ -z $pid ]; then
	echo failed to determine pid of skyd daemon
	exit 0
fi

if [ -z "$1" ]; then
	echo "specify name of grunt:"
	echo $grunts
	exit 0
fi

grunt=$1

if [ -z "$2" ]; then
	echo specify process 1 or 2
	exit 0
fi

if [ $2 == 1 ]; then
	wmark=$(tail -15 $log_file | grep runs | grep $grunt | gawk '{print $3}')
else
	wmark=$(tail -15 $log_file | grep runs | grep $grunt | gawk '{print $6}')
fi

if [ -z "$wmark" ]; then
	echo no watermark found
	exit 0
fi

wmark="skyd_"$wmark"_done"
echo $wmark
touch $SKYD/report_$host/$wmark
kill -10 $pid

exit 0
