#!/bin/bash
### BEGIN INIT INFO
# Provides:          mosquitto
# Required-Start:    $local_fs $remote_fs $network
# Required-Stop:     $local_fs $remote_fs $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start or stop the mosquitto service
### END INIT INFO

# Can be downloaded and installed in one go by using this command
#  sudo wget -O /tmp/download https://gist.github.com/DaveThw/4396124291bb4f92b427/download && sudo tar -zxf /tmp/download --strip-components 1 -C /etc/init.d && sudo chmod +x /etc/init.d/mosquitto && sudo
update-rc.d mosquitto defaults

# Or download and just move to /etc/init.d using this command
#  sudo wget -O /tmp/download https://gist.github.com/DaveThw/4396124291bb4f92b427/download && sudo tar -zxf /tmp/download --strip-components 1 -C /etc/init.d && sudo chmod +x /etc/init.d/mosquitto
# and then start/stop/restart the service with these commands
#  sudo service mosquitto stop
#  sudo service mosquitto start
#  sudo service mosquitto restart
# And then, if/when you want the service to autostart on bootup, use this
#  sudo update-rc.d mosquitto defaults
# Conversely, if you want to stop the service autostarting on bootup, use this
#  sudo update-rc.d -f mosquitto remove

# This script is based on my version of the Node-RED init.d script, here: https://gist.github.com/DaveThw/da394c8d04fa2bba55ac


# User that launches mosquitto (it's advised to create a new user)
#  You can do:
#   sudo useradd mosquitto
#  then change line below from  USER=pi  to  USER=mosquitto
USER=mosquitto

# Location of the mosquitto configuration file
CONFIG="/etc/mosquitto/mosquitto.conf"



# DON'T CHANGE anything below this line unless you know what you're doing!


NAME=mosquitto
DAEMON="/usr/local/sbin/mosquitto"
OPTIONS="-c ${CONFIG}"
LOG="/var/log/${NAME}.log"
PIDFILE="/var/run/${NAME}.pid"

. /lib/lsb/init-functions

start_daemon () {
        # If the log file doesn't yet exist, attempt to create it
        ! [ -e "$LOG" ] && sudo touch "$LOG"
        # Check if USER is the owner of the log file, and chown if not
        [ -e "$LOG" ] && [ -z "$(find "$LOG" -user $USER)" ] && sudo chown $USER "$LOG"

        echo >> $LOG
        date >> $LOG
        echo "Starting daemon $NAME with command '$DAEMON' and options '$OPTIONS'..." >> $LOG
        echo >> $LOG

        start-stop-daemon --start --background \
                --chuid $USER \
                --name $NAME \
                --make-pidfile --pidfile $PIDFILE \
                --startas /bin/bash -- -c "exec $DAEMON $OPTIONS >> $LOG 2>&1"
        # check if the daemon actually started, and return an appropriate result with log_end_msg
        status="0"
        pidofproc $DAEMON >/dev/null || status="$?"
        log_end_msg $status
}

stop_daemon () {
        echo >> $LOG
        date >> $LOG
        echo "Stopping daemon $NAME..." >> $LOG
        echo >> $LOG
        status="0"
        start-stop-daemon --stop --signal INT --quiet \
                --chuid $USER \
                --exec $DAEMON --pidfile $PIDFILE --retry 30 \
                --oknodo || status="$?"
        log_end_msg $status
}

case "$1" in
        start)
                log_daemon_msg "Starting daemon" "$NAME"
                start_daemon
                ;;
        stop)
                log_daemon_msg "Stopping daemon" "$NAME"
                stop_daemon
                ;;
        restart)
                log_daemon_msg "Stopping daemon" "$NAME"
                stop_daemon
                sleep 5
                log_daemon_msg "Re-Starting daemon" "$NAME"
                start_daemon
                ;;
        status)
                status_of_proc "$DAEMON" "$NAME"
                exit $?
                ;;
        *)
                echo "Usage: $0 {start|stop|restart|status}"
                exit 1
esac
exit 0


