#/bin/sh
#
# Author:  Carsten Grohmann
#
VERSION='0.1 ($Revision: 1.1 $)'
#
# License: GPL
#
# Date:    29. June 2006
#
# $Id: startboinc.sh,v 1.1 2006/06/29 17:44:00 carsten Exp $
#
# Usage:   Controled start of the boinc client
#
### BEGIN INIT INFO
# Provides:          boinc
# Required-Start:    $localfs
# Required-Stop:     $localfs
# Default-Start:     3 5
# Default-Stop:      0 1 2 6
# Short-Description: Starts the boinc client
# Description:       check state and start boinc client
### END INIT INFO

#set -x
#set -v

# directory of the boinc client
BOINC_DIR="/home/carsten/BOINC/"

BOINC_BIN=${BOINC_DIR}/boinc
BOINC_USER="carsten"

# command line options
BOINC_OPTS="-dir $BOINC_DIR -daemon"

# Should sudo be used to change the BOINC user?
USE_SUDO="no"

# nice level
BOINC_NICELEVEL="19"

PATH=/sbin:$PATH

#
# ==== No changes below this line required ====
#

SCRITPT_NAME=$(basename $0)

# reset the state of this script
. /etc/rc.status
rc_reset


case "$1" in
  start)
    echo -n "Starting boinc client "

    checkproc $BOINC_BIN

    # check if process is still running
    if [ $? -eq 0 ]; then
      echo -n "(process still running)"
      rc_status -s
    else

      # create command line
      COMMAND_LINE="setsid $BOINC_BIN $BOINC_OPTS"

      # use su or sudo to start the client with another user id
      if [ "$BOINC_USER" -a "$USER" -a "$USER" != "$BOINC_USER" ]; then
        if [ "$USE_SUDO" = "yes" ]; then
          COMMAND_LINE="sudo -u $BOINC_USER -c \"$COMMAND_LINE\""
        else
          COMMAND_LINE="su - $BOINC_USER -c \"$COMMAND_LINE\""
        fi
      fi

      # start boinc client
      eval "$COMMAND_LINE"

      rc_status -v
    fi
    ;;

  stop)
    echo -n "Shutting down boinc client "

    killproc -TERM $BOINC_BIN

    rc_status -v
    ;;

  status)
    echo -n "Checking boinc client "

    checkproc $BOINC_BIN

    rc_status -v

    # reset the state binary (to get a good return value)
    rc_reset
    ;;

  *)
    echo "$SCRIPT_NAME Version $VERSION"
    echo "Copyright (c) 2006 Carsten Grohmann"
    echo
    echo "Usage: $SCRIPT_NAME {start|stop|status|help}"
    echo " start     Start the BOINC client"
    echo " stop      Stop the BOINC client"
    echo " help      Show this text"
    echo " status    Show if a BOINC client is running"
    exit 1
    ;;
esac
rc_exit

