#!/bin/sh

### BEGIN INIT INFO
# Provides:          cloudkeeper-cron
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Enable periodic run of cloudkeeper
# Description:       This shell script enables the automatic use of cloudkeeper
### END INIT INFO

lockfile="/var/lock/cloudkeeper-cron"
retval=0
name=`basename $0`

run_dir="/var/run/cloudkeeper"
lock_dir="/var/lock/cloudkeeper"
user="cloudkeeper"

setup_dirs() {
    mkdir -p "$run_dir" "$lock_dir"
    chown "$user":"$user" "$run_dir" "$lock_dir"
}

start() {
    setup_dirs
    touch $lockfile
    echo "Enabling periodic $name"
    retval=0
}

stop() {
    rm -f $lockfile
    echo "Disabling periodic $name"
    retval=0
}

restart() {
    stop
    start
}

case "$1" in
    start)
    start
    ;;
    stop)
    stop
    ;;
    restart|force-reload)
    restart
    ;;
    reload)
    ;;
    condrestart)
    [ -f "$lockfile" ] && restart
    ;;
    status)
    if [ -f "$lockfile" ] ; then
        echo "Periodic $name is enabled"
        retval=0
    else
        echo "Periodic $name is disabled"
        retval=3
    fi
    ;;
    *)
    echo "Usage: $0 {start|stop|status|restart|reload|force-reload|condrestart}"
    retval=1
esac

exit $retval
