#!/bin/sh
#
# $NetBSD: prosody.sh,v 1.9 2025/11/25 15:53:15 sborrill Exp $
#
# PROVIDE: prosody
# REQUIRE: DAEMON
# KEYWORD: shutdown

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

name="prosody"
rcvar=${name}
command="/usr/pkg/bin/${name}"
command_args="-D"
command_interpreter="/usr/pkg/bin/lua5.4"
required_files="/usr/pkg/etc/prosody/${name}.cfg.lua"
pidfile="/var/run/prosody/${name}.pid"
start_precmd="prosody_precmd"
stop_postcmd="prosody_postcmd"
extra_commands="reload"
prosody_user="prosody"
prosody_group="prosody"

prosody_precmd()
{
    if ! test -d /var/run/prosody; then
        /bin/mkdir -p -m 0750 /var/run/prosody
    fi
    /usr/sbin/chown prosody:prosody /var/run/prosody
    #ulimit -n 2048
}

prosody_postcmd()
{
    # Fix for failed socket clean-up which stops
    # prosodyctl working after a restart. This is caused
    # by a potential build-time configuration error with
    # lua-socket.
    if test -S "/var/run/prosody/${name}.soc"; then
        rm -f "/var/run/prosody/${name}.soc"
    fi
}

load_rc_config $name
run_rc_command "$1"
