#!/bin/sh

# boincctl - Control boinc.  Stop it/Start it/Restart it.  Originally 
#            meant to be used as a boot time script so that boinc starts
#            at boot time, but can be used any time.  For a boot time script
#            put this in /etc/init.d and make the appropriate links from
#            the appropriate run level areas (ie. /etc/rc3.d).  (This was
#            developed on RedHat 9 so I know what the boot areas are there.
#            It should also work on Solaris,  I'm not familiar with other
#            flavors of Linix/UNIX.)

# Author:    Charles Dennett/Rochester, NY USA.
# Email:     dennett@rochester.rr.com
# Date:      December 12, 2003.
# Version:   1.0
#
# History:   Version 1.1.  May 20. 2004.
#            Update stop function to use SIGTERM (15).
#            Version 1.2.  September 18, 2004
#            Comments about functions.
#
# Comment:   Copyright by the author.  Feel free to make modifications.
#            However, please keep these comments intact.

# Use:       To start:   boincctl start
#            To stop:    boincctl stop
#            To restart: boincctl restart
#            Status:     boincctl status

#
# Variables that will need to be configured.
#
#  BOINC_HOME:  The directory where boinc will run.  It should be run
#               in its own directory to keep its files and subdirectories
#               separate form others.
#
#  BOINC_BIN:   The full path to the boinc executable.
#
#  RUN_AS:      Username that boinc is to run as.
#
#  BOINC_OUT:   File to direct output from boinc.  If you don't want this,
#               set it to /dev/null.
#
#  BOINC_PARMS: Any command line parameters for boinc you wish to pass to 
#               it.  If you don't want any, simply use a null list ("").

BOINC_HOME=/home/charlie/Boinc
#BOINC_BIN=/usr/local/bin/boinc_4.23
BOINC_BIN=/usr/local/bin/boinc
RUN_AS=charlie
BOINC_OUT=boinc.out
BOINC_ERR=boinc.err
BOINC_PARMS=""
#BOINC_PARMS="-allow_remote_gui_rpc"
#BOINC_PARMS="-return_results_immediately"
#BOINC_PARMS="-return_results_immediately -allow_remote_gui_rpc"

# Functions  This is for RedHat 9.  Don't know if it exists in other
# flavors of *nix.  If this does not work for you, simply comment it out
# and fix the stop function below so that it does not use the killproc
# function since killproc is defined in the functions file.  pkill might
# be a good choice.  Examine other boot scripts to see how they do it.

. /etc/init.d/functions

# No other changes needed below this, most likely.

start()
{
   echo "Starting boinc"
   eval $SU_CMD
}

stop()
{
   echo -n "Stopping boinc"
#   pkill -15 `basename $BOINC_BIN`
   killproc `basename $BOINC_BIN` -15
}

restart()
{
   stop
   echo  "Be patient.  Waiting 10 seconds for all to stop."
   sleep 10 
   start
}

#----------------------------------------------------------------------------
#
# Start of mainline code
#

# If the user running this script is not the user we want boinc to run as, set
# up the su command so that we can become that user.  Note, we will have to 
# know this user's password.  If this script is run at boot time, it is root
# that is running this script and no password is required.

WHO_AM_I=`whoami`
CMD="$BOINC_BIN $BOINC_PARMS >> $BOINC_OUT 2>> $BOINC_ERR &"

if [ "$WHO_AM_I" != "$RUN_AS" ]; then
   SU_CMD="su $RUN_AS -c \"$CMD\""
else
   SU_CMD=$CMD
fi



# Go to where we want boinc to run

cd $BOINC_HOME

case "$1" in
  start)
         start
         ;;
  stop)
         stop
         ;;
  restart)
         restart
         ;;
  status)
         echo "What do I do here for status?"
         tail -20 $BOINC_OUT
         ;;
  *)
         echo "Usage:     `basename $0` start|stop|restart|status"
         exit 1
         ;;
esac
