#!/bin/sh
#
# $NetBSD: named9.sh,v 1.1 2022/12/11 03:00:01 sekiya Exp $
#

# PROVIDE: named
# REQUIRE: NETWORKING mountcritremote syslogd
# BEFORE:  DAEMON
# KEYWORD: chrootdir

. /etc/rc.subr

name="named"
rcvar="${name}9"
command="/usr/pkg/sbin/${name}"
pidfile="/var/run/named/${name}.pid"
start_precmd="named_precmd"
extra_commands="reload"
required_dirs="$named_chrootdir"	# if it is set, it must exist

named_precmd()
{
	# Die if $named is also set (i.e. the system's named is not disabled
	if checkyesno named ; then
		warn \
	"disable 'named' when setting 'named9'!"
		return 1
	fi

       	if [ ! -d /var/run/named ]; then 
		/bin/mkdir -p /var/run/named 
       	fi      

	if [ -z "$named_chrootdir" ]; then
		return 0;
	fi

	for i in crypto null random urandom
	do
		if [ ! -c "${named_chrootdir}/dev/$i" ]; then
			/bin/rm -f "${named_chrootdir}/dev/$i"
			(cd /dev &&
				/bin/pax -rw -pe "$i" "${named_chrootdir}/dev")
		fi
	done

	if [ -f /etc/localtime ]; then
		/usr/bin/cmp -s /etc/localtime "${named_chrootdir}/etc/localtime" || \
		    /bin/cp -p /etc/localtime "${named_chrootdir}/etc/localtime"
	fi

	if [ -f /usr/lib/engines/libgost.so ]; then
		if [ ! -d ${named_chrootdir}/usr/lib/engines ]; then
			/bin/mkdir -p ${named_chrootdir}/usr/lib/engines
		fi
		/usr/bin/cmp -s /usr/lib/engines/libgost.so "${named_chrootdir}/usr/lib/engines/libgost.so" || \
		    /bin/cp -p /usr/lib/engines/libgost.so "${named_chrootdir}/usr/lib/engines/libgost.so"
	fi

       	if [ ! -d ${named_chrootdir}/var/run/named ]; then 
		/bin/mkdir -p ${named_chrootdir}/var/run/named 
		/usr/sbin/chown named ${named_chrootdir}/var/run/named
       	fi      

	/bin/rm -f ${pidfile}
	/bin/ln -s "${named_chrootdir}${pidfile}" ${pidfile}

	#	Change run_rc_commands()'s internal copy of $named_flags
	#
	rc_flags="-u named -t ${named_chrootdir} $rc_flags"
}

load_rc_config "$rcvar"
run_rc_command "$1"
