#!/bin/sh

# is_lsb_ubuntu exit status indicates whether system appears to be Ubuntu.
# Using required /etc/lsb-release file, instead of optional lsb_release command.

is_lsb_ubuntu ()
{
    awk '
        (match($0, "^[ \t]*DISTRIB_ID=") == 1) {    # A DISTRIB_ID line
            id = substr($0, RLENGTH + 1)            # Save its value
        }

        END {
            # Examine last DISTRIB_ID value to see if Ubuntu indicated

            if (match(id, "^(Ubuntu|\"Ubuntu\")[ \t]*$") == 1) {
                exit 0    # Ubuntu
            }
            exit 1        # Not Ubuntu
        }

    ' /etc/lsb-release >/dev/null 2>&1

    # Routine exit status is exit status of the last command -- the awk script.
    #
    #   Note: if /etc/lsb-release does not exist, the exit status indicates
    #         failure (not Ubuntu), which is the correct outcome.
}


DIRECTORY=/var/named

# check for SLES
grep -s -q sles /etc/os-release
IS_SLES=$?
if [ -f /etc/SuSE-release ] || [ $IS_SLES -eq 0 ]; then
  DIRECTORY=/var/lib/named
fi
FILE=/etc/named.conf

if ( is_lsb_ubuntu ); then
  FILE=/etc/bind/named.conf
fi

#unalias cp
if [ -f $FILE ]; then
  cp -f $FILE ${FILE}.ORIG
fi
if [ ! -d $DIRECTORY ]; then
  mkdir $DIRECTORY
fi
echo "options {
	directory \"$DIRECTORY\";
	dump-file \"$DIRECTORY/data/cache_dump.db\";
	statistics-file \"$DIRECTORY/data/named_stats.txt\";
	memstatistics-file \"$DIRECTORY/data/named_mem_stats.txt\";
	recursion yes;
	forward only;
	forwarders {" >$FILE

for i in $(grep "^nameserver" /etc/resolv.conf | awk '{print $2}')
do
	echo "		$i;"
done >>$FILE
echo "	};" >>$FILE
BIND_VERSION=$(/usr/sbin/named -v | cut -d" " -f2)
if [[ $BIND_VERSION > "9.16.5" ]]; then
        echo "	dnssec-enable no;
	dnssec-validation no;" >>$FILE
fi
echo "};" >>$FILE

