#!/bin/sh /etc/rc.common

START=95
STOP=10
USE_PROCD=1

NAME="secubox-p2p"
PROG="/usr/sbin/secubox-p2p"

# Ensure avahi-daemon is started before us
DEPEND="avahi-daemon"

start_service() {
	local enabled
	config_load secubox-p2p
	config_get enabled main enabled "1"

	[ "$enabled" = "1" ] || return

	# Ensure state directory exists
	mkdir -p /var/run/secubox-p2p

	# Ensure avahi-daemon is running for mDNS
	if [ -x /etc/init.d/avahi-daemon ]; then
		/etc/init.d/avahi-daemon running || /etc/init.d/avahi-daemon start
	fi

	procd_open_instance
	procd_set_param command $PROG daemon
	procd_set_param respawn
	procd_set_param stdout 1
	procd_set_param stderr 1
	procd_set_param pidfile /var/run/secubox-p2p/daemon.pid
	procd_close_instance

	logger -t secubox-p2p "P2P mesh daemon started"
}

stop_service() {
	# Stop mDNS announcement
	$PROG stop-mdns 2>/dev/null
	logger -t secubox-p2p "P2P mesh daemon stopped"
}

service_triggers() {
	procd_add_reload_trigger "secubox-p2p"
}

reload_service() {
	stop
	start
}
