#!/bin/sh
#
# $NetBSD: fcgiwrap.sh,v 1.3 2025/08/28 04:27:31 khorben Exp $
#
# PROVIDE: fcgiwrap
# REQUIRE: DAEMON
#
# You will need to set some variables in /etc/rc.conf to start fcgiwrap:
#
# fcgiwrap=YES

$_rc_subr_loaded . /etc/rc.subr

name="fcgiwrap"
rcvar=${name}
command="/usr/pkg/sbin/${name}"
pidfile="/var/run/${name}/${name}.pid"
fcgiwrap_user="nginx"
fcgiwrap_group="nginx"
start_precmd="fcgiwrap_precmd"
start_cmd="fcgiwrap_start"
start_postcmd="fcgiwrap_poststart"
fcgiwrap_socket="/var/run/${name}/${name}.sock"

fcgiwrap_precmd()
{
	if [ ! -d /var/run/${name} ]; then
		/bin/mkdir -p /var/run/${name}
		/usr/sbin/chown ${fcgiwrap_user}:${fcgiwrap_group} /var/run/${name}
	fi

	if [ -e ${fcgiwrap_socket} ]; then
		/bin/rm -f ${fcgiwrap_socket}
	fi
}

fcgiwrap_start()
{
	echo "Starting ${name}."
	/usr/bin/su -m ${fcgiwrap_user} -c "${command} -f -s unix:${fcgiwrap_socket}" &
}

fcgiwrap_poststart() {
	echo $! > ${pidfile}
}

fcgiwrap_stop()
{
	kill -HUP $(check_pidfile $pidfile $name)
}

load_rc_config $name
run_rc_command "$1"
