#!/bin/bash
#
# **************************************************************************
# ********************>>>>>>>>>> SETIWATCHDOG <<<<<<<<<<********************
# **************************************************************************
#
# This starts the setiathome client, and closes it, if the watched
# process (argument 1) dies
#
# This script is part of the KSetiSaver screensaver found at:
#  --- http://ksetisaver.frozenlight.de/ ---
#
# This script is copyright 2002 by Sebastian Schildt
#
# ***************************************************************************
# *                                                                         *
# *   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 2 of the License, or     *
# *   (at your option) any later version.                                   *
# *                                                                         *
# ***************************************************************************
#
#
# This is the first bash script I've written. So I guess it may be better, 
# but hey: It works ;-) 
# If you find an error or want to contact me write to sebastian@frozenlight.de 
#
# Arguments:
# 1: Parent PID
# 2: Path to setiathome
# 3: Working directory for seti (if empty = 2)
#

#echo -e "\n\n\n"

LOGFILE=$HOME/.setiwatchdog.log

PID_PARENT=-1
PID_SETI=-1

DIR_SETI=$HOME
DIR_WORKING=$HOME


#Delete the logfile if it is over 5KB in size
find $HOME -name .setiwatchdog.log -size +5k -maxdepth 1 -exec rm -f {} \;

#echo "Logfile: $LOGFILE"
touch $LOGFILE
echo -e "\n\n\n" >> $LOGFILE

#Output function for logfile. Outputs the first argument to the log
printlog() {
	if [ -z "$1" ]
	then
		echo "`date '+%x, %X'` No parameter for printlog" >> $LOGFILE;
	else
		echo -e "`date '+%x, %X'` $1" >> $LOGFILE;
#		echo -e "`date '+%x, %X'` $1";
	fi
}


printlog "***Setiwatchdog invoked."
#printlog error here!

#Terminate if number of arguments (2 || 3) is wrong
if [ $# -ne 2 ] 
	then
	if [ $# -ne 3 ]
		then
  		printlog "`basename $0` : wrong number of arguments"
  		exit $WRONG_ARGS
	fi
fi

#Fill variables
PID_PARENT=$1
printlog "Parent PID is $PID_PARENT"
DIR_SETI=$2
if [ -n $3 ]
	then
	DIR_WORKING=$DIR_SETI
	else
	DIR_WORKING=$3
fi
printlog "Setiathome client path $DIR_SETI"
printlog "Setiathome data path $DIR_WORKING"	


if [ ! -e "$DIR_SETI/setiathome" ]
	then
	printlog "Setiathome client not found in $DIR_SETI"
	printlog "Terminating"
	exit 11
fi

#start sah
printlog "Attempting to start setiathome"
cd $DIR_WORKING
#echo -e "RC $? \n"
$DIR_SETI/setiathome -nice 19  > /dev/null 2> /dev/null & 
res=$?
#echo -e "RC $? \n"

if [ $res -ne 0 ]
	then
	printlog "Error: Setiathome start unsucessful ($res)"
	printlog "Terminating"
	exit 10
fi

#give setiathome 5 seconds to sart up and set it's pidfile
sleep 5

pidfile="$DIR_SETI/pid.sah"

{
	read PID_SETI
} < $pidfile


if [ "$PID_SETI" -eq "-1" ] 
	then
		printlog "Unable to get setiathome pid. Terminating"
		exit 10
	else
		printlog "Setiathome pid is $PID_SETI ."
fi


#Now start the monitoring process
parent_procPath="/proc/$PID_PARENT/cmdline"
{
	read cmd
} < $parent_procPath
printlog "Monitoring $parent_procPath with command $cmd"

while [ true ] 
do
	#if process doesn't exist
	if [ ! -e "/proc/$PID_PARENT" ]
		then
		printlog "Process $PID_PARENT doesn't exist"
		printlog "Killing setiathome"
		kill $PID_SETI
		printlog "Terminate\n"
		exit
	fi
	
	#check whether the parent process has been replaced by another one
	{
		read curr_cmd
	} < $parent_procPath
	
	if [ ! "$cmd" == "$curr_cmd" ]
		then
		printlog "Process $PID_SETI has been replaced"
		printlog "Killing setiathome"
		kill $PID_SETI
		printlog "Terminate\n"
		exit
	fi
	
	printlog "Parent process $PID_PARENT is up and running"
	sleep 60
done


#echo -e "\n\n\n"

exit 0
