xref: /freebsd/contrib/ntp/scripts/build/updateBEDate (revision 7fdf597e96a02165cfe22ff357b857d5fa15ed8a)
1#! /usr/bin/env perl
2use warnings;
3use strict;
4
5# for each filename on the command line
6# get the modtime
7# make a backup of the file
8# - error if there is already a backup?
9# flush the  live version(?)
10# start a line-by-line copy of the backup to the new file,
11# doing the BeginDate/EndDate substitution
12
13# <!-- #BeginDate format:En1m -->3-oct-11  18:20<!-- #EndDate -->
14# <!-- #BeginDate format:En2m -->01-Aug-2011  17:56<!-- #EndDate -->
15# without the 'm' no minutes are included.
16
17my $i;
18my $mod_time;
19my $stamp;
20my @m_abbr = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
21
22foreach ( @ARGV ) {
23    $i = $_;
24    $mod_time = (stat ($i))[9];
25    $stamp = localtime($mod_time);
26    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
27                                                localtime($mod_time);
28    $year += 1900;
29
30    # print "<$i> at <$stamp>\n";
31
32    open(my $IFILE, "<", $i) or die "Cannot open < $i: $!";
33    open(my $OFILE, ">", $i.".new") or die "Cannot open > $i.new: $!";
34    while(<$IFILE>) {
35	if (/(.*<!--\s*#BeginDate\s*format:)(\S*)(\s*-->).*(<!--\s*#EndDate\s*-->.*)/) {
36	    # print "Got: $_";
37	    # print "as: <$1><$2><$3>...<$4>\n";
38	    print { $OFILE } $1,$2,$3;
39	    printf { $OFILE } "%s-%s-%s  %02d:%02d", $mday,$m_abbr[$mon],$year,$hour,$min;
40	    print { $OFILE } $4,"\n";
41	}
42	else {
43	    print { $OFILE } $_;
44	}
45    }
46    close($IFILE);
47    close($OFILE);
48    #
49    utime(time, $mod_time, "$i.new") || die "touch $i.new failed: $!";
50    #
51    rename $i,"$i.old" || die "rename $i,$i.old failed: $!";
52    rename "$i.new",$i || die "rename $i.new,$i failed: $!";
53}
54