updateBEDate revision 290001
11539Srgrimes#! /usr/bin/env perl
21539Srgrimesuse warnings;
31539Srgrimesuse strict;
41539Srgrimes
51539Srgrimes# for each filename on the command line
61539Srgrimes# get the modtime
71539Srgrimes# make a backup of the file
81539Srgrimes# - error if there is already a backup?
91539Srgrimes# flush the  live version(?)
101539Srgrimes# start a line-by-line copy of the backup to the new file,
111539Srgrimes# doing the BeginDate/EndDate substitution
121539Srgrimes
131539Srgrimes# <!-- #BeginDate format:En1m -->3-oct-11  18:20<!-- #EndDate -->
141539Srgrimes# <!-- #BeginDate format:En2m -->01-Aug-2011  17:56<!-- #EndDate -->
151539Srgrimes# without the 'm' no minutes are included.
161539Srgrimes
171539Srgrimesmy $i;
181539Srgrimesmy $mod_time;
191539Srgrimesmy $stamp;
201539Srgrimesmy @m_abbr = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
21203964Simp
221539Srgrimesforeach ( @ARGV ) {
231539Srgrimes    $i = $_;
241539Srgrimes    $mod_time = (stat ($i))[9];
251539Srgrimes    $stamp = localtime($mod_time);
261539Srgrimes    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
271539Srgrimes                                                localtime($mod_time);
281539Srgrimes    $year += 1900;
291539Srgrimes
301539Srgrimes    # print "<$i> at <$stamp>\n";
311539Srgrimes
321539Srgrimes    open(my $IFILE, "<", $i) or die "Cannot open < $i: $!";
331539Srgrimes    open(my $OFILE, ">", $i.".new") or die "Cannot open > $i.new: $!";
341539Srgrimes    while(<$IFILE>) {
351539Srgrimes	if (/(.*<!--\s*#BeginDate\s*format:)(\S*)(\s*-->).*(<!--\s*#EndDate\s*-->.*)/) {
361539Srgrimes	    # print "Got: $_";
371539Srgrimes	    # print "as: <$1><$2><$3>...<$4>\n";
38164244Sjkoshy	    print { $OFILE } $1,$2,$3;
39164244Sjkoshy	    printf { $OFILE } "%s-%s-%s  %02d:%02d", $mday,$m_abbr[$mon],$year,$hour,$min;
401539Srgrimes	    print { $OFILE } $4,"\n";
411539Srgrimes	}
421539Srgrimes	else {
431539Srgrimes	    print { $OFILE } $_;
441539Srgrimes	}
45164244Sjkoshy    }
46164244Sjkoshy    close($IFILE);
471539Srgrimes    close($OFILE);
481539Srgrimes    #
491539Srgrimes    utime(time, $mod_time, "$i.new") || die "touch $i.new failed: $!";
501539Srgrimes    #
511539Srgrimes    rename $i,"$i.old" || die "rename $i,$i.old failed: $!";
521539Srgrimes    rename "$i.new",$i || die "rename $i.new,$i failed: $!";
531539Srgrimes}
541539Srgrimes