#!/bin/bash

# Source function librairy
. /etc/rc.d/init.d/functions

################################################
#This script is a sample script to allow the seti 
#client to run as a service on a linux box.
#
#Install Notes :
#
#  customize the two first variables of the script
#  make a symbolic link from 
#    You have to link /etc/rc.d/rc.<your run level>/S99seti
#    to this file to make it run.
#  start the service (or reboot :-) )
#  check it works with the status option 
#
#
#How this works ?
#
#When using the script, your seti client will always
#run in the same folder (that you can choose) ,
#this is a "clean" way to run it.

#The service can  be:
# -launched   with  "/etc/rc.d/initd/seti start"
# -stoped     with  "/etc/rc.d/initd/seti stop"
# -restarted  with  "/etc/rc.d/initd/seti restart"
# -watched    with  "/etc/rc.d/initd/seti status"
# -be logged  with  "/etc/rc.d/initd/seti log"

#As the last option (log) in quite unsual,
#it is intended to be used with the cron daemon
#to allow users to log their progress in some file
#so that can get a good idea of there current score
#and progress.
#Here are the 2 lines to type in your crontab :
# SHELL=/bin/bash
# 30 0 * * * "/etc/rc.d/initd/seti log"
#You must have write permission on all the files
#to add it to your crontab.

#Author Stephane NICOLAS
#<snicolas@iad.ift.ulaval.ca>
#This program is distributed under the terms of the GPL.
#You can get a copy of it at 
#http://www.fsf.org/copyleft/copyleft.html#translations
################################################

#This variable points to the client's executable directory
#change this value to define the location of your client.
#We do NOT need the client to be in your $PATH
SETI_CLIENT_DIR=/usr/local/bin

#This direcory contains the root of setiathome's workspace
#you can customize it.
SETI_DIR=/var/log/seti

#This file is generated by setiathome and we need it
SETI_LOG_FILE=$SETI_DIR/state.sah

#this file is lock file we create to ensure one and 
#only one client is launched
SETI_LOCK_FILE=/var/lock/seti

case "$1" in
   start)
   echo -n "Starting computing seti: "
   more ${SETI_DIR}/user_info.sah | awk -F = '$1 == "nresults" { print $2}'
   cd $SETI_DIR
   ${SETI_CLIENT_DIR}/setiathome &      
   touch $SETI_LOCK_FILE
   ;;

   stop)
   echo -n "Shutting down seti: "
   more ${SETI_DIR}/user_info.sah | awk -F = ' $1 == "nresults" { print $2 }'
   killall setiathome
   rm -f $SETI_LOCK_FILE
   echo  
   ;;

   restart)
   $0 stop
   $0 start
   ;;

   status)
   echo -n "Seti work unit completed: "
   more ${SETI_DIR}/user_info.sah | awk -F = ' $1 == "nresults" { print $2}'
   
   running=`ps -A | grep setiathome`
   #thanks to Toan for this bug fix
   if( [ "${running}" = " "  ] )
   then
     echo "Seti client is not running."
   else
     echo -n "Seti client runs since "
     echo ${running} | awk -F " " '{ print $3 }' 
     echo -n "The current work unit is completed at "
     more $SETI_LOG_FILE | grep prog | awk -F "=" '{ print $2 } ' 
   fi
   echo -n "Last result returned at ("
   more /var/log/seti/user_info.sah | grep last_result_time | awk -F "(" '{ print $2 }' 
   echo ""
   ;;
   
   log)
   /bin/date >> $SETI_DIR/log
   $0 status >> $SETI_DIR/log
   echo "*******************************************" >> $SETI_DIR/log
   ;;

   *)
    echo "Usage: seti {start|stop|restart|status}"
    exit 1
esac