4fe4061376c29e5835e710498c270320a6ac79ba
[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.0.0beta1";
45 my $ReleaseDate = "30 Mar 2003";
46 my $DistDir     = "dist/BackupPC-$Version";
47
48 my @PerlSrc = qw(
49     bin/BackupPC
50     bin/BackupPC_dump
51     bin/BackupPC_link
52     bin/BackupPC_nightly
53     bin/BackupPC_restore
54     bin/BackupPC_sendEmail
55     bin/BackupPC_serverMesg
56     bin/BackupPC_trashClean
57     bin/BackupPC_tarExtract
58     bin/BackupPC_tarCreate
59     bin/BackupPC_compressPool
60     bin/BackupPC_zipCreate
61     bin/BackupPC_zcat
62     lib/BackupPC/Attrib.pm
63     lib/BackupPC/FileZIO.pm
64     lib/BackupPC/Lib.pm
65     lib/BackupPC/Lang/en.pm
66     lib/BackupPC/Lang/fr.pm
67     lib/BackupPC/Lang/es.pm
68     lib/BackupPC/PoolWrite.pm
69     lib/BackupPC/View.pm
70     lib/BackupPC/Xfer/Smb.pm
71     lib/BackupPC/Xfer/Tar.pm
72     lib/BackupPC/Xfer/Rsync.pm
73     lib/BackupPC/Xfer/RsyncFileIO.pm
74     lib/BackupPC/Zip/FileMember.pm
75     cgi-bin/BackupPC_Admin
76 );
77
78 #
79 # Check config parameters
80 #
81 my $ConfVars = {};
82 my $errCnt;
83
84 $errCnt += CheckConfigParams("conf/config.pl", $ConfVars, 0);
85 $ConfVars->{BackupPCUser} = 2;
86 $ConfVars->{CgiDir} = 2;
87 $ConfVars->{InstallDir} = 2;
88 $ConfVars->{CgiImageDir} = 2;
89 foreach my $file ( @PerlSrc ) {
90     $errCnt += CheckConfigParams($file, $ConfVars, 1);
91 }
92 $errCnt += CheckLangUsage();
93 exit(1) if ( $errCnt );
94
95 foreach my $var ( sort(keys(%$ConfVars) ) ) {
96     next if ( $ConfVars->{$var} >= 2 || $var =~ /^\$/ );
97     printf("Unused config parameter $var\n");
98     $errCnt++;
99 }
100 exit(1) if ( $errCnt );
101
102 rmtree($DistDir, 0, 0);
103 mkpath($DistDir, 0, 0777);
104
105 foreach my $dir ( qw(bin lib/BackupPC/Xfer lib/BackupPC/Zip lib/BackupPC/Lang
106                      doc conf images init.d/src cgi-bin) ) {
107     mkpath("$DistDir/$dir", 0, 0777);
108 }
109
110 my %ConfName;
111 my $ConfPod = config2pod();
112 rmtree("doc", 0, 0);
113 mkpath("doc", 0, 0777);
114 InstallFile("doc-src/BackupPC.pod", "doc/BackupPC.pod");
115
116 use Pod::Html;
117 pod2html("doc/BackupPC.pod",
118         "--backlink=Back to Top",
119         "--header",
120         "--title=BackupPC",
121         "--outfile=doc/BackupPC.html");
122
123 foreach my $file ( (@PerlSrc,
124             <images/*>,
125             qw(
126                 conf/config.pl
127                 conf/hosts
128                 init.d/README
129                 init.d/src/debian-backuppc
130                 init.d/src/linux-backuppc
131                 init.d/src/solaris-backuppc
132                 doc/BackupPC.pod
133                 doc/BackupPC.html
134                 README
135                 LICENSE
136                 ChangeLog
137                 configure.pl
138         )) ) {
139     InstallFile("$file", "$DistDir/$file");
140 }
141 rmtree("doc", 0, 0);
142 system("cd dist ; tar zcf BackupPC-$Version.tar.gz BackupPC-$Version");
143 print("Distribution written to dist/BackupPC-$Version.tar.gz\n");
144 unlink("pod2htmd.x~~");
145 unlink("pod2htmi.x~~");
146
147 ###########################################################################
148 # Subroutines
149 ###########################################################################
150
151 sub InstallFile
152 {
153     my($file, $dest) = @_;
154
155     unlink($dest) if ( -d $dest );
156     if ( $file =~ /\.gif/ ) {
157         die("can't copy($file, $dest)\n") unless copy($file, $dest);
158     } else {
159         open(FILE, $file)   || die("can't open $file for reading\n");
160         open(OUT, ">$dest") || die("can't open $dest for writing\n");
161         while ( <FILE> ) {
162             s/^# *Version \d+\.\d+[\.\w]*, released \d+ \w+ \d{4}\.?/# Version __VERSION__, released __RELEASEDATE__./;
163             s/__VERSION__/$Version/g;
164             s/__RELEASEDATE__/$ReleaseDate/g;
165             if ( $file =~ /BackupPC\.html$/ && !/A NAME="item_%24Conf/ ) {
166                 s/\$Conf{([^}]*)}/
167                         defined($ConfName{$1})
168                             ? "<A HREF=\"#$ConfName{$1}\">\$Conf{$1}<\/A>"
169                             : "\$Conf{$1}"/eg;
170             }
171             if ( /__CONFIGPOD__/ ) {
172                 print OUT $ConfPod;
173             } elsif ( /^use lib ".*BackupPC\/lib";/
174                     || /^use lib "\/home\/pcbackup\/install\/lib";/ ) {
175                 print OUT "use lib \"__INSTALLDIR__/lib\";\n";
176             } elsif ( $file =~ /Lib.pm/ && /(.*TopDir *=> .*)'.*',/ ) {
177                 print OUT "$1'__TOPDIR__',\n";
178             } elsif ( $file =~ /Lib.pm/ && /(.*Version *=> .*)'[\w\d\.]+',/ ) {
179                 print OUT "$1'$Version',\n";
180             } elsif ( $file =~ /Lib.pm/ && /(.*BinDir *=> .*)'.*',/ ) {
181                 print OUT "$1'__INSTALLDIR__',\n";
182             } elsif ( $file =~ /Lib.pm/ && /(.*LibDir *=> .*)'.*',/ ) {
183                 print OUT "$1'__INSTALLDIR__',\n";
184             } else {
185                 print OUT;
186             }
187         }
188         close(FILE);
189         close(OUT);
190     }
191     if ( -x $file ) {
192         chmod(0555, $dest);
193     } else {
194         chmod(0444, $dest);
195     }
196 }
197
198 sub config2pod
199 {
200     open(C, "conf/config.pl") || die("can't open conf/config.pl");
201     my($str, $out, $getHdr, @conf);
202     my $first = 1;
203     while ( <C> ) {
204         chomp;
205         s/ +$//;
206         if ( /^#########################/ ) {
207             if ( $getHdr ) {
208                 $str =~ s/\n.*//sg;
209                 $out .= "=back\n\n" if ( !$first );
210                 $out .= "=head2 $str\n\n=over 4\n\n";
211                 $str = "";
212                 $first = 0;
213             }
214             $getHdr = !$getHdr;
215             next;
216         }
217         if ( /^#/ ) {
218             s/# ?//;
219             next if ( $str eq "" && /^$/ );
220             $str .= $_ . "\n";
221             $str .= "\n" if ( $str =~ /examples?:\n$/i );
222         } elsif ( /^\$Conf{([^}]*)/ ) {
223             my $var = $1;
224             s/  +/ /g;
225             s/;\s*#.*/;/;
226             if ( !s/\[$/[ ... ];/ && !s/<<'EOF'/.../ ) {
227                 s/([^;])\s*$/$1 .../;
228             }
229             push(@conf, $_);
230             my $text = $_;
231             $text =~ s/\s+/_/sg;
232             $text =~ s{(\W)}{sprintf("%%%02X", ord($1) )}gxe;
233             $text = substr($text, 0, 50);
234             $ConfName{$var} = "item_$text";
235         } elsif ( /^$/ ) {
236             if ( $str ne "" && @conf ) {
237                 $out .= "=item " . join("\n\n=item ", @conf) . "\n\n";
238                 $out .= $str;
239                 $out .= "\n" if ( $str !~ /\n$/ );
240             }
241             $str = "";
242             @conf = ();
243         }
244     }
245     if ( $str ne "" && @conf ) {
246         $out .= "=item " . join("\n\n=item ", @conf) . "\n\n";
247         $out .= $str;
248         $out .= "\n" if ( $str !~ /\n$/ );
249     }
250     $out .= "=back\n\n" if ( !$first );
251     return $out;
252 }
253
254 sub CheckConfigParams
255 {
256     my($file, $vars, $check) = @_;
257     my $errors;
258
259     open(F, $file) || die("can't open $file\n");
260     if ( $check ) {
261         while ( <F> ) {
262             s/\$(self|bpc)->{Conf}{([^}\$]+)}/if ( !defined($vars->{$2}) ) {
263                     print("Unexpected Conf var $2 in $file\n");
264                     $errors++;
265                 } else {
266                     $vars->{$2}++;
267                 }/eg;
268             s/\$[Cc]onf(?:->)?{([^}\$]+)}/if ( !defined($vars->{$1}) ) {
269                     print("Unexpected Conf var $1 in $file\n");
270                     $errors++;
271                 } else {
272                     $vars->{$1}++;
273                 }/eg;
274             s/UserCommandRun\("([^"]*)"\)/if ( !defined($vars->{$1}) ) {
275                     print("Unexpected Conf var $1 in $file\n");
276                     $errors++;
277                 } else {
278                     $vars->{$1}++;
279                 }/eg;
280         }
281     } else {
282         while ( <F> ) {
283             s/^[^#]*\$self->{Conf}{([^}]*)/$vars->{$1} = 1;/eg;
284             s/^[^#]*\$Conf{([^}]*)/$vars->{$1} = 1;/eg;
285         }
286     }
287     close(F);
288     return $errors;
289 }
290
291 #
292 # Make sure that every lang variable in cgi-bin/BackupPC_Admin matches
293 # the strings in each lib/BackupPC/Lang/*.pm file.  This makes sure
294 # we didn't miss any translations in any of the languages.
295 #
296 sub CheckLangUsage
297 {
298     my $errors;
299     my $vars = {};
300
301     foreach my $file ( qw(cgi-bin/BackupPC_Admin bin/BackupPC_sendEmail) ) {
302         open(F, $file) || die("can't open $file");
303         while ( <F> ) {
304             s/\$Lang->{([^}]*)}/$vars->{$1} = 1;/eg;
305         }
306         close(F);
307     }
308     foreach my $f ( <lib/BackupPC/Lang/*.pm> ) {
309         my $done = {};
310         open(F, $f) || die("can't open $f\n");
311         while ( <F> ) {
312             s/#.*//g;
313             s/\$Lang{([^}]*)}/
314                     my $var = $1;
315                     next if ( $var =~ m{^(Reason_|Status_)} );
316                     if ( !defined($vars->{$var}) ) {
317                         print("Unexpected Lang var $var in $f\n");
318                         $errors++;
319                     } else {
320                         $done->{$var} = 1;
321                     }/eg;
322         }
323         close(F);
324         foreach my $v ( keys(%$vars) ) {
325             #
326             # skip "variables" with "$", since they are like expressions
327             #
328             next if ( $v =~ /\$/ );
329             if ( !defined($done->{$v}) ) {
330                 print("Lang var $v missing from $f\n");
331                 $errors++;
332             }
333         }
334     }
335     return $errors;
336 }