forked from backuppc/backuppc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
makeDist
executable file
·768 lines (717 loc) · 22.3 KB
/
makeDist
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
#!/usr/bin/env perl
#
# makeDist: Build a BackupPC distribution
#
# DESCRIPTION
#
# This script builds a distribution. Arguments
# specify the release date and version:
#
# makeDist [--nolangCheck] [--nosyntaxCheck] [--releasedate 'DD MMM YYYY'] --version X.Y.Z
#
# Example:
#
# makeDist --releasedate '1 Dec 2013' --version 4.0.0alpha3
#
# makeDist merges the version number, release date and turns all the
# library paths etc back into the symbolic form (eg: __INSTALLDIR__)
# so that configure.pl will do the right thing.
#
# Sometime the language files are not up to date, and makeDist exits
# after complaining about the lang files being inconsistent. Use
# the --nolangCheck option to turn off that checking.
#
# By default makeDist does a syntax check on all the source files.
# If you want to skip that you can use the --nosyntaxCheck option, eg:
#
# makeDist --nolangCheck --nosyntaxCheck --releasedate '1 Dec 2013' --version 4.0.0alpha3
#
# The distribution is in the file name: dist/BackupPC-$Version.tar.gz.
#
# AUTHOR
# Craig Barratt <[email protected]>
#
# COPYRIGHT
# Copyright (C) 2001-2015 Craig Barratt
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#========================================================================
use strict;
use File::Path;
use File::Copy;
use Getopt::Long;
use Data::Dumper;
use lib "./lib";
use BackupPC::Lib;
use BackupPC::Config::Meta qw(:all);
my $bpc;
die("BackupPC::Lib->new failed\n")
if ( !($bpc = BackupPC::Lib->new(".", ".", "./conf", 1)) );
umask(0022);
my @PerlSrc = qw(
bin/BackupPC
bin/BackupPC_Admin_SCGI
bin/BackupPC_archive
bin/BackupPC_archiveHost
bin/BackupPC_archiveStart
bin/BackupPC_attribPrint
bin/BackupPC_backupDelete
bin/BackupPC_backupDuplicate
bin/BackupPC_dump
bin/BackupPC_fixupBackupSummary
bin/BackupPC_fsck
bin/BackupPC_ls
bin/BackupPC_migrateV3toV4
bin/BackupPC_nightly
bin/BackupPC_poolCntPrint
bin/BackupPC_refCountUpdate
bin/BackupPC_restore
bin/BackupPC_rrdUpdate
bin/BackupPC_sendEmail
bin/BackupPC_serverMesg
bin/BackupPC_tarCreate
bin/BackupPC_tarExtract
bin/BackupPC_zcat
bin/BackupPC_zipCreate
lib/BackupPC/Config/Meta.pm
lib/BackupPC/DirOps.pm
lib/BackupPC/Lib.pm
lib/BackupPC/Storage.pm
lib/BackupPC/View.pm
lib/BackupPC/Xfer/Archive.pm
lib/BackupPC/Xfer/Ftp.pm
lib/BackupPC/Xfer/Protocol.pm
lib/BackupPC/Xfer/Rsync.pm
lib/BackupPC/Xfer/Smb.pm
lib/BackupPC/Xfer/Tar.pm
lib/BackupPC/Xfer.pm
lib/BackupPC/Zip/FileMember.pm
lib/Net/FTP/AutoReconnect.pm
lib/Net/FTP/RetrHandle.pm
lib/BackupPC/CGI/AdminOptions.pm
lib/BackupPC/CGI/Archive.pm
lib/BackupPC/CGI/ArchiveInfo.pm
lib/BackupPC/CGI/Browse.pm
lib/BackupPC/CGI/DeleteBackup.pm
lib/BackupPC/CGI/DirHistory.pm
lib/BackupPC/CGI/EditConfig.pm
lib/BackupPC/CGI/EmailSummary.pm
lib/BackupPC/CGI/GeneralInfo.pm
lib/BackupPC/CGI/HostInfo.pm
lib/BackupPC/CGI/Lib.pm
lib/BackupPC/CGI/LOGlist.pm
lib/BackupPC/CGI/Metrics.pm
lib/BackupPC/CGI/Queue.pm
lib/BackupPC/CGI/ReloadServer.pm
lib/BackupPC/CGI/Restore.pm
lib/BackupPC/CGI/RestoreFile.pm
lib/BackupPC/CGI/RestoreInfo.pm
lib/BackupPC/CGI/StartServer.pm
lib/BackupPC/CGI/StartStopBackup.pm
lib/BackupPC/CGI/StopServer.pm
lib/BackupPC/CGI/Summary.pm
lib/BackupPC/CGI/View.pm
lib/BackupPC/Lang/cz.pm
lib/BackupPC/Lang/de.pm
lib/BackupPC/Lang/en.pm
lib/BackupPC/Lang/es.pm
lib/BackupPC/Lang/fr.pm
lib/BackupPC/Lang/it.pm
lib/BackupPC/Lang/ja.pm
lib/BackupPC/Lang/nl.pm
lib/BackupPC/Lang/pl.pm
lib/BackupPC/Lang/pt_br.pm
lib/BackupPC/Lang/ru.pm
lib/BackupPC/Lang/uk.pm
lib/BackupPC/Lang/zh_CN.pm
lib/BackupPC/Storage/Text.pm
cgi-bin/BackupPC_Admin
);
my %opts;
$opts{langCheck} = 1;
$opts{syntaxCheck} = 1;
if ( !GetOptions(\%opts, "langCheck!", "syntaxCheck!", "version=s", "releasedate=s",)
|| @ARGV != 0
|| !defined($opts{version}) ) {
print STDERR <<EOF;
usage: $0 [--nolangCheck] [--nosyntaxCheck] [--releasedate 'DD MMM YYYY'] --version X.Y.Z
EOF
exit(1);
}
my($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime();
$year += 1900;
my @month_abbr = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
my $Version = $opts{version} || "3.2.0";
my $ReleaseDate = $opts{releasedate} || "$mday $month_abbr[$mon] $year";
my $DistDir = "dist/BackupPC-$Version";
#
# Check config parameters
#
my $ConfVars = {};
my $errCnt;
$errCnt += CheckConfigParams("conf/config.pl", $ConfVars, 0);
$errCnt += CheckConfigParams("doc-src/BackupPC.pod", $ConfVars, 1);
$errCnt += CheckMetaDataVsConfig($ConfVars, "lib/BackupPC/Config/Meta.pm");
$errCnt += CheckEditorVsConfig($ConfVars, "lib/BackupPC/CGI/EditConfig.pm");
#
# These config parameters are not used in the code, so ignore them.
#
$ConfVars->{BackupPCUser} = 2;
$ConfVars->{CgiDir} = 2;
$ConfVars->{TopDir} = 2;
$ConfVars->{LogDir} = 2;
$ConfVars->{RunDir} = 2;
$ConfVars->{ConfDir} = 2;
$ConfVars->{InstallDir} = 2;
$ConfVars->{CgiImageDir} = 2;
#
# These config parameters are used in the code
#
$ConfVars->{ClientCharsetLegacy} = 2;
$ConfVars->{DfCmd} = 2;
$ConfVars->{DfInodeUsageCmd} = 2;
#
# These config parameters are used in the code to be backward compatible,
# but are not present in the current config file, so ignore them.
#
$ConfVars->{BlackoutHourBegin} = 2;
$ConfVars->{BlackoutHourEnd} = 2;
$ConfVars->{BlackoutWeekDays} = 2;
$ConfVars->{RsyncLogLevel} = 2;
if ( $opts{syntaxCheck} ) {
system("perl -Ilib -c conf/config.pl 2> /dev/null")
&& die("$0: conf/config.pl contains an error (or someone killed me)\n");
system("perl -Ilib -c configure.pl 2> /dev/null")
&& die("$0: configure.pl contains an error (or someone killed me)\n");
}
foreach my $file ( @PerlSrc ) {
if ( $opts{syntaxCheck} ) {
system("perl -Ilib -c $file 2> /dev/null")
&& die("$0: $file contains an error (or someone killed me)\n");
}
#
# Skip checking of bundled libraries not explicitly part of
# BackupPC
#
next if ( $file =~ m/lib\/Net\/FTP/ );
$errCnt += CheckConfigParams($file, $ConfVars, 1);
}
if ( $opts{langCheck} ) {
$errCnt += CheckLangUsage();
$errCnt += CheckLangTags(\@PerlSrc);
}
if ( $errCnt ) {
print("Exiting because of errors\n");
exit(1);
}
$errCnt = 0;
foreach my $var ( sort(keys(%$ConfVars)) ) {
next if ( $ConfVars->{$var} >= 2 || $var =~ /^\$/ );
printf("Unused config parameter $var\n");
$errCnt++;
}
if ( $errCnt ) {
print("Exiting because of errors\n");
exit(1);
}
rmtree($DistDir, 0, 0);
mkpath($DistDir, 0, 0777);
foreach my $dir ( qw(bin doc conf images systemd/src/init.d cgi-bin httpd/src
lib/BackupPC/CGI
lib/BackupPC/Config
lib/BackupPC/Lang
lib/BackupPC/Storage
lib/BackupPC/Xfer
lib/BackupPC/Zip
lib/Net/FTP
) ) {
mkpath("$DistDir/$dir", 0, 0777);
}
my %ConfName;
my $ConfPod = config2pod();
rmtree("doc", 0, 0);
mkpath("doc", 0, 0777);
InstallFile("doc-src/BackupPC.pod", "doc/BackupPC.pod");
use Pod::Html;
pod2html(
"doc/BackupPC.pod",
#"--backlink=Back to Top",
"--header",
"--title=BackupPC",
"--outfile=doc/BackupPC.html"
);
foreach my $file ( (
@PerlSrc,
<images/*.gif>,
<images/*.svg>,
<images/*.png>,
<images/*.ico>,
qw(
conf/BackupPC_2020_mod.css
conf/BackupPC_mod.css
conf/BackupPC_retro_v2.css
conf/BackupPC_retro_v3.css
conf/BackupPC_stnd.css
conf/config.pl
conf/hosts
conf/sorttable.js
systemd/README
systemd/src/backuppc.service
systemd/src/init.d/debian-backuppc
systemd/src/init.d/freebsd-backuppc
systemd/src/init.d/freebsd-backuppc2
systemd/src/init.d/gentoo-backuppc
systemd/src/init.d/gentoo-backuppc.conf
systemd/src/init.d/linux-backuppc
systemd/src/init.d/slackware-backuppc
systemd/src/init.d/solaris-backuppc
systemd/src/init.d/suse-backuppc
systemd/src/init.d/ubuntu-backuppc
httpd/src/BackupPC.conf
doc/BackupPC.pod
doc/BackupPC.html
README.md
LICENSE
ChangeLog
configure.pl
)
) ) {
InstallFile("$file", "$DistDir/$file");
}
rmtree("doc", 0, 0);
system("cd dist ; tar zcf BackupPC-$Version.tar.gz BackupPC-$Version");
print("Distribution written to dist/BackupPC-$Version.tar.gz\n");
unlink("pod2htmd.x~~");
unlink("pod2htmi.x~~");
unlink("pod2htmd.tmp");
unlink("pod2htmi.tmp");
###########################################################################
# Subroutines
###########################################################################
sub InstallFile
{
my($file, $dest) = @_;
unlink($dest) if ( -d $dest );
if ( $file =~ /\.(?:ico|gif|svg|png)$/ ) {
die("can't copy($file, $dest)\n") unless copy($file, $dest);
} else {
my $lineCnt = 0;
open(FILE, $file) || die("can't open $file for reading\n");
open(OUT, ">$dest") || die("can't open $dest for writing\n");
binmode(FILE);
binmode(OUT);
while ( <FILE> ) {
s/^([#*\s]+)Version \d+\.\d+[\.\w]*, released \d+ \w+ \d{4}\.?/$1Version __VERSION__, released __RELEASEDATE__./;
s/__VERSION__/$Version/g;
s/__RELEASEDATE__/$ReleaseDate/g;
s/^([#*\s]+)Copyright\s+\(C\)\s+(\d{4})-\d{4}\s+Craig\s+Barratt/$1Copyright (C) $2-$year Craig Barratt/i;
if ( $file =~ /BackupPC\.html$/ ) {
s/^<body>$/<body id="manual">/;
#
# fixup for conf links
#
if ( /<dt id="Conf-/ ) {
s/<dt id=("Conf-.*)>\$Conf\{([^}]*)}/
defined($ConfName{$2})
? "\L<dt id=\"$ConfName{$2}\">\E\$Conf{$2}<\/a>"
: "<dt id=$1>\$Conf{$2}"/eg;
} else {
s/\$Conf\{([^}]*)}/
defined($ConfName{$1})
? "\L<a href=\"#$ConfName{$1}\">\E\$Conf{$1}<\/a>"
: "\$Conf{$1}"/eg;
}
s{^(<dt id="_conf.*</dt>)}{<strong>$1</strong>};
s/^<DD>/<DD><P>/;
s/^<li><\/li>/<li>/;
#
# add diveder lines before <h1>
#
s{^<h1}{<hr />\n<h1}g;
}
if ( $lineCnt == 0 && m{^#!/usr/local/bin/perl} ) {
#
# configure.pl overwrites the perl path on install, but do this to keep the
# distribution path backward compatible.
#
s{/usr/local/bin/perl}{/usr/bin/perl};
}
if ( /__CONFIGPOD__/ ) {
print OUT $ConfPod;
} elsif ( /^use lib ".*BackupPC\/lib";/
|| /^use lib "\/home\/pcbackup\/install\/lib";/ ) {
print OUT "use lib \"__INSTALLDIR__/lib\";\n";
} elsif ( $file =~ /Lib.pm/ && /^(\s*\$topDir\s*=\s*)'.*'(\s*if\s.*)/ ) {
print OUT "$1'__TOPDIR__'$2\n";
} elsif ( $file =~ /Lib.pm/ && /^(\s*\$installDir\s*=\s*)'.*'(\s*if\s.*)/ ) {
print OUT "$1'__INSTALLDIR__'$2\n";
} elsif ( $file =~ /Lib.pm/ && /^(\s*ConfDir\s*=\>\s*\$confDir eq.*)'.*'(.*)/ ) {
print OUT "$1'__CONFDIR__'$2\n";
} elsif ( $file =~ /Lib.pm/ && /^(\s*my \$useFHS\s*=\s*)\d;/ ) {
print OUT "${1}0;\n";
} elsif ( $file =~ /Lib.pm/ && /(.*Version *=> *)'[\w\d\.]+',?(.*)/ ) {
print OUT "$1'$Version'$2\n";
} elsif ( $file =~ /configure.pl/ && /__CONFIGURE_BIN_LIST__/ ) {
print OUT " ", join("\n ", grep(/^bin\//, @PerlSrc)), "\n";
} elsif ( $file =~ /configure.pl/ && /__CONFIGURE_LIB_LIST__/ ) {
print OUT " ", join("\n ", grep(/^lib\//, @PerlSrc)), "\n";
} elsif ( $file =~ /BackupPC_Admin/ && /(my *\$installDir *= *)'.*'/ ) {
print OUT "$1'__INSTALLDIR__/lib';\n";
} else {
print OUT;
}
$lineCnt++;
}
close(FILE);
close(OUT);
}
if ( -x $file ) {
chmod(0555, $dest);
} else {
chmod(0444, $dest);
}
}
sub config2pod
{
open(C, "conf/config.pl") || die("can't open conf/config.pl");
binmode(C);
my($str, $out, $getHdr, @conf);
my $first = 1;
while ( <C> ) {
chomp;
s/ +$//;
if ( /^#########################/ ) {
if ( $getHdr ) {
$str =~ s/\n.*//sg;
$out .= "=back\n\n" if ( !$first );
$out .= "=head2 $str\n\n=over 4\n\n";
$str = "";
$first = 0;
}
$getHdr = !$getHdr;
next;
}
if ( /^#/ ) {
s/# ?//;
next if ( $str eq "" && /^$/ );
$str .= $_ . "\n";
$str .= "\n" if ( $str =~ /examples?:\n$/i );
} elsif ( /^\$Conf\{([^}]*)/ ) {
my $var = $1;
s/ +/ /g;
s/;\s*#.*/;/;
if ( !s/\[$/[ ... ];/ && !s/<<'EOF'/.../ ) {
s/([^;])\s*$/$1 .../;
}
push(@conf, $_);
my $text = "_conf_${var}_";
$text =~ s{[\W\s]}{_}g;
$ConfName{$var} = "$text";
} elsif ( /^$/ ) {
if ( $str ne "" && @conf ) {
$out .= "=item " . join("\n\n=item ", @conf) . "\n\n";
$out .= $str;
$out .= "\n" if ( $str !~ /\n$/ );
}
$str = "";
@conf = ();
}
}
if ( $str ne "" && @conf ) {
$out .= "=item " . join("\n\n=item ", @conf) . "\n\n";
$out .= $str;
$out .= "\n" if ( $str !~ /\n$/ );
}
$out .= "=back\n\n" if ( !$first );
return $out;
}
sub CheckConfigParams
{
my($file, $vars, $check) = @_;
my $errors;
open(F, $file) || die("can't open $file\n");
binmode(F);
if ( $check ) {
while ( <F> ) {
s/\$(self|bpc)->\{Conf}\{([^}\$]+)}/if ( !defined($vars->{$2}) ) {
print("Unexpected Conf var $2 in $file\n");
$errors++;
} else {
$vars->{$2}++;
}/eg;
s/\$(?:HostConf|Conf|conf)(?:->)?\{([^}\$]+)}/if ( !defined($vars->{$1}) ) {
print("Unexpected Conf var $1 in $file\n");
$errors++;
} else {
$vars->{$1}++;
}/eg;
s/UserCommandRun\("([^"]*)"/if ( !defined($vars->{$1}) ) {
print("Unexpected Conf var $1 in $file\n");
$errors++;
} else {
$vars->{$1}++;
}/eg;
}
} else {
while ( <F> ) {
s/^[^#]*\$self->\{Conf}\{([^}]*)/$vars->{$1} = 1;/eg;
s/^[^#]*\$Conf\{([^}]*)/$vars->{$1} = 1;/eg;
}
}
close(F);
return $errors;
}
sub CheckMetaDataVsConfig
{
my($confVars, $file) = @_;
my $done = {};
my $errors;
#
# Check that the meta file mentions all the config
# parameters
#
open(F, $file) || die("can't open $file");
while ( <F> ) {
next if ( !/^\s{4}(\w+)\s+=>/ );
if ( $confVars->{$1} ) {
$done->{$1} = 1;
next;
}
next if ( $1 eq "Hosts" );
print("$file has $1 but missing from conf/config.pl\n");
$errors++;
}
close(F);
foreach my $v ( keys(%$confVars) ) {
next if ( $done->{$v} );
print("$file missing $v from conf/config.pl\n");
$errors++;
}
#
# Do extra checks that the CgiUserConfigEdit hash in the Meta
# file matches the config file
#
foreach my $p ( keys(%{$ConfigMeta{CgiUserConfigEdit}{child}}) ) {
if ( !defined($bpc->{Conf}{CgiUserConfigEdit}{$p}) ) {
print( "lib/BackupPC/Config/Meta.pm has $p in CgiUserConfigEdit,"
. " but conf/config.pl CgiUserConfigEdit does not\n");
$errors++;
}
}
foreach my $p ( keys(%{$bpc->{Conf}{CgiUserConfigEdit}}) ) {
if ( !defined($ConfigMeta{CgiUserConfigEdit}{child}{$p}) ) {
print("conf/config.pl CgiUserConfigEdit has $p, but lib/BackupPC/Config/Meta.pm does not\n");
$errors++;
}
}
return $errors;
}
sub CheckEditorVsConfig
{
my($confVars, $file) = @_;
my $done = {};
my $errors;
#
# Check that the config editor file mentions all the config
# parameters
#
open(F, $file) || die("can't open $file");
while ( <F> ) {
next if ( !/name\s*=>\s*"(\w+)"/ );
if ( $confVars->{$1} ) {
$done->{$1} = 1;
next;
}
next if ( $1 eq "Hosts" );
print("$file has $1 but missing from conf/config.pl\n");
$errors++;
}
close(F);
foreach my $v ( keys(%$confVars) ) {
next if ( $done->{$v} );
print("$file missing $v from conf/config.pl\n");
$errors++;
}
return $errors;
}
#
# Make sure that every lang variable in cgi-bin/BackupPC_Admin matches
# the strings in each lib/BackupPC/Lang/*.pm file. This makes sure
# we didn't miss any translations in any of the languages.
#
sub CheckLangUsage
{
my $errors;
my $vars = {};
foreach my $file ( (
qw(cgi-bin/BackupPC_Admin bin/BackupPC_sendEmail lib/BackupPC/Lang/en.pm lib/BackupPC/Config/Meta.pm),
<lib/BackupPC/CGI/*pm>,
) ) {
open(F, $file) || die("can't open $file");
binmode(F);
while ( <F> ) {
next if ( /^\s*#/ );
s/\$Lang->\{([^}]*)}/$vars->{$1} = 1;/eg;
s/(text|comment|keyText)\s*=>\s*"(CfgEdit_.*)"/$vars->{$2} = 1;/eg;
}
close(F);
}
foreach my $f ( <lib/BackupPC/Lang/*.pm> ) {
my $done = {};
open(F, $f) || die("can't open $f\n");
binmode(F);
while ( <F> ) {
s/#.*//g;
s/\$Lang\{([^}]*)}/
my $var = $1;
next if ( $var =~ m{^(Reason_|Status_|backupType_|Disabled_)} );
next if ( $var eq "Documentation" );
if ( !defined($vars->{$var}) ) {
print("Unexpected Lang var $var in $f\n");
$errors++;
} else {
$done->{$var} = 1;
}/eg;
}
close(F);
foreach my $v ( keys(%$vars) ) {
#
# skip "variables" with "$", since they are like expressions
#
next if ( $v =~ /\$/ );
if ( !defined($done->{$v}) ) {
print("Lang var $v missing from $f\n");
$errors++;
}
}
}
return $errors;
}
#
# Pedantically check that all the html tags in each language file
# match.
#
sub CheckLangTags
{
my($perlSrc) = @_;
my($en, $enVars) = LangParse("lib/BackupPC/Lang/en.pm");
my($errors);
foreach my $srcFile ( @$perlSrc ) {
next if ( $srcFile !~ m{lib/BackupPC/Lang/(.*)} );
my $lang = $1;
next if ( $lang eq "en.pm" );
my($d, $dVars) = LangParse("lib/BackupPC/Lang/$lang");
foreach my $v1 ( @$en ) {
my $v2 = shift(@$d);
if ( $v1->{var} ne $v2->{var} ) {
print("Botch: got $lang var $v2->{var} vs en.pm $v1->{var}\n");
exit 1;
}
my $t1 = LangTextStrip($v1->{val});
my $t2 = LangTextStrip($v2->{val});
if ( $t1 ne $t2 ) {
my $i;
for ( $i = 0 ; $i < length($t1) ; $i++ ) {
last if ( substr($t1, 0, $i) ne substr($t2, 0, $i) );
}
print("$v1->{var}: ($i) got en.pm $t1\nvs $lang $t2\n\n");
$errors++;
}
}
}
return $errors;
}
sub LangTextStrip
{
my($t) = @_;
$t = "" if ( $t !~ /<.*>/ );
$t =~ s/^[^<]*</</s;
$t =~ s/([}>])[^<]*</$1</g;
$t =~ s/>[^<]*$/>/;
$t =~ s/(value=)"[^"]*"/$1""/sg;
$t =~ s/(\{h[12]\()"[^"]*"/$1""/g;
$t =~ s/ENG[\s\n]*//sg;
$t =~ s/^(<<EOF;\n)[^<]*/$1/g;
return $t;
}
sub LangParse
{
my($file) = @_;
open(C, $file) || die("can't open $file");
binmode(C);
my($out, @lang, $var);
my $comment = 1;
my $allVars = {};
my $endLine = undef;
while ( <C> ) {
if ( /^#/ && !defined($endLine) ) {
if ( $comment ) {
$out .= $_;
} else {
if ( $out ne "" ) {
$allVars->{$var} = @lang if ( defined($var) );
push(
@lang,
{
text => $out,
var => $var,
}
);
}
$var = undef;
$comment = 1;
$out = $_;
}
} elsif ( /^\s*\$Lang\{([^}]*)/ ) {
$comment = 0;
if ( defined($var) ) {
$allVars->{$var} = @lang if ( defined($var) );
push(
@lang,
{
text => $out,
var => $var,
}
);
$out = $_;
} else {
$out .= $_;
}
$var = $1;
$endLine = $1 if ( /^\s*\$Lang\{[^}]*} *= *<<(.*);/ );
$endLine = $1 if ( /^\s*\$Lang\{[^}]*} *= *<<'(.*)';/ );
} else {
$endLine = undef if ( defined($endLine) && /^\Q$endLine[\n\r]*$/ );
$out .= $_;
}
}
if ( $out ne "" ) {
$allVars->{$var} = @lang if ( defined($var) );
push(
@lang,
{
text => $out,
var => $var,
}
);
}
close(C);
foreach my $v ( @lang ) {
if ( $v->{text} =~ /\$Lang\{$v->\{var}}\s*=\s*(.*)/s ) {
$v->{val} = $1;
}
}
return (\@lang, $allVars);
}