21bca1fbd8960a2f597319f17b44240c451f7013
[BackupPC.git] / makeDist
1 #!/usr/bin/env perl
2 #
3 # makeDist: Build a BackupPC distribution
4 #
5 # DESCRIPTION
6 #
7 #   This script should be run with no arguments to build a
8 #   distribution.  The $Version and $ReleaseDate should be
9 #   edited below to specify the version name and the release
10 #   date.  The distribution is createede in the sub-directory
11 #   dist.  The dsitribution is in the file name:
12 #
13 #           dist/BackupPC-$Version.tar.gz.
14 #
15 #   Often the language files are not up to date, and makeDist
16 #   exits after complaining about the lang files being inconsistent.
17 #   Use the -nolangCheck option to turn off that behavior.
18 #
19 # AUTHOR
20 #   Craig Barratt <cbarratt@users.sourceforge.net>
21 #
22 # COPYRIGHT
23 #   Copyright (C) 2001-2009  Craig Barratt
24 #
25 #   This program is free software; you can redistribute it and/or modify
26 #   it under the terms of the GNU General Public License as published by
27 #   the Free Software Foundation; either version 2 of the License, or
28 #   (at your option) any later version.
29 #
30 #   This program is distributed in the hope that it will be useful,
31 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
32 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33 #   GNU General Public License for more details.
34 #
35 #   You should have received a copy of the GNU General Public License
36 #   along with this program; if not, write to the Free Software
37 #   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
38 #
39 #========================================================================
40
41 use strict;
42 use File::Path;
43 use File::Copy;
44 use Getopt::Long;
45 use Data::Dumper;
46
47 use lib "./lib";
48 use BackupPC::Lib;
49 use BackupPC::Config::Meta qw(:all);
50
51 my $bpc;
52 die("BackupPC::Lib->new failed\n")
53         if ( !($bpc = BackupPC::Lib->new(".", ".", "./conf", 1)) );
54
55 umask(0022);
56
57 my @PerlSrc = qw(
58     bin/BackupPC
59     bin/BackupPC_archive
60     bin/BackupPC_archiveHost
61     bin/BackupPC_archiveStart
62     bin/BackupPC_attribPrint
63     bin/BackupPC_dump
64     bin/BackupPC_fixupBackupSummary
65     bin/BackupPC_link
66     bin/BackupPC_nightly
67     bin/BackupPC_restore
68     bin/BackupPC_sendEmail
69     bin/BackupPC_serverMesg
70     bin/BackupPC_trashClean
71     bin/BackupPC_tarExtract
72     bin/BackupPC_tarCreate
73     bin/BackupPC_tarPCCopy
74     bin/BackupPC_zipCreate
75     bin/BackupPC_zcat
76     lib/BackupPC/Attrib.pm
77     lib/BackupPC/Config.pm
78     lib/BackupPC/FileZIO.pm
79     lib/BackupPC/Lib.pm
80     lib/BackupPC/PoolWrite.pm
81     lib/BackupPC/Storage.pm
82     lib/BackupPC/View.pm
83     lib/BackupPC/CGI/AdminOptions.pm
84     lib/BackupPC/CGI/Archive.pm
85     lib/BackupPC/CGI/ArchiveInfo.pm
86     lib/BackupPC/CGI/Browse.pm
87     lib/BackupPC/CGI/DirHistory.pm
88     lib/BackupPC/CGI/EditConfig.pm
89     lib/BackupPC/CGI/EmailSummary.pm
90     lib/BackupPC/CGI/GeneralInfo.pm
91     lib/BackupPC/CGI/HostInfo.pm
92     lib/BackupPC/CGI/Lib.pm
93     lib/BackupPC/CGI/LOGlist.pm
94     lib/BackupPC/CGI/Queue.pm
95     lib/BackupPC/CGI/ReloadServer.pm
96     lib/BackupPC/CGI/RestoreFile.pm
97     lib/BackupPC/CGI/RestoreInfo.pm
98     lib/BackupPC/CGI/Restore.pm
99     lib/BackupPC/CGI/RSS.pm
100     lib/BackupPC/CGI/StartServer.pm
101     lib/BackupPC/CGI/StartStopBackup.pm
102     lib/BackupPC/CGI/StopServer.pm
103     lib/BackupPC/CGI/Summary.pm
104     lib/BackupPC/CGI/View.pm
105     lib/BackupPC/Config/Meta.pm
106     lib/BackupPC/Lang/de.pm
107     lib/BackupPC/Lang/en.pm
108     lib/BackupPC/Lang/es.pm
109     lib/BackupPC/Lang/fr.pm
110     lib/BackupPC/Lang/it.pm
111     lib/BackupPC/Lang/nl.pm
112     lib/BackupPC/Lang/pl.pm
113     lib/BackupPC/Lang/pt_br.pm
114     lib/BackupPC/Lang/zh_CN.pm
115     lib/BackupPC/Storage/Text.pm
116     lib/BackupPC/Xfer.pm
117     lib/BackupPC/Xfer/Archive.pm
118     lib/BackupPC/Xfer/Ftp.pm
119     lib/BackupPC/Xfer/Protocol.pm
120     lib/BackupPC/Xfer/Rsync.pm
121     lib/BackupPC/Xfer/RsyncDigest.pm
122     lib/BackupPC/Xfer/RsyncFileIO.pm
123     lib/BackupPC/Xfer/Smb.pm
124     lib/BackupPC/Xfer/Tar.pm
125     lib/BackupPC/Zip/FileMember.pm
126     lib/Net/FTP/AutoReconnect.pm
127     lib/Net/FTP/RetrHandle.pm
128     cgi-bin/BackupPC_Admin
129 );
130
131 my %opts;
132 $opts{langCheck}   = 1;
133 $opts{syntaxCheck} = 1;
134 if ( !GetOptions(
135                  \%opts,
136                  "langCheck!",
137                  "syntaxCheck!",
138                  "version=s",
139                  "releasedate=s",
140            ) || @ARGV != 0
141              || !defined($opts{version})
142         ) {
143     print STDERR <<EOF;
144 usage: $0 [--nolangCheck] [--nosyntaxCheck] [--releasedate 'DD MMM YYYY'] --version X.Y.Z
145 EOF
146     exit(1);
147 }
148
149 my($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime();
150 $year += 1900;
151
152 my @month_abbr  = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
153
154 my $Version     = $opts{version} || "3.2.0";
155 my $ReleaseDate = $opts{releasedate} || "$mday $month_abbr[$mon] $year";
156 my $DistDir     = "dist/BackupPC-$Version";
157
158 #
159 # Check config parameters
160 #
161 my $ConfVars = {};
162 my $errCnt;
163
164 $errCnt += CheckConfigParams("conf/config.pl", $ConfVars, 0);
165
166 $errCnt += CheckConfigParams("doc-src/BackupPC.pod", $ConfVars, 1);
167
168 $errCnt += CheckMetaDataVsConfig($ConfVars, "lib/BackupPC/Config/Meta.pm");
169
170 $errCnt += CheckEditorVsConfig($ConfVars, "lib/BackupPC/CGI/EditConfig.pm");
171
172 #
173 # These config parameters are not used in the code, so ignore them.
174 #
175 $ConfVars->{BackupPCUser} = 2;
176 $ConfVars->{CgiDir}       = 2;
177 $ConfVars->{TopDir}       = 2;
178 $ConfVars->{LogDir}       = 2;
179 $ConfVars->{ConfDir}      = 2;
180 $ConfVars->{InstallDir}   = 2;
181 $ConfVars->{CgiImageDir}  = 2;
182 $ConfVars->{ClientCharsetLegacy}  = 2;      # actually used
183
184 #
185 # These config parameters are used in the code to be backward compatible,
186 # but are not present in the current config file, so ignore them.
187 #
188 $ConfVars->{BlackoutHourBegin} = 2;
189 $ConfVars->{BlackoutHourEnd}   = 2;
190 $ConfVars->{BlackoutWeekDays}  = 2;
191 $ConfVars->{RsyncLogLevel}     = 2;
192
193 if ( $opts{syntaxCheck} ) {
194     system("perl -Ilib -c conf/config.pl >& /dev/null")
195             && die("$0: conf/config.pl contains a syntax error (or someone killed me)\n");
196 }
197 foreach my $file ( @PerlSrc ) {
198     if ( $opts{syntaxCheck} ) {
199         system("perl -Ilib -c $file >& /dev/null")
200             && die("$0: $file contains a syntax error (or someone killed me)\n");
201     }
202     #
203     # Skip checking of bundled libraries not explicitly part of
204     # BackupPC
205     #
206     next if ( $file =~ m/lib\/Net\/FTP/ );
207     $errCnt += CheckConfigParams($file, $ConfVars, 1);
208 }
209 if ( !$opts{langCheck} ) {
210     $errCnt += CheckLangUsage();
211     $errCnt += CheckLangTags();
212 }
213 if ( $errCnt ) {
214     print("Exiting because of errors\n");
215     exit(1)
216 }
217
218 $errCnt = 0;
219 foreach my $var ( sort(keys(%$ConfVars) ) ) {
220     next if ( $ConfVars->{$var} >= 2 || $var =~ /^\$/ );
221     printf("Unused config parameter $var\n");
222     $errCnt++;
223 }
224 if ( $errCnt ) {
225     print("Exiting because of errors\n");
226     exit(1)
227 }
228
229 rmtree($DistDir, 0, 0);
230 mkpath($DistDir, 0, 0777);
231
232 foreach my $dir ( qw(bin doc conf images init.d/src cgi-bin httpd/src
233                      lib/BackupPC/CGI
234                      lib/BackupPC/Config
235                      lib/BackupPC/Lang
236                      lib/BackupPC/Storage
237                      lib/BackupPC/Xfer
238                      lib/BackupPC/Zip
239                      lib/Net/FTP
240                 ) ) {
241     mkpath("$DistDir/$dir", 0, 0777);
242 }
243
244 my %ConfName;
245 my $ConfPod = config2pod();
246
247 rmtree("doc", 0, 0);
248 mkpath("doc", 0, 0777);
249 InstallFile("doc-src/BackupPC.pod", "doc/BackupPC.pod");
250
251 use Pod::Html;
252 pod2html("doc/BackupPC.pod",
253         "--backlink=Back to Top",
254         "--header",
255         "--title=BackupPC",
256         "--outfile=doc/BackupPC.html");
257
258 foreach my $file ( (@PerlSrc,
259             <images/*.gif>,
260             <images/*.png>,
261             <images/*.ico>,
262             qw(
263                 conf/config.pl
264                 conf/hosts
265                 conf/BackupPC_stnd.css
266                 conf/BackupPC_stnd_orig.css
267                 conf/sorttable.js
268                 init.d/README
269                 init.d/src/debian-backuppc
270                 init.d/src/freebsd-backuppc
271                 init.d/src/freebsd-backuppc2
272                 init.d/src/gentoo-backuppc
273                 init.d/src/gentoo-backuppc.conf
274                 init.d/src/linux-backuppc
275                 init.d/src/slackware-backuppc
276                 init.d/src/solaris-backuppc
277                 init.d/src/suse-backuppc
278                 httpd/src/BackupPC.conf
279                 doc/BackupPC.pod
280                 doc/BackupPC.html
281                 README
282                 LICENSE
283                 ChangeLog
284                 configure.pl
285         )) ) {
286     InstallFile("$file", "$DistDir/$file");
287 }
288 rmtree("doc", 0, 0);
289 system("cd dist ; tar zcf BackupPC-$Version.tar.gz BackupPC-$Version");
290 print("Distribution written to dist/BackupPC-$Version.tar.gz\n");
291 unlink("pod2htmd.x~~");
292 unlink("pod2htmi.x~~");
293 unlink("pod2htmd.tmp");
294 unlink("pod2htmi.tmp");
295
296 ###########################################################################
297 # Subroutines
298 ###########################################################################
299
300 sub InstallFile
301 {
302     my($file, $dest) = @_;
303
304     unlink($dest) if ( -d $dest );
305     if ( $file =~ /\.gif/ || $file =~ /\.png/ || $file =~ /\.ico/ ) {
306         die("can't copy($file, $dest)\n") unless copy($file, $dest);
307     } else {
308         open(FILE, $file)   || die("can't open $file for reading\n");
309         open(OUT, ">$dest") || die("can't open $dest for writing\n");
310         binmode(FILE);
311         binmode(OUT);
312         while ( <FILE> ) {
313             s/^([#*\s]+)Version \d+\.\d+[\.\w]*, released \d+ \w+ \d{4}\.?/$1Version __VERSION__, released __RELEASEDATE__./;
314             s/__VERSION__/$Version/g;
315             s/__RELEASEDATE__/$ReleaseDate/g;
316             if ( $file =~ /BackupPC\.html$/ ) {
317                 #
318                 # fixup for conf links
319                 #
320                 if ( !/a name="_conf/i ) {
321                     s/\$Conf{([^}]*)}/
322                         defined($ConfName{$1})
323                             ? "\L<a href=\"#$ConfName{$1}\">\E\$Conf{$1}<\/a>"
324                             : "\$Conf{$1}"/eg;
325                 }
326                 s/^<DD>/<DD><P>/;
327                 s/^<li><\/li>/<li>/;
328             }
329             if ( /__CONFIGPOD__/ ) {
330                 print OUT $ConfPod;
331             } elsif ( /^use lib ".*BackupPC\/lib";/
332                     || /^use lib "\/home\/pcbackup\/install\/lib";/ ) {
333                 print OUT "use lib \"__INSTALLDIR__/lib\";\n";
334             } elsif ( $file =~ /Lib.pm/ && /^(\s*\$topDir\s*=\s*)'.*'(\s*if\s.*)/ ) {
335                 print OUT "$1'__TOPDIR__'$2\n";
336             } elsif ( $file =~ /Lib.pm/ && /^(\s*\$installDir\s*=\s*)'.*'(\s*if\s.*)/ ) {
337                 print OUT "$1'__INSTALLDIR__'$2\n";
338             } elsif ( $file =~ /Lib.pm/ && /^(\s*ConfDir\s*=\>\s*\$confDir eq.*)'.*'(.*)/ ) {
339                 print OUT "$1'__CONFDIR__'$2\n";
340             } elsif ( $file =~ /Lib.pm/ && /^(\s*my \$useFHS\s*=\s*)\d;/ ) {
341                 print OUT "${1}0;\n";
342             } elsif ( $file =~ /Lib.pm/ && /(.*Version *=> .*)'[\w\d\.]+',/ ) {
343                 print OUT "$1'$Version',\n";
344             } elsif ( $file =~ /configure.pl/ && /__CONFIGURE_BIN_LIST__/ ) {
345                 print OUT "        ", join("\n        ", grep(/^bin\//, @PerlSrc)), "\n";
346             } elsif ( $file =~ /configure.pl/ && /__CONFIGURE_LIB_LIST__/ ) {
347                 print OUT "        ", join("\n        ", grep(/^lib\//, @PerlSrc)), "\n";
348             } elsif ( $file =~ /BackupPC_Admin/ && /(my *\$installDir *= *)'.*'/ ) {
349                 print OUT "$1'__INSTALLDIR__/lib';\n";
350             } else {
351                 print OUT;
352             }
353         }
354         close(FILE);
355         close(OUT);
356     }
357     if ( -x $file ) {
358         chmod(0555, $dest);
359     } else {
360         chmod(0444, $dest);
361     }
362 }
363
364 sub config2pod
365 {
366     open(C, "conf/config.pl") || die("can't open conf/config.pl");
367     binmode(C);
368     my($str, $out, $getHdr, @conf);
369     my $first = 1;
370     while ( <C> ) {
371         chomp;
372         s/ +$//;
373         if ( /^#########################/ ) {
374             if ( $getHdr ) {
375                 $str =~ s/\n.*//sg;
376                 $out .= "=back\n\n" if ( !$first );
377                 $out .= "=head2 $str\n\n=over 4\n\n";
378                 $str = "";
379                 $first = 0;
380             }
381             $getHdr = !$getHdr;
382             next;
383         }
384         if ( /^#/ ) {
385             s/# ?//;
386             next if ( $str eq "" && /^$/ );
387             $str .= $_ . "\n";
388             $str .= "\n" if ( $str =~ /examples?:\n$/i );
389         } elsif ( /^\$Conf{([^}]*)/ ) {
390             my $var = $1;
391             s/  +/ /g;
392             s/;\s*#.*/;/;
393             if ( !s/\[$/[ ... ];/ && !s/<<'EOF'/.../ ) {
394                 s/([^;])\s*$/$1 .../;
395             }
396             push(@conf, $_);
397             my $text = "_conf_${var}_";
398             $text =~ s{[\W\s]}{_}g;
399             $ConfName{$var} = "$text";
400         } elsif ( /^$/ ) {
401             if ( $str ne "" && @conf ) {
402                 $out .= "=item " . join("\n\n=item ", @conf) . "\n\n";
403                 $out .= $str;
404                 $out .= "\n" if ( $str !~ /\n$/ );
405             }
406             $str = "";
407             @conf = ();
408         }
409     }
410     if ( $str ne "" && @conf ) {
411         $out .= "=item " . join("\n\n=item ", @conf) . "\n\n";
412         $out .= $str;
413         $out .= "\n" if ( $str !~ /\n$/ );
414     }
415     $out .= "=back\n\n" if ( !$first );
416     return $out;
417 }
418
419 sub CheckConfigParams
420 {
421     my($file, $vars, $check) = @_;
422     my $errors;
423
424     open(F, $file) || die("can't open $file\n");
425     binmode(F);
426     if ( $check ) {
427         while ( <F> ) {
428             s/\$(self|bpc)->{Conf}{([^}\$]+)}/if ( !defined($vars->{$2}) ) {
429                     print("Unexpected Conf var $2 in $file\n");
430                     $errors++;
431                 } else {
432                     $vars->{$2}++;
433                 }/eg;
434             s/\$[Cc]onf(?:->)?{([^}\$]+)}/if ( !defined($vars->{$1}) ) {
435                     print("Unexpected Conf var $1 in $file\n");
436                     $errors++;
437                 } else {
438                     $vars->{$1}++;
439                 }/eg;
440             s/UserCommandRun\("([^"]*)"/if ( !defined($vars->{$1}) ) {
441                     print("Unexpected Conf var $1 in $file\n");
442                     $errors++;
443                 } else {
444                     $vars->{$1}++;
445                 }/eg;
446         }
447     } else {
448         while ( <F> ) {
449             s/^[^#]*\$self->{Conf}{([^}]*)/$vars->{$1} = 1;/eg;
450             s/^[^#]*\$Conf{([^}]*)/$vars->{$1} = 1;/eg;
451         }
452     }
453     close(F);
454     return $errors;
455 }
456
457 sub CheckMetaDataVsConfig
458 {
459     my($confVars, $file) = @_;
460     my $done = {};
461     my $errors;
462
463     #
464     # Check that the meta file mentions all the config
465     # parameters
466     #
467     open(F, $file) || die("can't open $file");
468
469     while ( <F> ) {
470         next if ( !/^\s{4}(\w+)\s+=>/ );
471         if ( $confVars->{$1} ) {
472             $done->{$1} = 1;
473             next;
474         }
475         next if ( $1 eq "Hosts" );
476         print("$file has $1 but missing from conf/config.pl\n");
477         $errors++;
478     }
479     close(F);
480     foreach my $v ( keys(%$confVars) ) {
481         next if ( $done->{$v} );
482         print("$file missing $v from conf/config.pl\n");
483         $errors++;
484     }
485
486     #
487     # Do extra checks that the CgiUserConfigEdit hash in the Meta
488     # file matches the config file
489     #
490     foreach my $p ( keys(%{$ConfigMeta{CgiUserConfigEdit}{child}}) ) {
491         if ( !defined($bpc->{Conf}{CgiUserConfigEdit}{$p}) ) {
492             print("lib/BackupPC/Config/Meta.pm has $p in CgiUserConfigEdit,"
493                 . " but conf/config.pl CgiUserConfigEdit does not\n");
494             $errors++;
495         }
496     }
497     foreach my $p ( keys(%{$bpc->{Conf}{CgiUserConfigEdit}}) ) {
498         if ( !defined($ConfigMeta{CgiUserConfigEdit}{child}{$p}) ) {
499             print("conf/config.pl CgiUserConfigEdit has $p, but"
500                 . " lib/BackupPC/Config/Meta.pm does not\n");
501             $errors++;
502         }
503     }
504     return $errors;
505 }
506
507 sub CheckEditorVsConfig
508 {
509     my($confVars, $file) = @_;
510     my $done = {};
511     my $errors;
512
513     #
514     # Check that the config editor file mentions all the config
515     # parameters
516     #
517     open(F, $file) || die("can't open $file");
518
519     while ( <F> ) {
520         next if ( !/name\s*=>\s*"(\w+)"/ );
521         if ( $confVars->{$1} ) {
522             $done->{$1} = 1;
523             next;
524         }
525         next if ( $1 eq "Hosts" );
526         print("$file has $1 but missing from conf/config.pl\n");
527         $errors++;
528     }
529     close(F);
530     foreach my $v ( keys(%$confVars) ) {
531         next if ( $done->{$v} );
532         print("$file missing $v from conf/config.pl\n");
533         $errors++;
534     }
535     return $errors;
536 }
537
538 #
539 # Make sure that every lang variable in cgi-bin/BackupPC_Admin matches
540 # the strings in each lib/BackupPC/Lang/*.pm file.  This makes sure
541 # we didn't miss any translations in any of the languages.
542 #
543 sub CheckLangUsage
544 {
545     my $errors;
546     my $vars = {};
547
548     foreach my $file ( (
549                 qw(cgi-bin/BackupPC_Admin bin/BackupPC_sendEmail),
550                 <lib/BackupPC/CGI/*pm>,
551                 <lib/BackupPC/Lang/en.pm>,
552             ) ) {
553         open(F, $file) || die("can't open $file");
554         binmode(F);
555         while ( <F> ) {
556             next if ( /^\s*#/ );
557             s/\$Lang->{([^}]*)}/$vars->{$1} = 1;/eg;
558             s/(text|comment)\s*=>\s*"(CfgEdit_.*)"/$vars->{$2} = 1;/eg;
559         }
560         close(F);
561     }
562
563     foreach my $f ( <lib/BackupPC/Lang/*.pm> ) {
564         my $done = {};
565         open(F, $f) || die("can't open $f\n");
566         binmode(F);
567         while ( <F> ) {
568             s/#.*//g;
569             s/\$Lang{([^}]*)}/
570                     my $var = $1;
571                     next if ( $var =~ m{^(Reason_|Status_|backupType_|Disabled_)} );
572                     next if ( $var eq "Documentation" );
573                     if ( !defined($vars->{$var}) ) {
574                         print("Unexpected Lang var $var in $f\n");
575                         $errors++;
576                     } else {
577                         $done->{$var} = 1;
578                     }/eg;
579         }
580         close(F);
581         foreach my $v ( keys(%$vars) ) {
582             #
583             # skip "variables" with "$", since they are like expressions
584             #
585             next if ( $v =~ /\$/ );
586             if ( !defined($done->{$v}) ) {
587                 print("Lang var $v missing from $f\n");
588                 $errors++;
589             }
590         }
591     }
592     return $errors;
593 }
594
595 #
596 # Pedantically check that all the html tags in each language file
597 # match.
598 #
599 sub CheckLangTags
600 {
601     my($en, $enVars) = LangParse("lib/BackupPC/Lang/en.pm");
602     my($errors);
603
604     foreach my $lang ( qw(fr.pm de.pm es.pm it.pm nl.pm pl.pm pt_br.pm zh_CN.pm) ) {
605         my($d, $dVars) = LangParse("lib/BackupPC/Lang/$lang");
606         foreach my $v1 ( @$en ) {
607             my $v2 = shift(@$d);
608             if ( $v1->{var} ne $v2->{var} ) {
609                 print("Botch: got $lang var $v2->{var} vs en.pm $v1->{var}\n");
610                 exit;
611             }
612             my $t1 = LangTextStrip($v1->{val});
613             my $t2 = LangTextStrip($v2->{val});
614             if ( $t1 ne $t2 ) {
615                 my $i;
616                 for ( $i = 0 ; $i < length($t1) ; $i++ ) {
617                     last if ( substr($t1, 0, $i) ne substr($t2, 0, $i) );
618                 }
619                 print("$v1->{var}: ($i) got en.pm $t1\nvs $lang $t2\n\n");
620                 $errors++;
621             }
622         }
623     }
624     return $errors;
625 }
626
627 sub LangTextStrip
628 {
629     my($t) = @_;
630
631     $t = "" if ( $t !~ /<.*>/ );
632     $t =~ s/^[^<]*</</s;
633     $t =~ s/([}>])[^<]*</$1</g;
634     $t =~ s/>[^<]*$/>/;
635     $t =~ s/(value=)"[^"]*"/$1""/sg;
636     $t =~ s/({h[12]\()"[^"]*"/$1""/g;
637     $t =~ s/ENG[\s\n]*//sg;
638     $t =~ s/^(<<EOF;\n)[^<]*/$1/g;
639     return $t;
640 }
641
642 sub LangParse
643 {
644     my($file) = @_;
645     open(C, $file) || die("can't open $file");
646     binmode(C);
647     my($out, @lang, $var);
648     my $comment = 1;
649     my $allVars = {};
650     my $endLine = undef;
651     while ( <C> ) {
652         if ( /^#/ && !defined($endLine) ) {
653             if ( $comment ) {
654                 $out .= $_;
655             } else {
656                 if ( $out ne "" ) {
657                     $allVars->{$var} = @lang if ( defined($var) );
658                     push(@lang, {
659                         text => $out,
660                         var => $var,
661                     });
662                 }
663                 $var = undef;
664                 $comment = 1;
665                 $out = $_;
666             }
667         } elsif ( /^\s*\$Lang\{([^}]*)/ ) {
668             $comment = 0;
669             if ( defined($var) ) {
670                 $allVars->{$var} = @lang if ( defined($var) );
671                 push(@lang, {
672                     text => $out,
673                     var => $var,
674                 });
675                 $out = $_;
676             } else {
677                 $out .= $_;
678             }
679             $var = $1;
680             $endLine = $1 if ( /^\s*\$Lang\{[^}]*} *= *<<(.*);/ );
681             $endLine = $1 if ( /^\s*\$Lang\{[^}]*} *= *<<'(.*)';/ );
682         } else {
683             $endLine = undef if ( defined($endLine) && /^\Q$endLine[\n\r]*$/ );
684             $out .= $_;
685         }
686     }
687     if ( $out ne "" ) {
688         $allVars->{$var} = @lang if ( defined($var) );
689         push(@lang, {
690             text => $out,
691             var  => $var,
692         });
693     }
694     close(C);
695     foreach my $v ( @lang ) {
696         if ( $v->{text} =~ /\$Lang{$v->{var}}\s*=\s*(.*)/s ) {
697             $v->{val} = $1;
698         }
699     }
700     return (\@lang, $allVars);
701 }