+ 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/CGI/*pm>,
+ <lib/BackupPC/Lang/en.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)\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($en, $enVars) = LangParse("lib/BackupPC/Lang/en.pm");
+ my($errors);
+
+ foreach my $lang ( qw(cz.pm fr.pm de.pm es.pm it.pm nl.pm pl.pm pt_br.pm zh_CN.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;
+ }
+ 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);