1275970Scy#! /usr/bin/env perl
2275970Scyuse warnings;
3275970Scyuse strict;
4275970Scy
5275970Scy# for each filename on the command line
6275970Scy# get the modtime
7275970Scy# make a backup of the file
8275970Scy# - error if there is already a backup?
9275970Scy# flush the  live version(?)
10275970Scy# start a line-by-line copy of the backup to the new file,
11275970Scy# doing the BeginDate/EndDate substitution
12275970Scy
13275970Scy# <!-- #BeginDate format:En1m -->3-oct-11  18:20<!-- #EndDate -->
14275970Scy# <!-- #BeginDate format:En2m -->01-Aug-2011  17:56<!-- #EndDate -->
15275970Scy# without the 'm' no minutes are included.
16275970Scy
17275970Scymy $i;
18275970Scymy $mod_time;
19275970Scymy $stamp;
20275970Scymy @m_abbr = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
21275970Scy
22275970Scyforeach ( @ARGV ) {
23275970Scy    $i = $_;
24275970Scy    $mod_time = (stat ($i))[9];
25275970Scy    $stamp = localtime($mod_time);
26275970Scy    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
27275970Scy                                                localtime($mod_time);
28275970Scy    $year += 1900;
29275970Scy
30275970Scy    # print "<$i> at <$stamp>\n";
31275970Scy
32275970Scy    open(my $IFILE, "<", $i) or die "Cannot open < $i: $!";
33275970Scy    open(my $OFILE, ">", $i.".new") or die "Cannot open > $i.new: $!";
34275970Scy    while(<$IFILE>) {
35275970Scy	if (/(.*<!--\s*#BeginDate\s*format:)(\S*)(\s*-->).*(<!--\s*#EndDate\s*-->.*)/) {
36275970Scy	    # print "Got: $_";
37275970Scy	    # print "as: <$1><$2><$3>...<$4>\n";
38275970Scy	    print { $OFILE } $1,$2,$3;
39275970Scy	    printf { $OFILE } "%s-%s-%s  %02d:%02d", $mday,$m_abbr[$mon],$year,$hour,$min;
40275970Scy	    print { $OFILE } $4,"\n";
41275970Scy	}
42275970Scy	else {
43275970Scy	    print { $OFILE } $_;
44275970Scy	}
45275970Scy    }
46275970Scy    close($IFILE);
47275970Scy    close($OFILE);
48275970Scy    #
49275970Scy    utime(time, $mod_time, "$i.new") || die "touch $i.new failed: $!";
50275970Scy    #
51275970Scy    rename $i,"$i.old" || die "rename $i,$i.old failed: $!";
52275970Scy    rename "$i.new",$i || die "rename $i.new,$i failed: $!";
53275970Scy}
54