#!/bin/sh /etc/rc.common
# Copyright (C) 2015 OpenWrt.org

NAME=pianod
START=95

USE_PROCD=1

PD_USER=$NAME
PD_STARTSCRIPT=/etc/$NAME/startscript
PD_PASSWDFILE=/etc/$NAME/users
PD_WEBUI=/www/$NAME

start_service() {
	# Take group ownership of audio devices
	chown root:$NAME /dev/snd/* /dev/mixer /dev/dsp
	chmod 664 /dev/snd/* /dev/mixer /dev/dsp

	procd_open_instance

	procd_set_param command "/usr/bin/$NAME"

	procd_append_param command -n $PD_USER
	procd_append_param command -i "$PD_STARTSCRIPT"
	procd_append_param command -u "$PD_PASSWDFILE"
	[ -d "$PD_WEBUI" ] && procd_append_param command -c "$PD_WEBUI"
	# No HTTPS support without certificates
	procd_append_param command -s 0

	procd_close_instance
}

# Wait for service to exit and release sockets
reload_service() {
	stop
	sleep 2
	start
}

restart() {
	reload_service
}
