#!/usr/bin/perl -i
# change the serial number of a SOA record in a bind zone file

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year += 1900;
$today = sprintf ("%.4d%.2d%.2d", $year,++$mon,$mday);

sub usage
{
	print STDERR <<EoF;
usage: $0 zonefile ...
EoF
	exit 1;
}

&usage unless @ARGV > 0;

while(<>) {
	if( /SOA/ .. /\)/ ) {
		s#\b\d{10}\b#&newserial($&, $today)#e;
	}
	print;
}

sub newserial
{
	local($old, $today) = @_;
	local($date, $count) = $old =~ /(\d{8})(\d\d)/;
	local($new);
	$new = ($date < $today) ? $today . '00' : $date . ++$count;
	print STDERR "Replacing serial $old by $new in SOA of $ARGV\n";
	return $new;
}
