#!/bin/sh
#
# $NetBSD: turnserver.sh,v 1.4 2025/01/23 17:21:26 gdt Exp $
#
# PROVIDE: turnserver
# REQUIRE: LOGIN
# KEYWORD: shutdown
#
# Add the following lines to /etc/rc.conf.local or /etc/rc.conf
# to enable this service:
#
# turnserver (bool):		Set to NO by default.
#				Set it to YES to enable turnserver.
# turnserver_config (path):	Set to /usr/pkg/etc/turnserver.conf
#				by default.

if [ -f /etc/rc.subr ]; then
  . /etc/rc.subr
fi

name="turnserver"
rcvar=${name}

if [ -f /etc/rc.subr -a -d /etc/rc.d -a -f /etc/rc.d/DAEMON ]; then
	load_rc_config $name
elif [ -f /etc/rc.conf ]; then
	. /etc/rc.conf
fi

: ${turnserver:=no}
: ${turnserver_config=/usr/pkg/etc/turnserver.conf}
: ${coturn_user:=coturn}
: ${coturn_group:=coturn}

pidfile="/var/run/${name}.pid"
command="/usr/pkg/bin/${name}"
command_args="--daemon --proc-user ${coturn_user} --proc-group ${coturn_user} -c ${turnserver_config} --pidfile ${pidfile}"
required_files=${turnserver_config}

if [ -f /etc/rc.subr -a -d /etc/rc.d -a -f /etc/rc.d/DAEMON ]; then
	run_rc_command "$1"
else
	case "$1" in
	stop)
		if [ -r "${pidfile}" ]; then
			echo "Stopping ${name}."
			kill `/bin/cat ${pidfile}`
		fi
		;;
	*)
		${command} ${command_args}
		;;
	esac
fi
