#!/bin/sh

DESC="CCU Framework"
DAEMON="/usr/bin/python"
DAEMON_OPTS="/framework/connected_devices_framework/src/framework/pluginmanager.pyc"
PID_FILE="/var/run/pluginmanager.pid"

PID_FILE2="/var/run/webserver.pid"
DAEMON_OPTS2="/usr/bin/webserver/main.pyc"

TESTFILE=/tmp/testsoftwareEnabled

# Make sure the progam exists
[ -f $DAEMON ] || exit 0

start() {
    if [ -f $TESTFILE ];
    then
        echo "Starting test software..."
        # ifconfig eth0 192.168.1.200 netmask 255.255.255.0 up
        # ip route add default via 192.168.1.1
        iptables -P INPUT ACCEPT
        start-stop-daemon -S -q -p $PID_FILE2 -m -b --exec $DAEMON -- $DAEMON_OPTS2
        echo "testsoftware started!"
    else
        echo -n "Starting $DESC: "
        cp /framework/factoryconfig/PluginManager.json /data/config/PluginManager.json

        . /tmp/dbus_session_bus_address
        start-stop-daemon -S -q -p $PID_FILE -m -b --exec $DAEMON -- $DAEMON_OPTS
        echo "done"
    fi
}
stop() {
    echo -n "Stopping $DESC: "
    start-stop-daemon -K -q -p $PID_FILE
    echo "done"
}
restart() {
    stop
    sleep 1
    start
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart|reload)
        restart
        ;;
    *)
        echo "Usage: $0 {start|stop|restart}"
        exit 1
esac

exit $?
