#!/bin/sh
#
# $NetBSD: tinydyndns.sh,v 1.2 2020/10/19 07:15:55 schmonz Exp $
#
# tinydyndns-run-20250425 script to control tinydyndns (dynamic DNS service).
#

# PROVIDE: tinydyndns named
# REQUIRE: NETWORKING mountcritremote syslogd
# BEFORE:  DAEMON

name="tinydyndns"

# User-settable rc.conf variables and their default values:
: ${tinydyndns_postenv:=""}
: ${tinydyndns_ip:="0.0.0.0"}
: ${tinydyndns_datalimit:="300000"}
: ${tinydyndns_autodata:="YES"}
: ${tinydyndns_autocdb:="YES"}
: ${tinydyndns_log:="YES"}
: ${tinydyndns_logcmd:="logger -t nbtinydyn/dns -p daemon.info"}
: ${tinydyndns_nologcmd:="/usr/pkg/bin/multilog -*"}

tinydyn_data="/usr/pkg/etc/tinydyn/data"
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_domain} ${tinydyn_passwd}"
command="/usr/pkg/bin/tinydns"
procname=nb${name}
start_precmd="tinydyndns_precmd"
extra_commands="rebuild"
rebuild_cmd="tinydyndns_rebuild"

tinydyndns_precmd() {
	if [ -f /etc/rc.subr ] && ! checkyesno tinydyndns_log; then
		tinydyndns_logcmd=${tinydyndns_nologcmd}
	fi
	if [ -f /etc/rc.subr ] && checkyesno tinydyndns_autodata; then
		tinydyndns_needdata && tinydyndns_initdata
	else
		required_files="${tinydyn_data} ${required_files}"
	fi
	if [ -f /etc/rc.subr ] && checkyesno tinydyndns_autocdb; then
		tinydyndns_needcdb && tinydyndns_rebuild
	else
		required_files="${tinydyn_data}.cdb ${required_files}"
	fi
	command="/usr/pkg/bin/pgrphack /usr/bin/env - ${tinydyndns_postenv} \
ROOT=/usr/pkg/etc/tinydyn \
IP=${tinydyndns_ip} \
/usr/pkg/bin/envuidgid tinydyn \
/usr/pkg/bin/softlimit -d ${tinydyndns_datalimit} \
/usr/pkg/bin/argv0 /usr/pkg/bin/tinydns ${procname} \
</dev/null 2>&1 | \
/usr/pkg/bin/pgrphack /usr/pkg/bin/setuidgid dnslog ${tinydyndns_logcmd}"
	command_args="&"
	rc_flags=""
}

tinydyndns_needdata() {
	[ ! -f "${tinydyn_data}" ]
}

tinydyndns_initdata() {
	echo "Auto-creating ${tinydyn_data}."
	if [ ${tinydyndns_ip} = "0.0.0.0" ]; then
		echo >&2 "ERROR: set tinydyndns_ip in rc.conf"
		exit 1
	fi
	_domain=$(/usr/bin/head -1 ${tinydyn_domain})
	if [ -z "${_domain}" ]; then
		echo >&2 "ERROR: write the domain to ${tinydyn_domain}"
		exit 2
	fi
	/bin/cat >${tinydyn_data}.tmp <<EOF
.${_domain}:${tinydyndns_ip}:a:259200
+${_domain}:${tinydyndns_ip}:86400
EOF
	mv -f -f ${tinydyn_data}.tmp ${tinydyn_data}
	/usr/sbin/chown tinydyn ${tinydyn_data}
	/bin/chmod 644 ${tinydyn_data}
}

tinydyndns_needcdb() {
	_src=${tinydyn_data}
	_dst=${tinydyn_data}.cdb
	[ -f "${_src}" -a "${_src}" -nt "${_dst}" ] || [ ! -f "${_dst}" ]
}

tinydyndns_rebuild() {
	echo "Rebuilding ${tinydyn_data}.cdb."
	cd /usr/pkg/etc/tinydyn && /usr/pkg/bin/tinydns-data
	_domain=$(/usr/bin/head -1 ${tinydyn_domain})
	/usr/bin/cut -d: -f1 < ${tinydyn_passwd} \
		| /usr/bin/sed -e "s|\$|.${_domain}|" \
		| /usr/bin/xargs /usr/pkg/bin/tinydyndns-data add
	/usr/sbin/chown tinydyn ${tinydyn_data}.cdb
	/bin/chmod 644 ${tinydyn_data}.cdb
}

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