#!/usr/bin/perl
# Copyright 2006-2008 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 environments!
#

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

$rrf = $ARGV[0];

#
# Get the GMT time and lop off the trailing newline.
#
$tempus = gmtime;
chomp $tempus;

#
# 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.
#
$min = adjuster($min,1);

#
# Build the new "phasestart" lines for the rollrec file.
#
$phase1 = sprintf("phasestart	\"$date1%02d$date2\"",$min);

#
# Fix the "phasestart" lines in the rollrec file.
#
system("perl -pi -e 's/phasestart/$phase1/' $rrf");

print "gmtime - $tempus\n";
print "phase  - $phase1\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);
}

