#!/usr/bin/perl
# Copyright 2006-2013 SPARTA, Inc.  All rights reserved.
# See the COPYING file included with the DNSSEC-Tools package for details.
#
# phaser
#
#	This script adjusts the phasestart lines in a DNSSEC-Tools rollrec
#	file so the current phases have only just started.
#
#	This is ONLY intended for building testing and demo environments!
#
#	This script can be copied for use in other demos.  The only thing
#	that'll probably need to be modified is the number of phasestart
#	lines in the rollrec file.  This version looks for three; others
#	must look for how many ever the rollrec has.  Also, each zone's
#	phasestart should be numbered differently from the others.
#

usage() if(@ARGV == 0);

$rrf = $ARGV[0];

#
# Get the GMT time and lop off the trailing newline.
#
$tempus = gmtime;
$tempus =~ s/\n$//;

#
# Split the time up into three chunks, with the minutes in the middle.
#
$tempus =~ /(.*?:)(..)(:.*)/;
$date1 = $1;
$min   = $2;
$date2 = $3;

#
# Drop the minutes back a shade.
#
$min1 = adjuster($min,0);
$min2 = adjuster($min,2);
$min3 = adjuster($min,1);
$min4 = adjuster($min,2);
$min5 = adjuster($min,3);
$min6 = adjuster($min,0);
$min7 = adjuster($min,1);

#
# Build the new "phasestart" lines for the rollrec file.
#
$phase1 = sprintf("phasestart	\"$date1%02d$date2\"",$min1);
$phase2 = sprintf("phasestart	\"$date1%02d$date2\"",$min2);
$phase3 = sprintf("phasestart	\"$date1%02d$date2\"",$min3);
$phase4 = sprintf("phasestart	\"$date1%02d$date2\"",$min4);
$phase5 = sprintf("phasestart	\"$date1%02d$date2\"",$min5);
$phase6 = sprintf("phasestart	\"$date1%02d$date2\"",$min6);
$phase7 = sprintf("phasestart	\"$date1%02d$date2\"",$min7);

#
# Fix the "phasestart" lines in the rollrec file.
#
system("perl -pi -e 's/phasestart1/$phase1/' $rrf");
system("perl -pi -e 's/phasestart2/$phase2/' $rrf");
system("perl -pi -e 's/phasestart3/$phase3/' $rrf");
system("perl -pi -e 's/phasestart4/$phase4/' $rrf");
system("perl -pi -e 's/phasestart5/$phase5/' $rrf");
system("perl -pi -e 's/phasestart6/$phase6/' $rrf");
system("perl -pi -e 's/phasestart7/$phase7/' $rrf");

if(0)
{
	print "gmtime  - $tempus\n";
	print "phase 1 - $phase1\n";
	print "phase 2 - $phase2\n";
	print "phase 3 - $phase3\n";
	print "phase 4 - $phase4\n";
	print "phase 5 - $phase5\n";
	print "phase 6 - $phase6\n";
	print "phase 7 - $phase7\n";
}

exit(0);

#########################################################################
#
# Adjust a minutes count by a certain amount, making sure it doesn't go
# negative.
#
sub adjuster
{
	my $min = shift;				# Minutes to adjust.
	my $adj = shift;				# Adjustment value.

	$min -= $adj;

	$min = 0 if($min < 0);
	return($min);
}

#########################################################################
#
# Give usage and exit.
#
sub usage
{
	print STDERR "usage:  phaser <rollrec-file>\n";
	exit(0);
}


