#!/bin/sh
#
# $NetBSD: tinydynpop3d.sh,v 1.2 2020/10/19 07:15:55 schmonz Exp $
#
# tinydyndns-run-20201030nb3 script to authenticate tinydyndns updates.
#

# PROVIDE: tinydynpop3d named

name="tinydynpop3d"

# User-settable rc.conf variables and their default values:
: ${tinydynpop3d_postenv:="CVM_PWFILE_PWCMP=crypt"}
: ${tinydynpop3d_datalimit:="180000000"}
: ${tinydynpop3d_pretcpserver:=""}
: ${tinydynpop3d_tcpserver:="/usr/pkg/bin/tcpserver"}
: ${tinydynpop3d_tcpflags:="-vRH"}
: ${tinydynpop3d_tcphost:="0.0.0.0"}
: ${tinydynpop3d_tcpport:="110"}
: ${tinydynpop3d_precheckpassword:="/usr/pkg/bin/qmail-popup $(/usr/bin/head -1 /usr/pkg/etc/tinydyn/domain)"}
: ${tinydynpop3d_checkpassword:="/usr/pkg/bin/cvm-checkpassword /usr/pkg/bin/cvm-pwfile"}
: ${tinydynpop3d_prepop3d:="/usr/pkg/bin/tinydyndns-call-update"}
: ${tinydynpop3d_pop3dcmd:="/usr/pkg/bin/tinydyndns-pop3d"}
: ${tinydynpop3d_postpop3d:=""}
: ${tinydynpop3d_log:="YES"}
: ${tinydynpop3d_logcmd:="logger -t nbtinydyn/pop3d -p mail.info"}
: ${tinydynpop3d_nologcmd:="/usr/pkg/bin/multilog -*"}

tinydyn_cdb="/usr/pkg/etc/tinydyn/data.cdb"
tinydyn_domain="/usr/pkg/etc/tinydyn/domain"
tinydyn_passwd="/usr/pkg/etc/tinydyn/passwd"

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

rcvar=${name}
required_files="${tinydyn_cdb} ${tinydyn_domain}"
rcvar=${name}
command="${tinydynpop3d_tcpserver}"
procname=nb${name}
start_precmd="tinydynpop3d_precmd"

tinydynpop3d_precmd() {
	if [ -f /etc/rc.subr ] && ! checkyesno tinydynpop3d_log; then
		tinydynpop3d_logcmd=${tinydynpop3d_nologcmd}
	fi
	# tcpserver(1) is akin to inetd(8), but runs one service per process.
	# We want to signal only the tcpserver process responsible for this
	# service. Use argv0(1) to set procname to "nbtinydynpop3d".
	command="/usr/pkg/bin/pgrphack /usr/bin/env - ${tinydynpop3d_postenv} \
CVM_PWFILE_PATH=${tinydyn_passwd} \
/usr/pkg/bin/softlimit -m ${tinydynpop3d_datalimit} ${tinydynpop3d_pretcpserver} \
/usr/pkg/bin/argv0 ${tinydynpop3d_tcpserver} ${procname} \
${tinydynpop3d_tcpflags} \
${tinydynpop3d_tcphost} ${tinydynpop3d_tcpport} \
${tinydynpop3d_precheckpassword} ${tinydynpop3d_checkpassword} \
${tinydynpop3d_prepop3d} ${tinydynpop3d_pop3dcmd} ${tinydynpop3d_postpop3d} \
2>&1 7>&1 | \
/usr/pkg/bin/pgrphack /usr/pkg/bin/setuidgid dnslog ${tinydynpop3d_logcmd}"
	command_args="&"
	rc_flags=""
}

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