-
Notifications
You must be signed in to change notification settings - Fork 41
/
update.pl
executable file
·149 lines (124 loc) · 3.53 KB
/
update.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/usr/bin/perl
use DirHandle;
$DEBUG=0;
$dir="."; # start in current directory...
# $docroot="/trains/JMRI/site.new";
$docroot="/";
$logoalt = "\"JMRI Logo\"";
# absolute path from docroot, without the leading slash...
$logo = "images/logo-jmri.gif";
$TDIR="./templates";
$H = "Header";
$F = "Footer";
$S = "Style";
$L = "Logo";
sub processfile() {
my ($f) = @_;
my $old = $f;
my $new = $f;
$old =~ s/html$/html.bak/;
$new =~ s/html$/html.new/;
die "can not create unique name for $f (old=$old, new=$new)\n"
if ($new eq $old || $new eq $f || $old eq $f);
my $regen=0;
foreach my $x ("$TDIR/$H", "$TDIR/$F", "$TDIR/$S", "$TDIR/$L") {
printf ("%25s %8.6f\t%25s %8.6f\n", $x, (-M $x), $f, (-M $f))
if ($DEBUG);
if (-M $x < -M $f) {
print "Regenerating $f because $x is newer\n"
if ($DEBUG);
$regen=1;
last;
}
}
if ($regen == 1) {
print "Processing $f ...";
# Reset
my $state="copy";
my @boilerplate = ();
my %defines=undef;
# set up default values
$defines{"docroot"} = $docroot;
$defines{"logo"} = $logo;
$defines{"logoalt"} = $logoalt;
open(OLD, "< $f") or die "can't open $old: $!";
open(NEW, "> $new") or die "can't open $new: $!";
while (<OLD>) {
if ($state eq "copy") {
#<!-- Define tag value -->
if ( /^<!-- Define ([a-zA-Z0-9_-]+)\s(.*) -->/ ) {
$defines{$1} = $2;
print NEW $_;
} elsif (/^<!-- Header -->/) {
print("[Header]");
$lookfor="<!-- \/Header -->";
$state="delete";
@boilerplate=@HC;
} elsif (/^<!-- Footer -->/) {
print("[Footer]");
$lookfor="<!-- \/Footer -->";
$state="delete";
@boilerplate=@FC;
} elsif (/<!-- Logo -->/) {
print("[Logo]");
$lookfor="<!-- \/Logo -->";
$state="delete";
@boilerplate=@LC;
} elsif (/<!-- Style -->/) {
print("[Style]");
$lookfor="<!-- \/Style -->";
$state="delete";
@boilerplate=@SC;
} else {
print NEW $_;
}
} elsif ($state eq "delete") {
if (/$lookfor/) {
# grab a copy of the boilerplate
my $c = join "", @boilerplate;
# and substitute all the ${tags} found in it
# (undefined tags are not changed)
foreach $tag (keys %defines) {
my $val = $defines{$tag};
$c =~ s|\${$tag}|$val|g;
}
print NEW $c;
$state="copy";
}
}
}
close(OLD) or die "can't close $old: $!";
close(NEW) or die "can't close $new: $!";
print("[backup]");
rename($f, $old) or die "can't rename $f to $old: $!";
print("[rename]");
rename($new, $f) or die "can't rename $new to $f: $!";
printf(" Done\n");
}
}
sub getfilenames() {
my ($d) = @_;
my @f = ();
my $file;
my $dh = DirHandle->new($d) or die "Can't open $d : $!\n";
while( defined($file = $dh->read()) ) {
my $filename = "$d/$file";
next if (($file =~ /^\.$/) || ($file =~ /^\.\.$/));
if (-d $filename) {
push (@f, &getfilenames($filename));
} elsif ($file =~ /\.html$/i) {
push(@f, $filename);
}
# else Ignored
}
return @f;
}
open(F, "< $TDIR/$H") or die "can't open $H: $!"; @HC = <F>; close(F);
open(F, "< $TDIR/$F") or die "can't open $F: $!"; @FC = <F>; close(F);
open(F, "< $TDIR/$S") or die "can't open $S: $!"; @SC = <F>; close(F);
open(F, "< $TDIR/$L") or die "can't open $L: $!"; @LC = <F>; close(F);
# @files = ( "index.html" );
@files = &getfilenames($dir);
foreach $f (@files) {
&processfile($f);
}