#! /bin/sh
### BEGIN INIT INFO
# Provides:          boss-participant-rsync
# Required-Start:    $remote_fs boss
# Required-Stop:     $null
# Default-Start:     2 3 4 5
# Default-Stop:      S 0 1 6
# Short-Description: Rsync service.
# Description:       BOSS participant rsync.
### END INIT INFO

#set -e
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="Rsync service"
NAME=$0
SCRIPTNAME=/etc/init.d/$NAME
CONFIG=/etc/boss/boss-participant-rsync.conf
PNAME=rsync_imgs
SNAME=boss-rsync-participant

. /etc/rc.status

# Shell functions sourced from /etc/rc.status:
#      rc_check         check and set local and overall rc status
#      rc_status        check and set local and overall rc status
#      rc_status -v     ditto but be verbose in local rc status
#      rc_status -v -r  ditto and clear the local rc status
#      rc_failed        set local and overall rc status to failed
#      rc_reset         clear local rc status (overall remains)
#      rc_exit          exit appropriate to overall rc status

# First reset status of this service
rc_reset

#
#       Function that starts the daemon/service.
#
d_start()
{
        if [ -f /var/run/boss/$PNAME.pid ]; then
            echo -n " already running"
        else
            startproc /usr/bin/$SNAME.py -c $CONFIG
        fi
}

#
#       Function that stops the daemon/service.
#
d_stop() {
        killproc /usr/bin/$SNAME.py \
                          || echo -n " not running"
        if [ -f /var/run/boss/$PNAME.pid ]; then
           rm /var/run/boss/$PNAME.pid
        fi
}

ACTION="$1"
case "$ACTION" in
    start)
        echo -n "Starting $DESC "
        [ -d /var/run/boss ] || mkdir /var/run/boss
        d_start
        echo "."
        ;;

    stop)
        echo -n "Stopping $DESC "
        d_stop
        echo "."
        ;;

    restart|force-reload)
        echo -n "Restarting $DESC "
        d_stop
        sleep 1
        d_start
        echo "."
        ;;
    status)
        checkproc -p /var/run/boss/$PNAME.pid /usr/bin/$SNAME.py
        rc_status -v
        ;;
    *)
        echo "Usage: $NAME {start|stop|restart|force-reload}" >&2
        exit 3
        ;;
esac

rc_exit

