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