#!/bin/sh
#
# $NetBSD: pubhttpd.sh,v 1.2 2015/11/25 12:52:12 jperkin Exp $
#
# publicfile-run-20180730 script to control publicfile httpd (HTTP server)
#

# PROVIDE: pubhttpd
# REQUIRE: NETWORKING mountcritremote syslogd
# BEFORE:  DAEMON

name="pubhttpd"

# User-settable rc.conf variables and their default values:
: ${pubhttpd_root:="/var/public"}
: ${pubhttpd_postenv:=""}
: ${pubhttpd_datalimit:="50000"}
: ${pubhttpd_localname:="0"}
: ${pubhttpd_backlog:="50"}
: ${pubhttpd_conlimit:="100"}
: ${pubhttpd_host:="0"}
: ${pubhttpd_port:="80"}
: ${pubhttpd_log:="YES"}
: ${pubhttpd_logcmd:="logger -t nb${name} -p ftp.info"}
: ${pubhttpd_nologcmd:="/usr/pkg/bin/multilog -*"}

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

rcvar=${name}
required_dirs="${pubhttpd_root}"
command="/usr/pkg/bin/tcpserver"
procname=${name}
start_precmd="pubhttpd_precmd"

pubhttpd_precmd()
{
	# tcpserver(1) is akin to inetd(8), but runs one service per process.
	# We want to signal only the tcpserver process responsible for the HTTP
	# service.  Use argv0(1) to set the process name to "pubhttpd".
	if [ -f /etc/rc.subr ]; then
		checkyesno pubhttpd_log || pubhttpd_logcmd=${pubhttpd_nologcmd}
	fi
 	command="/usr/bin/env - ${pubhttpd_postenv} /usr/pkg/bin/envuidgid pubhttp /usr/pkg/bin/softlimit -o20 -d${pubhttpd_datalimit} /usr/pkg/bin/argv0 /usr/pkg/bin/tcpserver ${name} -vDRH -l${pubhttpd_localname} -b${pubhttpd_backlog} -c${pubhttpd_conlimit} ${pubhttpd_host} ${pubhttpd_port} /usr/pkg/publicfile/bin/httpd ${pubhttpd_root} 2>&1 | /usr/pkg/bin/setuidgid publog ${pubhttpd_logcmd}"
	command_args="&"
	rc_flags=""
}

if [ -f /etc/rc.subr ]; then
	load_rc_config $name
	run_rc_command "$1"
else
	echo -n " ${name}"
	pubhttpd_precmd
	eval ${command} ${pubhttpd_flags} ${command_args}
fi
