#!/usr/bin/perl
#
# Copyright 2006-2012 SPARTA, Inc.  All rights reserved.
# See the COPYING file included with the DNSSEC-Tools package for details.
#
# rundemo
#
#	This script was automatically generated by the makezones script.
#	It is used to run a demo of the DNSSEC-Tools rollerd daemon.
#

use strict;

use Getopt::Long qw(:config no_ignore_case_always);
use Net::DNS::SEC::Tools::rollrec;

my %options = ();				# Filled option array.
my @opts =
(
	"autods",				# Auto-pub DS records.
	"reload",				# Reload zones.
	"loglevel=n",				# Logging level.
	"help",					# Display help message.
);

my $cmd;					# Demo command to execute.
my $dir;					# Current-directory argument.
my $log;					# Log-file argument.
my $lvl;					# Log-level argument.
my $rrf;					# Rollrec-file argument.
my $slp;					# Sleep-time argument.

my $curdir    = ".";
my $logfile   = "log.demo";
my $loglvl    = "phase";
my $noload    = "-noreload";
my $sleeptime = "15";
my $rrfile    = "demo.rollrec";

my $autods    = 0;				# Auto-DS'ing flag.
my $cpid      = 0;				# autods child pid.

main();
exit();

#------------------------------------------------------------------------------
# Routine:	main()
#
sub main
{
	#
	# Set the log level, maybe using a command-line argument.
	#
	opts();
	print "\nusing a loglevel of \"$loglvl\"\n\n";

	#
	# Set our remaining arguments.
	#
	$dir	= "-dir $curdir";
	$log	= "-logfile $logfile";
	$lvl	= "-loglevel $loglvl";
	$slp	= "-sleep $sleeptime";
	$rrf	= "$rrfile";

	#
	# Build our demo command.
	#
	$cmd = "rollerd $dir $log $lvl $noload $slp -rrf $rrf";

	#
	# Ensure rollerd is not running and zap the demo log file.
	#
	system("rollctl -halt >/dev/null 2>&1 &");
#	system("rollctl -halt");
	system("cp /dev/null $logfile");

	#
	# Set up automatic DS handling.  (maybe)
	autods();

	#
	# Start the demo and peek at the log file.
	#
	system("$cmd &");
#	system("tail -f $logfile");

	#
	# Start a GUI to keep track of what's happening.
	#
	sleep(2);
	system("lights -int 15s -rrf demo.rollrec &");
}

#------------------------------------------------------------------------------
# Routine:	opts()
#
sub opts
{
	GetOptions(\%options,@opts) || usage();
	usage() if($options{'help'});

	$loglvl	= $options{'loglvl'} if(defined($options{'loglvl'}));

	$autods = defined($options{'autods'});

	$noload = "" if(defined($options{'reload'}));
}

#------------------------------------------------------------------------------
# Routine:	autods()
#
sub autods
{
	my $nap = $sleeptime * 2;			# autods sleep-time.

	#
	# Only set up autods childproc if -autods was given.
	#
	return if(!$autods);
	print "\nstarting auto-DS checking\n\n";
	$SIG{CHLD} = "IGNORE";

	#
	# Spawn a child and let the parent return.
	#
	$cpid = fork();
	return if($cpid != 0);

	#
	# Whenever a zone hits KSK rollover phase 6, tell rollerd that
	# the parent has published the new DS record.
	#
	while(42)
	{
		sleep($nap);

		rollrec_read($rrfile);
		foreach my $zone (sort(rollrec_names()))
		{
			my $kphase = rollrec_recval($zone,'kskphase');

			if($kphase == 6)
			{
				system("rollctl -dspub $zone > /dev/null");
			}
		}
		rollrec_close();
	}
}

#------------------------------------------------------------------------------
# Routine:	usage()
#
sub usage
{
	print STDERR "usage:  rundemo [-autods | -loglevel lvl | -reload]\n";
	exit(0);
}

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

=pod

=head1 NAME

rundemo - Runs a demo of rollerd, the DNSSEC-Tools rollover daemon.

=head1 SYNOPSIS

  rundemo [options]

=head1 DESCRIPTION

B<rundemo> runs a demo of B<rollerd>, the DNSSEC-Tools rollover daemon.
Other DNSSEC-Tools, such as B<zonesigner> and B<blinkenlights> are also
be used in the demo.

This is a generic script that can be used with any number of zone
configurations.  The script was created by the B<makezones> script, which
creates and signs the required zones files, and makes the B<rollrec> file
used to control B<rollerd>.  It may be used for other B<rollerd>-based tests
and demos whose data are not created by B<makezones>.

B<rundemo> starts with initializing the environment by using B<rollctl>
to halt any running B<rollerd> and clearing the B<rollerd> log file.  It
will then start automatic DS-watching, if required, and execute B<rollerd>.
Finally, a display GUI will then be started to show the demo's progress.
This display program may be B<blinkenlights>, B<lights>, or B<bubbles>.
B<blinkenlights> is started by default, but any collection of these GUI
programs may be started by editing the B<rundemo> script.

=head1 OPTIONS

B<rundemo> may be given the following options:

=over 4

=item B<-autods>

This specifies that B<rundemo> will watch the demo's B<rollrec> file
for zones entering KSK rollover phase 6.  When such a zone is found,
B<rundemo> will tell B<rollerd> that the parent has published the
zone's DS record and the zone can continue to KSK phase 7.

Caveat:  Occasionally, a timing issue strikes and the demo will not start
until a B<rundemo> has been given a Control-C.  In this case, the
B<autods> script should be used.

=item B<-loglevel level>

This specifies the logging level that will be used by B<rollerd>.

=item B<-reload>

By default, B<rundemo> will run B<rollerd> with the B<-noreload> option
to prevent it from informing the DNS daemon that zones should reloaded.
This option specifies that B<rollerd> should not be executed with B<-noreload>,
thus allowing the DNS daemon to reload its zones.

=item B<-help>

Display a usage message.

=back

=head1 COPYRIGHT

Copyright 2012 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<blinkenlights(8)>,
B<bubbles(8)>,
B<lights(8)>,
B<rollctl(8)>,
B<rollerd(8)>>,
B<zonesigner(8)>>

B<autods>(1)>,
B<makezones>(1)>

B<rollrec(5)>

=cut
