#!/bin/bash

. /etc/rc.conf
. /etc/rc.d/functions


BOINCUSER="boinc"
BOINCDIR="/var/lib/boinc"
PID=`pidof -o %PPID /usr/bin/boinc_client`
case "$1" in
  attach)
    echo "Enter the Project URL: "
    read url
    echo "Enter your Account Key: "
    read key
    echo "Attaching to project"
    cd $BOINCDIR
    su $BOINCUSER -c "boinccmd --project_attach $url $key"
;;
  start)
    stat_busy "Starting boinc"
    if [ -z "$PID" ]; then
      su $BOINCUSER -c "nice -n 19 /usr/bin/boinc_client --daemon --dir ${BOINCDIR} --redirectio"
    else
      false
    fi
    if [ $? -gt 0 ]; then
      stat_fail
    else
      add_daemon boinc
      stat_done
    fi
    ;;
  stop)
    stat_busy "Stopping boinc"
    [ ! -z "$PID" ] && kill $PID &> /dev/null
    if [ $? -gt 0 ]; then
      stat_fail
    else
      rm_daemon boinc
      stat_done
    fi
    ;;
  restart)
    $0 stop
    sleep 2
    $0 start
    ;;
  *)
    echo "usage: $0 {attach|start|stop|restart}"
esac
exit 0

