#!/bin/sh
PIDREMOTEACCESS_PLUGIN="/var/run/remoteaccess-plugin.pid"
REMOTEACCESS_PLUGIN="/bin/pcb_plugin -vv -n remoteaccess-plugin -c /usr/lib/remoteaccess/remoteaccess.odl"

start() {

    if [ -e ${PIDREMOTEACCESS_PLUGIN} ]; then
        echo "Remote access plug-in already started"
    else
        echo "Starting remote access plug-in"
        ${REMOTEACCESS_PLUGIN};
    fi
}

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

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

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

