#!/bin/sh
PIDTOD_PLUGIN="/var/run/tod-plugin.pid"
TOD_PLUGIN="/bin/pcb_plugin -vv -n tod-plugin -c /usr/lib/tod/tod_plugin.odl"

start() {

    if [ -e ${PIDTOD_PLUGIN} ]; then
        echo "ToD plug-in already started"
    else
        echo "Starting ToD plug-in"
        ${TOD_PLUGIN};
    fi
}

stop() {
    if [ -e ${PIDTOD_PLUGIN} ]; then
        echo "Stopping ToD plug-in"
        kill $(cat ${PIDTOD_PLUGIN});
    else
        echo "ToD plug-in already stopped"
    fi
}

restart() {
    stop
    while test -e ${PIDTOD_PLUGIN}; do
        sleep 1
    done
    start
}

case $1 in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        restart
        ;;
    backup)
        if [ -e $PIDTOD_PLUGIN ] ; then 
            pcb_cli "ToD.export($2)"
        else 
            echo "Time of day plugin is not running, impossible to make a backup"
        fi
        ;;
    restore)
        mkdir -p /var/lib/tod
        touch /var/lib/tod/restore
        ;;
    *)
        echo "Usage : $0 [start|stop|restart|backup|restore]"
        ;;
esac

