#!/bin/sh

# Shell script to collect DNS-data as an optional user-module.
# Copyright (C) 2001-2004 Open Challenge B.V.
# Copyright (C) 2004 OpenEyeT Professional Services.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; see the file COPYING.
# If not, write to the Free Software Foundation,
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# Contact information: www.OpenEyeT.nl/scc/index.html 

# This is a system module of scc, to call it separately in the
# proper environment, use: scc-collect -i -e <module_name>

# SCC-release:	1.6.26
# file-version:	$Revision: 1.5 $
 
. ${SCC_BIN}/scc_modules/scc_utils

# When you change these labels, also change them in the network system-module!
named_class="fix:network:named"
scc_keep_named_config="network:named:config"
scc_keep_named_count="network:named:count"

named_conf="$(sed -n -e "s/^${scc_keep_named_config}://p" ${SCC_KEEP_NEW})"
if [ -z "${named_conf}" ]
then
	# Nothing to collect.
	exit 0
fi

if [ ! -f "${named_conf}" ]
then
	named_conf=/var/named/named.conf
	if [ ! -f "${named_conf}" ]
	then
		named_conf=/etc/named.conf
	fi
fi

# This named.conf can contain included files.
# The syntax is:
#	include "named.zones.pri" ;
includes="$(sed -n -e 's/[" 	;]*$//' -e 's/^[ 	]*include[ 	]*["]*//p' "${named_conf}" 2>/dev/null)"

# The named.conf can contain the base-directory of the config-files.
# The syntax is:
#	options {
#		directory "/var/named";
#		}
base_dir="$(awk	'/^[ 	]*options /	{ in_options=1; }
		/directory/		{
						if ( in_options )
						{
							$1 = "";
							gsub( /^[ 	"]*/, "", $0 );
							gsub( /[ 	";]*$/, "", $0 );
							print $0;
						}
					}
		/^[ 	]*}/		{ in_options=0 }' "${named_conf}" 2>/dev/null)"

if [ ! -d "${base_dir}" ]
then
	# Not in the config file, try the base-directory of the config-file.
	base_dir="$(dirname ${named_conf})"
fi
for f in ${named_conf} ${includes}
do
	if [ ! -f "${f}" ]
	then
		f="${base_dir}/${f}"
	fi

	# Add the configuration file to the snapshot.
	if [ -f "${f}" -a "${f}" != "${named_conf}" ]
	then
		sed -e "s@^@${named_class}:config ${f}::@" "${f}"
	fi

	if [ -f "${f}" ]
	then
		# Get the database files from the configuration file and its included files.
		# Syntax is:
		#	zone "17.168.192.in-addr.arpa" {
		#	  type master;
		#	  notify no;
		#	  file "db/192.168.17";
		#	};
		awk	'/^[ 	]*zone/	{ in_zone=1; next }
			/^[	]}/	{ in_zone=0; next }
			/^[ 	]*file/	{
						if ( ! in_zone )
						{
							next;
						}
						$1 = "";
						gsub( /^[ 	"]*/, "", $0 );
						gsub( /[ 	";]*$/, "", $0 );
						print $0;
					}' "${f}"			|
		while read db_file
		do
			if [ ! -f "${db_file}" ]
			then
				if [ -f "${base_dir}/${db_file}" ]
				then
					db_file="${base_dir}/${db_file}"
				else
					db_file="$(dirname "${f}")/${db_file}"
				fi
			fi
			if [ -f "${db_file}" ]
			then
				# Add the data of the file to the snapshot.
				sed -e "s@^@${named_class}:zone ${db_file}::@" "${db_file}"

				# Also show the included files.
				# Syntax is:
				#	$INCLUDE db-clns.nsap.rev
				sed -n	-e 's/[ 	";]*$//'	\
					-e 's/.*INCLUDE[ 	"]*//p'	\
						"${db_file}"			|
				while read db_include
				do
					if [ ! -f "${db_include}" ]
					then
						if [ -f "${base_dir}/${db_include}" ]
						then
							db_include="${base_dir}/${db_include}"
						else
							db_include="$(dirname "${db_file}")"
						fi
					fi
					if [ -f "${db_include}" ]
					then
						sed -e "s@^@${named_class}:include ${db_include}::@" "${db_include}"
					fi
				done
			fi
		done	# while read db_file
	fi	# if [ -f "${f}" ] # Config-file
done	# for f in ${named_conf} ${includes}

exit 0
