#!/bin/sh
#
# chkconfig: 345 99 20
# description: fermitest-boinc
# This script starts and stops the BOINC client, by default only 
# accessing seti@home.

case "$1" in

'start')
	if [ -x /root/bin/rotate.fermitest-boinc ] 
	then
		cp -p /root/bin/rotate.fermitest-boinc /etc/cron.daily
        else
		echo "Error: /root/bin/rotate.fermitest-boinc not found"
		echo "fermitest-boinc not started correctly"
		exit 1
        fi
        if [ -x /root/bin/check.fermitest-boinc ] 
        then
		cp -p /root/bin/check.fermitest-boinc /etc/cron.hourly
        else 
		echo "Error: /root/bin/check.fermitest-boinc not found"
		echo "fermitest-boinc not started correctly"
                exit 1
        fi
        if [ ! -d /root/test ] 
	then
		echo "Error: /root/test not found"
		echo "fermitest-boinc not started correctly"
		exit 1
        fi
	cd /root/test
#
#       Check to see if the BOINC subdirectory is already there
#
        if [ ! -d ./BOINC ]
	then
#       
#       Unpack the BOINC client
#
		if [ -x /root/bin/boinc_5.2.13_i686-pc-linux-gnu.sh ] 
		then
			/root/bin/boinc_5.2.13_i686-pc-linux-gnu.sh
                else
			echo "BOINC self-extracting shell script not found"
			echo "fermitest-boinc not started correctly"
			exit 1
                fi
        fi
#
#       Verify there is a good BOINC client
#
        cd BOINC
        if [ -x ./run_client ] 
	then
		./run_client -attach_project http://setiathome.berkeley.edu bba3df27b78421379b9373ee9f76b5ca > boinc.out 2> boinc.err &
        fi
		   		
	;;
	
'stop')
        if [ -x /root/test/BOINC/boinc_cmd ] 
	then
		/root/test/BOINC/boinc_cmd --quit
        fi
	if [ -x /etc/cron.hourly/check.fermitest-boinc ] 
	then 
		rm /etc/cron.hourly/check.fermitest-boinc
	fi
	if [ -x /etc/cron.daily/rotate.fermitest-boinc ] 
        then
		rm /etc/cron.daily/rotate.fermitest-boinc
        fi
	;;

'status')
	/root/test/BOINC/boinc_cmd --get_state
	;;

*)
	echo "usage: $0 {start|stop|status}"
	;;
esac

exit 0
