#!/bin/sh
#
# $NetBSD: sndio.sh,v 1.3 2025/11/30 13:47:37 vins Exp $
#
# PROVIDE: sndio
# REQUIRE: DAEMON
# KEYWORD: shutdown

. /etc/rc.subr

name="sndio"
rcvar=${name}
command="/usr/pkg/bin/sndiod"
command_args="-dd"
pidfile="/var/run/sndio/pid"
logfile="/var/run/sndio/log"
start_precmd=sndio_prestart
start_cmd=sndio_start
stop_cmd="sndio_stop"

sndio_prestart() {
	dir="/var/run/sndio"
	if [ ! -d "$dir" ]; then
		/bin/mkdir -p -p $dir
		/bin/chmod 0755 $dir
		/usr/sbin/chown _sndio:_sndio $dir
	fi
}

sndio_start()
{
	echo "Starting sndio server"
	daemonize="/usr/pkg/sbin/daemonize"
	if [ -x "$daemonize" ]; then
		$daemonize -p ${pidfile} -e ${logfile} \
			${command} ${command_args} ${sndio_flags}
	else
		${command} ${command_args} ${sndio_flags}
	fi
}

sndio_stop()
{
	echo "Stopping sndio server"
	sndio_s=$(check_pidfile ${pidfile} ${command})
	if [ -n "${sndio_s}" ]; then
		kill ${sig_stop} ${sndio_s}
		wait_for_pids ${sndio_s}
	fi
}

load_rc_config ${name}
run_rc_command "$1"
