1#!/bin/sh
2
3# PROVIDE: cbsd_mq_api
4# REQUIRE: NETWORK
5# BEFORE:  DAEMON
6
7. /etc/rc.subr
8
9name="cbsd_mq_api"
10desc="CBSD cbsd API Module"
11rcvar="cbsd_mq_api_enable"
12pidfile="/var/run/${name}.pid"
13daemon_pidfile="/var/run/${name}-daemon.pid"
14logdir="/var/log/${name}"
15logfile="${logdir}/cbsd_mq_api.log"
16extra_commands="reload"
17command="/usr/local/bin/cbsd-mq-api"
18cbsd_mq_api_user=${cbsd_mq_api_user-"cbsd"}
19cbsd_mq_api_config=${cbsd_mq_api_config-"/usr/local/etc/cbsd-mq-api.json"}
20required_files="${cbsd_mq_api_config}"
21
22cbsd_mq_api_args=${cbsd_mq_api_args-"-config ${cbsd_mq_api_config}"}
23# ACL flags sample:
24#cbsd_mq_api_flags="-listen 127.0.0.1:65531 -allowlist /usr/local/etc/cbsd-mq-api.allow"
25cbsd_mq_api_flags=${cbsd_mq_api_flags="-listen 127.0.0.1:65531"}
26
27load_rc_config ${name}
28
29start_cmd="start"
30stop_cmd="stop"
31status_cmd="status"
32reload_cmd="reload"
33
34stop()
35{
36	if [ -f "${daemon_pidfile}" ]; then
37		pids=$( pgrep -F ${daemon_pidfile} 2>&1 )
38		_err=$?
39		[ ${_err} -eq  0 ] && kill -9 ${pids} && /bin/rm -f ${daemon_pidfile}
40	fi
41	if [ -f "${pidfile}" ]; then
42		pids=$( pgrep -F ${pidfile} 2>&1 )
43		_err=$?
44		[ ${_err} -eq  0 ] && kill -9 ${pids} && /bin/rm -f ${pidfile}
45	fi
46}
47
48start()
49{
50	[ ! -d ${logdir} ] && mkdir -p ${logdir}
51	touch ${logfile}
52	chown ${cbsd_mq_api_user} ${logdir} ${logfile}
53	/usr/sbin/daemon -u ${cbsd_mq_api_user} -f -R5 -p ${pidfile} -P ${daemon_pidfile} -o ${logfile} ${command} ${cbsd_mq_api_args} ${cbsd_mq_api_flags}
54}
55
56reload()
57{
58	stop
59	start
60}
61
62status()
63{
64	if [ -f "${pidfile}" ]; then
65		pids=$( pgrep -F ${pidfile} 2>&1 )
66		_err=$?
67		if [ ${_err} -eq  0 ]; then
68			echo "${name} is running as pid ${pids}"
69			exit 0
70		else
71			echo "wrong pid: ${pids}"
72			exit 1
73		fi
74	else
75		echo "no pidfile $pidfile"
76		exit 1
77	fi
78}
79
80run_rc_command "$1"
81