#!/usr/bin/perl
#
# Copyright 2007-2014 SPARTA, Inc.  All rights reserved.  See the COPYING
# file distributed with this software for details.
#
#
# dtconf
#
#	This script displays the contents of a DNSSEC-Tools configuration file.
#

use strict;

use Getopt::Long qw(:config no_ignore_case_always);

use Net::DNS::SEC::Tools::conf;
use Net::DNS::SEC::Tools::defaults;

#
# Version information.
#
my $NAME   = "dtconf";
my $VERS   = "$NAME version: 2.1.0";
my $DTVERS = "DNSSEC-Tools Version: 2.1";

#######################################################################

#
# Data required for command line options.
#
my %options = ();			# Filled option array.
my @opts =
(
	"key=s",			# Just give this key's value.
	"Version",			# Display the version number.
	"help",				# Give a usage message and exit.
);

my $key;				# Key to display.

my %dtconf;				# Config file contents.

#######################################################################

main();
exit(0);

#-----------------------------------------------------------------------------
# Routine:	main()
#
# Purpose:	Staging area.
#
sub main()
{
	my $conffile;			# The config file to check.

	#
	# Get our options.
	#
	doopts();

	#
	# Get the config filename to check.
	#
	$conffile = getfile();

	#
	# Read the config file.
	#
	%dtconf = parseconfig($conffile);
	if(%dtconf == 0)
	{
		err("config file \"$conffile\" not parsed\n");
		exit(-1);
	}

	confout();

}

#-----------------------------------------------------------------------------
# Routine:	doopts()
#
# Purpose:	This routine deals with the command's options.
#
sub doopts
{
	#
	# Check our options.
	#
	GetOptions(\%options,@opts) || usage();
	$key	 = $options{'key'};

	#
	# Show the version number if requested
	#
	version() if(defined($options{'Version'}));
	usage() if(defined($options{'help'}));
}

#-----------------------------------------------------------------------------
# Routine:	getfile()
#
# Purpose:	This routine gets the configuration file to display.
#		If a file wasn't specified on the command line, we'll
#		use the default DNSSEC-Tools config file.
#
sub getfile
{
	my $conffile;				# Configuration filename.

	#
	# Get the name to use.
	#
	if($ARGV[0] ne "")
	{
		$conffile = $ARGV[0];
	}
	else
	{
		$conffile = getconffile();
	}

	#
	# Ensure the file exists.
	#
	if(! -e $conffile)
	{
		print STDERR "$conffile does not exist\n";
		exit(1);
	}

	#
	# Ensure the file is a file.
	#
	if(! -f $conffile)
	{
		print STDERR "$conffile is not a regular file\n";
		exit(1);
	}

	#
	# Return the file name to the caller.
	#
	return($conffile);
}

#-----------------------------------------------------------------------------
# Routine:	confout()
#
# Purpose:	This routine displays the contents of the file.
#
sub confout
{
	if($key)
	{
		if(defined($dtconf{$key}))
		{
			print "$key        $dtconf{$key}\n";
		}
		else
		{
			print "$key        (undefined)\n";
		}
	}
	else
	{
		my $maxlen = 0;				# Maximum key length.

		#
		# Calculate the start of the second column.
		#
		foreach my $k (sort(keys(%dtconf)))
		{
			$maxlen = length($k) if(length($k) > $maxlen);
		}
		$maxlen += 8;

		foreach my $k (sort(keys(%dtconf)))
		{
			my $len;			# Length of key.
			my $spaces;			# Number of spaces.

			$len = length($k);
			$spaces = $maxlen - $len;

			print "$k" . (' ' x $spaces) . "$dtconf{$k}\n";
		}
	}
}

#----------------------------------------------------------------------
# Routine:	version()
#
# Purpose:	Print the version number(s) and exit.
#
sub version
{
	print STDERR "$VERS\n";
	print STDERR "$DTVERS\n";

	exit(0);
}

#-----------------------------------------------------------------------------
# Routine:	usage()
#
sub usage
{
	print STDERR "usage:  conf [options] <config file>\n";
	print STDERR "\toptions:\n";
	print STDERR "\t\t-key <key>\n";
	print STDERR "\t\t-Version\n";
	print STDERR "\t\t-help\n";
	exit(0);
}

1;

###############################################################################

=pod

=head1 NAME

dtconf - Display the contents of a DNSSEC-Tools configuration file

=head1 SYNOPSIS

  dtconf [options] [config_file]

=head1 DESCRIPTION

B<dtconf> displays the key/value pairs of a DNSSEC-Tools configuration file.
If a configuration file isn't specified, the system configuration file will
be displayed.

Without the B<-key> option, B<dtconf> displays all the key/value pairs in the
configuration file.  Comments are never displayed.  If the B<-key> option is
given, then only that key/value pair is displayed.  If the key isn't defined,
then the value will be "(undefined)."

=head1 OPTIONS

=over 4

=item B<-key>

The value of the specified key will be printed.  If the key is not defined,
then the value will be given as "(undefined)".

=item B<-Version>

Displays the version information for B<dtconf> and the DNSSEC-Tools package.

=item B<-help>

Display a usage message.

=back

=head1 COPYRIGHT

Copyright 2007-2014 SPARTA, Inc.  All rights reserved.
See the COPYING file included with the DNSSEC-Tools package for details.

=head1 AUTHOR

Wayne Morrison, tewok@tislabs.com

=head1 SEE ALSO

B<dtinitconf(8)>,
B<dtconfchk(8)>

B<Net::DNS::SEC::Tools::conf.pm(3)>

B<dnssec-tools.conf(5)>

=cut
