#!/bin/sh
#
# Starts and stops mosquitto if required


start_mosquitto()
{
    PERSISTENCY="/home/charge/persistency/DlmCMode_dlmc"

    if grep -qi 'Enabled (with local MQTT broker)' "$PERSISTENCY" 2> /dev/null
    then
        echo -n "Starting mosquitto: "
        start-stop-daemon -S -q -m -b -p  /var/run/mosquitto.pid \
            --exec /usr/sbin/mosquitto -- -c /etc/mosquitto/mosquitto.conf
        [ "$?" = "0" ] && echo "done" || echo "failed"
    fi
}


stop_mosquitto()
{
    [ -e "/var/run/mosquitto.pid" ] || return

    echo -n "Stopping mosquitto: "
    start-stop-daemon -K -q -p /var/run/mosquitto.pid
    [ "$?" = "0" ] && echo "done" || echo "failed"
}


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

exit 0


# Local variables:
# tab-width: 4
# indent-tabs-mode: nil
# End:
