#!/bin/sh
#
# Copyright (c) 2003-2014 SUSE Linux GmbH, Nuernberg, Germany.
# All rights reserved.
#
# Authors: Lars Mueller <lmuelle@suse.com>
#


#
# check if we are started as root
# only one of UID and USER must be set correctly
#
[ -n "$UID" ] || UID="$(id -ru)"
if test "$UID" != 0 -a "$USER" != root; then
    echo "You must be root to start $0."
    exit 1
fi

BASENAME=$( basename $0)
NAMED_CHECKCONF_BIN="/usr/sbin/named-checkconf"

warnMessage()
{
	tput bold
	echo -n "Warning: "
	tput sgr0
	echo $1
}

errorMessage()
{
	tput bold
	echo -n "Error: "
	tput sgr0
	echo $1
}

MD5DIR="/var/adm/bind/md5"
check_md5_and_move() # Usage: check_md5_and_move file_name-without.BINDconfig
{
    # This function checks the existence of a file (specified without the
    # extension .BINDconfig and without "$r") and a corresponding md5 checksum
    # and tests whether the time stamp of the file has changed.
    # If it has, nothing further will happen. If not, the "file.BINDconfig"
    # will be moved to "file".

    FILE=$1
    MD5DIR=/var/adm/bind
    if test -n "$r" ; then
        RELPATH=`echo $FILE | sed -e"s:^$r::"`
    else
        RELPATH=$FILE
    fi
    MD5FILE=$MD5DIR/$RELPATH
    #
    # make sure that the directory exists
    mkdir -p `dirname $MD5FILE`
    NEWMD5SUM="`cat $FILE.BINDconfig | grep -v "^#" | md5sum`"
    if test ! -s $FILE ; then
        touch $FILE
        rm -f $MD5FILE
    fi
    if test "$FORCE_REPLACE" = true ; then
        cp -p $FILE.BINDconfig $FILE
    fi
    USERMD5SUM="`cat $FILE | grep -v "^#" | md5sum`"
    test -e $MD5FILE || echo "$USERMD5SUM" > $MD5FILE
    OLDMD5SUM="`cat $MD5FILE`"
    if test "$USERMD5SUM" != "$OLDMD5SUM" -a \
            "$USERMD5SUM" != "$NEWMD5SUM" ; then
        echo
        echo "ATTENTION: You have modified $RELPATH.  Leaving it untouched..."
        echo "You can find my version in $FILE.BINDconfig..."
        echo
    else
        if test "$USERMD5SUM" != "$NEWMD5SUM" -o "$FORCE_REPLACE" = true ; then
            echo "Installing new $RELPATH"
            cp -p $FILE.BINDconfig $FILE
        else
            test "$VERBOSE" = false || echo "No changes for $RELPATH"
        fi
        rm -f $FILE.BINDconfig
    fi
    rm -f $MD5FILE
    echo "$NEWMD5SUM" > $MD5FILE
}

#
# check for named settings
#
if [ ! -f /etc/sysconfig/named ]; then
	errorMessage "No /etc/sysconfig/named found!  Exiting."
	exit 1
fi
. /etc/sysconfig/named

# Check for NAMED_CONF_META_INCLUDE_FILE or set it to our default if we
# make use of this script.
if [ -z "${NAMED_CONF_META_INCLUDE_FILE}" ]; then
	for script in ${NAMED_INITIALIZE_SCRIPTS}; do
		if [ "${script}" = "${BASENAME}" -o \
		"${script}" = "/usr/share/bind/createNamedConfInclude" ]; then
			NAMED_CONF_META_INCLUDE_FILE="/etc/named.conf.include"
			break
		fi
	done
	# If NAMED_CONF_META_INCLUDE_FILE is still empty skip silent.
	test -z "${NAMED_CONF_META_INCLUDE_FILE}" && exit 0
fi

NEW_NAMEDCONFINCLUDE_FILE="${NAMED_CONF_META_INCLUDE_FILE}.BINDconfig"
if [ -f "${NAMED_CONF_META_INCLUDE_FILE}" -a \
	! -f "${NEW_NAMEDCONFINCLUDE_FILE}" ]; then
	touch "${NEW_NAMEDCONFINCLUDE_FILE}"
	chmod --reference="${NAMED_CONF_META_INCLUDE_FILE}" "${NEW_NAMEDCONFINCLUDE_FILE}"
	chown --reference="${NAMED_CONF_META_INCLUDE_FILE}" "${NEW_NAMEDCONFINCLUDE_FILE}"
fi
#
# find the next unused file descriptor
#
fd=3
while [ -t ${fd} ]; do
	fd=$(( ${fd} + 1 ))
done
eval "exec ${fd}> \"${NEW_NAMEDCONFINCLUDE_FILE}\""

DATE=$( LANG=POSIX date)
cat << EOF >&${fd}
#
# This file is autogenerated by /usr/share/bind/createNamedConfInclude
# on ${DATE}.  Don't edit it manually.
#
# Add additional configuration files which should be added to /etc/named.conf
# by this mechanism to NAMED_CONF_INCLUDE_FILES in /etc/sysconfig/named.  This
# is possible with the YaST sysconfig or any other editor.
#
# See /usr/share/doc/packages/__BIND_PACKAGE_NAME__/README.__VENDOR__ section
# createNamedConfInclude for more details.
#

EOF

INCLUDE_LINES=$( grep -e '^[[:space:]]*include' "/etc/named.conf" | cut -f 2 -d '"')
includeUsed="no"
for file in ${NAMED_CONF_INCLUDE_FILES}; do
	# prepend the default include directory if the filename is relative
	case "$file" in
		/*) ;;
		*) file="/etc/named.d/${file}" ;;
	esac
	if [ ! -f "${file}" ]; then
		warnMessage "File, ${file} to include not found!  Skipping."
		continue
#	elif ! ${NAMED_CHECKCONF_BIN} "${file}"; then
#		warnMessage "${file} is no valid named configuration part.  Skipping."
#		continue
	fi
	# FIXME Is the file readable by user 'named'?
	echo "include \"${file}\";" >&${fd}
	includeUsed="yes"
done
eval "exec ${fd}<&-"

#
# only replace "${NAMED_CONF_META_INCLUDE_FILE}" if we need it
#
if [ "${includeUsed}" = 'yes' ]; then
	test ! -f "${NAMED_CONF_META_INCLUDE_FILE}" && SET_PERMISSIONS="yes"
	VERBOSE="false"
	check_md5_and_move "${NAMED_CONF_META_INCLUDE_FILE}"
else
	rm -f "${NEW_NAMEDCONFINCLUDE_FILE}"
	touch "${NAMED_CONF_META_INCLUDE_FILE}"
	SET_PERMISSIONS="yes"
fi

if [ "yes" = "${SET_PERMISSIONS}" ]; then
	chown root:named "${NAMED_CONF_META_INCLUDE_FILE}"
	chmod 0644 "${NAMED_CONF_META_INCLUDE_FILE}"
fi

exit 0

