#!/bin/sh 
#
# BOINC - start and stop the BOINC client daemon on Unix
#
#  Unix start/stop script to run the BOINC client as a daemon at
#  system startup, as the 'boinc' user (not root!).
#
#  This version works on Red Hat Linux, Fedora Core, Mandrake, Debian,
#  and Slackware Linux, and should work on generic Linux systems
#  provided that they have 'pidof' (most do).   
#  Metadata for chkconfig and the SUSE equivalent INIT info are included below.
#
#  Usage:  boinc { start | stop | status | restart }
#  
###
# chkconfig: 345 98 03
# description: This script starts the local BOINC client as a daemon
#         For more information about BOINC (the Berkeley Open Infrastructure
#         for Network Computing) see http://boinc.berkeley.edu
# processname: boinc
# config: /etc/sysconfig/boinc
#
### BEGIN INIT INFO
# Provides: boinc
# Required-Start: $network
# Required-Stop:  $network
# Default-Start: 3 4 5
# Default-Stop: 0 1 2 6
# Description: This script starts the local BOINC client as a daemon
#         For more information about BOINC (the Berkeley Open Infrastructure
#         for Network Computing) see http://boinc.berkeley.edu
### END INIT INFO
#
# Eric Myers <myers@vassar.edu>  - 27 July 2004
# Department of Physics and Astronomy, Vassar College, Poughkeepsie NY
# Eric Myers <myers@spy-hill.net> 
# Spy Hill Research, Poughkeepsie, New York
# @(#) $Id: boinc,v 1.8 2007/09/02 01:05:35 myers Exp $
########################################################################

##
# Defaults, which can be overridden in /etc/sysconfig/boinc  (Red Hat)
#           or /etc/default/boinc (Debian)

BOINCUSER=boinc
BOINCDIR=/home/boinc
BUILD_ARCH=i686-pc-linux-gnu
BOINCEXE=/usr/local/bin/boinc_client


# Log and error files (you should rotate these occasionally)
LOGFILE=boinc.log
ERRORLOG=error.log

# Additional BOINC options:
#   Be wary of -allow_remote_gui_rpc, as it can open your machine up
#   to the world if you don't set a password, or if you set a poor one.
#   Should be okay if you are behind a NAT firewall.  

#BOINCOPTS="-allow_remote_gui_rpc"   # opens up your machine to the world!


# Mandrake 10.1 really wants a subsys lock file ...
if [ -d /var/lock/subsys ]; then
  LOCKDIR=/var/lock/subsys
elif [ -d /var/lock ]; then
  LOCKDIR=/var/lock
fi

# su on Linux seems to need this to be set to work properly in a script
export TERM dumb


##
# Init script function library.   This stuff is Red Hat specific,
#   but if the functions are not found we create our own simple replacements.
#   (The idea for replacing the functions comes from OpenAFS.  Thanks guys!)

if [ -f /etc/rc.d/init.d/functions ] ; then
        . /etc/rc.d/init.d/functions
else
        export PATH=/sbin:/bin:/usr/sbin:/usr/bin
        function echo_success () { echo -n "	[  OK  ]  " ; }
        function echo_failure () { echo -n "	[FAILED]  " ; }
        function echo_warning () { echo -n "	[WARNING] " ; }
	function killproc() {
	     PID=$(pidof -s -x -o $$ -o $PPID -o %PPID $1) 
	     [ $PID ] && kill $PID ; }
fi


## Look for any local configuration settings which override all above

if [ -f /etc/sysconfig/boinc ]; then
  . /etc/sysconfig/boinc
elif [ -f /etc/default/boinc ]; then
  . /etc/default/boinc
fi


## Verify the working directory exists:

if [ ! -d $BOINCDIR ]; then
  echo -n "Cannot find BOINC directory $BOINCDIR "
  echo_failure
  echo 
  exit 7
fi


# Some additional places to look for the client executable
# (Should do this after init.d/functions and sysconfig/boinc, which sets PATH)

export PATH=$BOINCDIR:/usr/local/bin:$PATH


## Locate the executable, either boinc_client, boinc, 
## or boinc_M.mm_.... with highest version number
## We only do this if BOINCEXE set above isn't found and executable.

if [ ! -x $BOINCEXE ]; then
  BOINCEXE=$(/usr/bin/which boinc_client 2>/dev/null)
  if [ ! -x "$BOINCEXE" ]; then
    BOINCEXE=$(/usr/bin/which boinc 2>/dev/null)
    if [ ! -x "$BOINCEXE" ]; then
      BOINCEXE=$(/bin/ls -1 $BOINCDIR/boinc_*_$BUILD_ARCH 2>/dev/null | tail  -1 )
    fi
  fi
fi

if [ ! -x "$BOINCEXE" ]; then
  echo -n "Cannot find an executable for the BOINC client."
  echo_failure
  echo 
  exit 2
fi



## Functions: $1 is one of  start|stop|status|restart

case "$1" in
  start)
        cd $BOINCDIR

        if [ -f lockfile ] ; then
          echo -n "Another instance of BOINC is running (lockfile exists)."
          echo_failure
          echo 
          exit 4  
        fi

        if [ ! -d projects ] ; then
          echo -n "The BOINC client requires initialization."
          echo_warning
          echo 
        fi

        echo -n "Starting BOINC client as a daemon:  "
        su $BOINCUSER -c "$BOINCEXE $BOINCOPTS" >>$LOGFILE 2>>$ERRORLOG &
        sleep 1  
        PID=$(pidof -s -x -o $$ -o $PPID -o %PPID $BOINCEXE)
        if [ $PID ]; then
	  [ -d $LOCKDIR ] && touch $LOCKDIR/boinc
          echo_success
        else
          echo_failure
        fi
        echo 
        ;;

  stop)
        cd $BOINCDIR
        if [ ! -f lockfile -a ! -f $LOCKDIR/boinc ] ; then
          echo -n "BOINC is not running (no lockfiles found)."
          echo_success
        else
          echo -n "Stopping BOINC client daemon:  "    
	  killproc $BOINCEXE  && echo_success  || echo_failure 
	  # clean up in any case
	  rm -f $BOINCDIR/lockfile
	  rm -f $LOCKDIR/boinc
	fi
        echo 
        ;;

  restart)
        $0 stop
        $0 start
        ;;

  status)
        PID=$(pidof -x -o $$ -o $PPID -o %PPID boinc_client)
        if [ "$PID" == "" ]; then
          PID=$(pidof -x -o $$ -o $PPID -o %PPID $BOINCEXE)
        fi
        if [ "$PID" != "" ]; then
          echo "BOINC client is running (pid $PID)."
        else
          if [ -f $BOINCDIR/lockfile -o -f $LOCKDIR/boinc ]; then 
             echo "BOINC is stopped but lockfile(s) exist."
          else 
             echo "BOINC client is stopped."
          fi
        fi
        ;;

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

exit

#EOF#
