#!/bin/sh
#
# mosquitto	MQTT Broker
#
# chkconfig: - 90 10
# description:	mosquitto (MQTT broker)
# processname: mosquitto
# config: /etc/mosquitto.conf
# pidfile: /var/run/mosquitto.pid
### BEGIN INIT INFO
# Provides: mosquitto
# Required-Start: $network
# Required-Stop: $network
# Default-Start:
# Default-Stop:
# Description: mosquitto (MQTT broker)
# Short-Description: start and stop mosquitto service
### END INIT INFO


# Do not load RH compatibility interface.
WITHOUT_RC_COMPAT=1

# Source function library.
. /etc/init.d/functions

# default options
M_OPT="-c /etc/mosquitto/mosquitto.conf"

# Source config..............................................................................................................................
SourceIfNotEmpty /etc/sysconfig/mosquitto

PIDFILE=/var/run/mosquitto.pid
LOCKFILE=/var/lock/subsys/mosquitto
RETVAL=0
EXPECTUSER=mosquitto

start()
{
	start_daemon --background --make-pidfile --pidfile "$PIDFILE" --lockfile "$LOCKFILE" --expect-user $EXPECTUSER -- mosquitto $M_OPT
	RETVAL=$?
	return $RETVAL
}

stop()
{
	stop_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" --expect-user $EXPECTUSER -- mosquitto
	RETVAL=$?
	return $RETVAL
}

restart()
{
	stop
	start
}

reload()
{
	msg_reloading mosquitto
	stop_daemon --pidfile "$PIDFILE" --expect-user $EXPECTUSER -HUP -- mosquitto
	RETVAL=$?
	return $RETVAL
} 

# See how we were called.
case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	reload)
		reload
		;;
	restart)
		restart
		;;
	condstop)
		if [ -e "$LOCKFILE" ]; then
			stop
		fi
		;;
	condrestart)
		if [ -e "$LOCKFILE" ]; then
			restart
		fi
		;;
	condreload)
		if [ -e "$LOCKFILE" ]; then
			reload
		fi
		;;
	status)
		status --pidfile "$PIDFILE" --expect-user $EXPECTUSER -- mosquitto
		RETVAL=$?
		;;
	*)
		msg_usage "${0##*/} {start|stop|reload|restart|condstop|condrestart|condreload|status}"
		RETVAL=1
esac

exit $RETVAL
