#!/bin/sh
#
# $OpenBSD: INSTALL,v 1.1 1999/09/23 11:32:45 ho Exp $
#
# Pre/post-installation setup of named

set -e
PATH=/bin:/usr/bin:/sbin:/usr/sbin
PREFIX=${PKG_PREFIX:-/}
NROOT=/var/named
SAMPLEDIR=${NROOT}/inst-sample

if [ $# -ne 2 ]; then
    echo "usage: $0 distname { PRE-INSTALL | POST-INSTALL }" >&2
    exit 1
fi

pre_fix_dir()
{
    if [ ! -d ${1} ]; then
	rm -f ${1}
	mkdir -p ${1}
    fi

    if [ "x${2}" != "x" ]; then
	chown ${2} ${1}
	chmod ${3} ${1}
    else # default
	chown root:wheel ${1}
	chmod 0755 ${1}
    fi
}

post_copy_move()
{
    if [ -f ${SAMPLEDIR}/$1 ]; then
        if [ -f ${NROOT}/${2} ]; then
            echo "Keeping old ${NROOT}/${2} as ${NROOT}/${2}.pre-bind8" >&2
            rm -f ${NROOT}/${2}.pre-bind8
            mv -f ${NROOT}/${2} ${NROOT}/${2}.pre-bind8
        fi
    
        echo "Installing sample ${NROOT}/${2}" >&2
        cp ${SAMPLEDIR}/${1} ${NROOT}/${2}
        chown ${3} ${NROOT}/${2}
        chmod ${4} ${NROOT}/${2}
    fi
}

# Verify/process the command
#
case $2 in
    PRE-INSTALL)
	# Note "descending" order
	pre_fix_dir /usr/share/doc/html/named
	pre_fix_dir /usr/libexec/named
	pre_fix_dir ${NROOT}
	pre_fix_dir ${NROOT}/slave named:bin 0755
	pre_fix_dir ${NROOT}/master named:bin 0755
	pre_fix_dir ${NROOT}/var
	pre_fix_dir ${NROOT}/var/run named:bin 0755
	pre_fix_dir ${NROOT}/var/tmp root:wheel 01777
	pre_fix_dir ${SAMPLEDIR}
	;;
    POST-INSTALL)
	if [ ! -d ${SAMPLEDIR} ]; then
	    # Oops.
	    echo "No sample configuration files found to install !" >&2
	    exit 0
	fi

	echo "" >&2
	echo "Installing BIND 8 sample configuration files" >&2
	echo "" >&2

	post_copy_move named.conf      named.conf      root:named 0640
	post_copy_move named.root      named.root      root:wheel 0644
	post_copy_move named.loopback  named.loopback  root:wheel 0644
	post_copy_move named.localhost named.localhost root:wheel 0644

	rm -rf ${SAMPLEDIR}

	echo "" >&2
	echo "Check your /etc/rc.conf file for the 'named' options." >&2
	echo "Recommended (and also OpenBSD default) values are:" >&2
	echo "  named_flags=\"\"" >&2
	echo "  named_user=named" >&2
	echo "  named_chroot=${NROOT}" >&2
	echo "" >&2
	echo "Check/modify ${NROOT}/named.conf to suit your site." >&2
	echo "" >&2
	;;
    *)
	echo "usage: $0 distname { PRE-INSTALL | POST-INSTALL }" >&2
	exit 1
	;;
esac

exit 0
