* Major changes from Ryan Kucera to add style sheets to the CGI
[BackupPC.git] / makeDist
1 #!/bin/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 # AUTHOR
16 #   Craig Barratt <cbarratt@users.sourceforge.net>
17 #
18 # COPYRIGHT
19 #   Copyright (C) 2001-2003  Craig Barratt
20 #
21 #   This program is free software; you can redistribute it and/or modify
22 #   it under the terms of the GNU General Public License as published by
23 #   the Free Software Foundation; either version 2 of the License, or
24 #   (at your option) any later version.
25 #
26 #   This program is distributed in the hope that it will be useful,
27 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
28 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29 #   GNU General Public License for more details.
30 #
31 #   You should have received a copy of the GNU General Public License
32 #   along with this program; if not, write to the Free Software
33 #   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
34 #
35 #========================================================================
36 #
37
38 use strict;
39 use File::Path;
40 use File::Copy;
41
42 umask(0022);
43
44 my $Version     = "2.1.0_CVS";
45 my $ReleaseDate = "3 Jul 2003";
46 my $DistDir     = "dist/BackupPC-$Version";
47
48 my @PerlSrc = qw(
49     bin/BackupPC
50     bin/BackupPC_archive
51     bin/BackupPC_archivecd
52     bin/BackupPC_archivetape
53     bin/BackupPC_dump
54     bin/BackupPC_link
55     bin/BackupPC_nightly
56     bin/BackupPC_restore
57     bin/BackupPC_sendEmail
58     bin/BackupPC_serverMesg
59     bin/BackupPC_trashClean
60     bin/BackupPC_tarExtract
61     bin/BackupPC_tarCreate
62     bin/BackupPC_compressPool
63     bin/BackupPC_zipCreate
64     bin/BackupPC_zcat
65     lib/BackupPC/Attrib.pm
66     lib/BackupPC/FileZIO.pm
67     lib/BackupPC/Lib.pm
68     lib/BackupPC/PoolWrite.pm
69     lib/BackupPC/View.pm
70     lib/BackupPC/CGI/AdminOptions.pm
71     lib/BackupPC/CGI/Archive.pm
72     lib/BackupPC/CGI/ArchiveInfo.pm
73     lib/BackupPC/CGI/Browse.pm
74     lib/BackupPC/CGI/DirHistory.pm
75     lib/BackupPC/CGI/EmailSummary.pm
76     lib/BackupPC/CGI/GeneralInfo.pm
77     lib/BackupPC/CGI/HostInfo.pm
78     lib/BackupPC/CGI/Lib.pm
79     lib/BackupPC/CGI/LOGlist.pm
80     lib/BackupPC/CGI/Queue.pm
81     lib/BackupPC/CGI/ReloadServer.pm
82     lib/BackupPC/CGI/RestoreFile.pm
83     lib/BackupPC/CGI/RestoreInfo.pm
84     lib/BackupPC/CGI/Restore.pm
85     lib/BackupPC/CGI/StartServer.pm
86     lib/BackupPC/CGI/StartStopBackup.pm
87     lib/BackupPC/CGI/StopServer.pm
88     lib/BackupPC/CGI/Summary.pm
89     lib/BackupPC/CGI/View.pm
90     lib/BackupPC/Lang/de.pm
91     lib/BackupPC/Lang/en.pm
92     lib/BackupPC/Lang/es.pm
93     lib/BackupPC/Lang/fr.pm
94     lib/BackupPC/Xfer/Archive.pm
95     lib/BackupPC/Xfer/Smb.pm
96     lib/BackupPC/Xfer/Tar.pm
97     lib/BackupPC/Xfer/Rsync.pm
98     lib/BackupPC/Xfer/RsyncFileIO.pm
99     lib/BackupPC/Zip/FileMember.pm
100     cgi-bin/BackupPC_Admin
101 );
102
103 #
104 # Check config parameters
105 #
106 my $ConfVars = {};
107 my $errCnt;
108
109 $errCnt += CheckConfigParams("conf/config.pl", $ConfVars, 0);
110 $ConfVars->{BackupPCUser} = 2;
111 $ConfVars->{CgiDir} = 2;
112 $ConfVars->{InstallDir} = 2;
113 $ConfVars->{CgiImageDir} = 2;
114 foreach my $file ( @PerlSrc ) {
115     $errCnt += CheckConfigParams($file, $ConfVars, 1);
116 }
117 $errCnt += CheckLangUsage();
118 exit(1) if ( $errCnt );
119
120 $errCnt = 0;
121 foreach my $var ( sort(keys(%$ConfVars) ) ) {
122     next if ( $ConfVars->{$var} >= 2 || $var =~ /^\$/ );
123     printf("Unused config parameter $var\n");
124     $errCnt++;
125 }
126 exit(1) if ( $errCnt );
127
128 rmtree($DistDir, 0, 0);
129 mkpath($DistDir, 0, 0777);
130
131 foreach my $dir ( qw(bin doc conf images init.d/src cgi-bin
132                      lib/BackupPC/CGI
133                      lib/BackupPC/Lang
134                      lib/BackupPC/Xfer
135                      lib/BackupPC/Zip
136                 ) ) {
137     mkpath("$DistDir/$dir", 0, 0777);
138 }
139
140 my %ConfName;
141 my $ConfPod = config2pod();
142 rmtree("doc", 0, 0);
143 mkpath("doc", 0, 0777);
144 InstallFile("doc-src/BackupPC.pod", "doc/BackupPC.pod");
145
146 use Pod::Html;
147 pod2html("doc/BackupPC.pod",
148         "--backlink=Back to Top",
149         "--header",
150         "--title=BackupPC",
151         "--outfile=doc/BackupPC.html");
152
153 foreach my $file ( (@PerlSrc,
154             <images/*>,
155             qw(
156                 conf/config.pl
157                 conf/hosts
158                 init.d/README
159                 init.d/src/debian-backuppc
160                 init.d/src/gentoo-backuppc
161                 init.d/src/gentoo-backuppc.conf
162                 init.d/src/linux-backuppc
163                 init.d/src/solaris-backuppc
164                 init.d/src/suse-backuppc
165                 doc/BackupPC.pod
166                 doc/BackupPC.html
167                 README
168                 LICENSE
169                 ChangeLog
170                 configure.pl
171         )) ) {
172     InstallFile("$file", "$DistDir/$file");
173 }
174 rmtree("doc", 0, 0);
175 system("cd dist ; tar zcf BackupPC-$Version.tar.gz BackupPC-$Version");
176 print("Distribution written to dist/BackupPC-$Version.tar.gz\n");
177 unlink("pod2htmd.x~~");
178 unlink("pod2htmi.x~~");
179
180 ###########################################################################
181 # Subroutines
182 ###########################################################################
183
184 sub InstallFile
185 {
186     my($file, $dest) = @_;
187
188     unlink($dest) if ( -d $dest );
189     if ( $file =~ /\.gif/ ) {
190         die("can't copy($file, $dest)\n") unless copy($file, $dest);
191     } else {
192         open(FILE, $file)   || die("can't open $file for reading\n");
193         open(OUT, ">$dest") || die("can't open $dest for writing\n");
194         binmode(FILE);
195         binmode(OUT);
196         while ( <FILE> ) {
197             s/^# *Version \d+\.\d+[\.\w]*, released \d+ \w+ \d{4}\.?/# Version __VERSION__, released __RELEASEDATE__./;
198             s/__VERSION__/$Version/g;
199             s/__RELEASEDATE__/$ReleaseDate/g;
200             if ( $file =~ /BackupPC\.html$/ && !/A NAME="item_%24Conf/ ) {
201                 s/\$Conf{([^}]*)}/
202                         defined($ConfName{$1})
203                             ? "<A HREF=\"#$ConfName{$1}\">\$Conf{$1}<\/A>"
204                             : "\$Conf{$1}"/eg;
205             }
206             if ( /__CONFIGPOD__/ ) {
207                 print OUT $ConfPod;
208             } elsif ( /^use lib ".*BackupPC\/lib";/
209                     || /^use lib "\/home\/pcbackup\/install\/lib";/ ) {
210                 print OUT "use lib \"__INSTALLDIR__/lib\";\n";
211             } elsif ( $file =~ /Lib.pm/ && /(.*TopDir *=> .*)'.*',/ ) {
212                 print OUT "$1'__TOPDIR__',\n";
213             } elsif ( $file =~ /Lib.pm/ && /(.*Version *=> .*)'[\w\d\.]+',/ ) {
214                 print OUT "$1'$Version',\n";
215             } elsif ( $file =~ /Lib.pm/ && /(.*BinDir *=> .*)'.*',/ ) {
216                 print OUT "$1'__INSTALLDIR__',\n";
217             } elsif ( $file =~ /Lib.pm/ && /(.*LibDir *=> .*)'.*',/ ) {
218                 print OUT "$1'__INSTALLDIR__',\n";
219             } else {
220                 print OUT;
221             }
222         }
223         close(FILE);
224         close(OUT);
225     }
226     if ( -x $file ) {
227         chmod(0555, $dest);
228     } else {
229         chmod(0444, $dest);
230     }
231 }
232
233 sub config2pod
234 {
235     open(C, "conf/config.pl") || die("can't open conf/config.pl");
236     binmode(C);
237     my($str, $out, $getHdr, @conf);
238     my $first = 1;
239     while ( <C> ) {
240         chomp;
241         s/ +$//;
242         if ( /^#########################/ ) {
243             if ( $getHdr ) {
244                 $str =~ s/\n.*//sg;
245                 $out .= "=back\n\n" if ( !$first );
246                 $out .= "=head2 $str\n\n=over 4\n\n";
247                 $str = "";
248                 $first = 0;
249             }
250             $getHdr = !$getHdr;
251             next;
252         }
253         if ( /^#/ ) {
254             s/# ?//;
255             next if ( $str eq "" && /^$/ );
256             $str .= $_ . "\n";
257             $str .= "\n" if ( $str =~ /examples?:\n$/i );
258         } elsif ( /^\$Conf{([^}]*)/ ) {
259             my $var = $1;
260             s/  +/ /g;
261             s/;\s*#.*/;/;
262             if ( !s/\[$/[ ... ];/ && !s/<<'EOF'/.../ ) {
263                 s/([^;])\s*$/$1 .../;
264             }
265             push(@conf, $_);
266             my $text = $_;
267             $text =~ s/\s+/_/sg;
268             $text =~ s{(\W)}{sprintf("%%%02X", ord($1) )}gxe;
269             $text = substr($text, 0, 50);
270             $ConfName{$var} = "item_$text";
271         } elsif ( /^$/ ) {
272             if ( $str ne "" && @conf ) {
273                 $out .= "=item " . join("\n\n=item ", @conf) . "\n\n";
274                 $out .= $str;
275                 $out .= "\n" if ( $str !~ /\n$/ );
276             }
277             $str = "";
278             @conf = ();
279         }
280     }
281     if ( $str ne "" && @conf ) {
282         $out .= "=item " . join("\n\n=item ", @conf) . "\n\n";
283         $out .= $str;
284         $out .= "\n" if ( $str !~ /\n$/ );
285     }
286     $out .= "=back\n\n" if ( !$first );
287     return $out;
288 }
289
290 sub CheckConfigParams
291 {
292     my($file, $vars, $check) = @_;
293     my $errors;
294
295     open(F, $file) || die("can't open $file\n");
296     binmode(F);
297     if ( $check ) {
298         while ( <F> ) {
299             s/\$(self|bpc)->{Conf}{([^}\$]+)}/if ( !defined($vars->{$2}) ) {
300                     print("Unexpected Conf var $2 in $file\n");
301                     $errors++;
302                 } else {
303                     $vars->{$2}++;
304                 }/eg;
305             s/\$[Cc]onf(?:->)?{([^}\$]+)}/if ( !defined($vars->{$1}) ) {
306                     print("Unexpected Conf var $1 in $file\n");
307                     $errors++;
308                 } else {
309                     $vars->{$1}++;
310                 }/eg;
311             s/UserCommandRun\("([^"]*)"\)/if ( !defined($vars->{$1}) ) {
312                     print("Unexpected Conf var $1 in $file\n");
313                     $errors++;
314                 } else {
315                     $vars->{$1}++;
316                 }/eg;
317         }
318     } else {
319         while ( <F> ) {
320             s/^[^#]*\$self->{Conf}{([^}]*)/$vars->{$1} = 1;/eg;
321             s/^[^#]*\$Conf{([^}]*)/$vars->{$1} = 1;/eg;
322         }
323     }
324     close(F);
325     return $errors;
326 }
327
328 #
329 # Make sure that every lang variable in cgi-bin/BackupPC_Admin matches
330 # the strings in each lib/BackupPC/Lang/*.pm file.  This makes sure
331 # we didn't miss any translations in any of the languages.
332 #
333 sub CheckLangUsage
334 {
335     my $errors;
336     my $vars = {};
337
338     foreach my $file ( (
339                 qw(cgi-bin/BackupPC_Admin bin/BackupPC_sendEmail),
340                 <lib/BackupPC/CGI/*pm>
341             ) ) {
342         open(F, $file) || die("can't open $file");
343         binmode(F);
344         while ( <F> ) {
345             s/\$Lang->{([^}]*)}/$vars->{$1} = 1;/eg;
346         }
347         close(F);
348     }
349     # CB: disable other lang checks for now
350     #
351     # foreach my $f ( <lib/BackupPC/Lang/*.pm> ) {
352     #
353     foreach my $f ( <lib/BackupPC/Lang/en.pm> ) {
354         my $done = {};
355         open(F, $f) || die("can't open $f\n");
356         binmode(F);
357         while ( <F> ) {
358             s/#.*//g;
359             s/\$Lang{([^}]*)}/
360                     my $var = $1;
361                     next if ( $var =~ m{^(Reason_|Status_|backupType_)} );
362                     if ( !defined($vars->{$var}) ) {
363                         print("Unexpected Lang var $var in $f\n");
364                         $errors++;
365                     } else {
366                         $done->{$var} = 1;
367                     }/eg;
368         }
369         close(F);
370         foreach my $v ( keys(%$vars) ) {
371             #
372             # skip "variables" with "$", since they are like expressions
373             #
374             next if ( $v =~ /\$/ );
375             if ( !defined($done->{$v}) ) {
376                 print("Lang var $v missing from $f\n");
377                 $errors++;
378             }
379         }
380     }
381     return $errors;
382 }