* Added internationalization (i18n) code from Xavier Nicollet.
[BackupPC.git] / makeDist
1 #!/bin/perl
2 #
3 # Build a BackupPC distribution
4 #
5
6 use strict;
7 use File::Path;
8 use File::Copy;
9
10 umask(0022);
11
12 my $Version     = "1.5.0";
13 my $ReleaseDate = "2 Aug 2002";
14 my $DistDir     = "dist/BackupPC-$Version";
15
16 my @PerlSrc = qw(
17     bin/BackupPC
18     bin/BackupPC_dump
19     bin/BackupPC_link
20     bin/BackupPC_nightly
21     bin/BackupPC_restore
22     bin/BackupPC_sendEmail
23     bin/BackupPC_serverMesg
24     bin/BackupPC_trashClean
25     bin/BackupPC_tarExtract
26     bin/BackupPC_tarCreate
27     bin/BackupPC_compressPool
28     bin/BackupPC_zipCreate
29     bin/BackupPC_zcat
30     lib/BackupPC/Attrib.pm
31     lib/BackupPC/FileZIO.pm
32     lib/BackupPC/Lib.pm
33     lib/BackupPC/Lang/en.pm
34     lib/BackupPC/Lang/fr.pm
35     lib/BackupPC/PoolWrite.pm
36     lib/BackupPC/Xfer/Smb.pm
37     lib/BackupPC/Xfer/Tar.pm
38     lib/BackupPC/Zip/FileMember.pm
39     cgi-bin/BackupPC_Admin
40 );
41
42 #
43 # Check config parameters
44 #
45 my $ConfVars = {};
46 CheckConfigParams("conf/config.pl", $ConfVars, 0);
47 $ConfVars->{BackupPCUser} = 2;
48 $ConfVars->{CgiDir} = 2;
49 $ConfVars->{InstallDir} = 2;
50 $ConfVars->{CgiImageDir} = 2;
51 foreach my $file ( @PerlSrc ) {
52     CheckConfigParams($file, $ConfVars, 1);
53 }
54 my $errCnt;
55 foreach my $var ( sort(keys(%$ConfVars) ) ) {
56     next if ( $ConfVars->{$var} >= 2 || $var =~ /^\$/ );
57     printf("Unused config parameter $var\n");
58     $errCnt++;
59 }
60 exit(1) if ( $errCnt );
61
62 rmtree($DistDir, 0, 0);
63 mkpath($DistDir, 0, 0777);
64
65 foreach my $dir ( qw(bin lib/BackupPC/Xfer lib/BackupPC/Zip doc conf
66                      images init.d/src cgi-bin) ) {
67     mkpath("$DistDir/$dir", 0, 0777);
68 }
69
70 my %ConfName;
71 my $ConfPod = config2pod();
72 rmtree("doc", 0, 0);
73 mkpath("doc", 0, 0777);
74 InstallFile("doc-src/BackupPC.pod", "doc/BackupPC.pod");
75
76 use Pod::Html;
77 pod2html("doc/BackupPC.pod",
78         "--backlink=Back to Top",
79         "--header",
80         "--title=BackupPC",
81         "--outfile=doc/BackupPC.html");
82
83 foreach my $file ( (@PerlSrc,
84             <images/*>,
85             qw(
86                 conf/config.pl
87                 conf/hosts
88                 init.d/README
89                 init.d/src/linux-backuppc
90                 init.d/src/solaris-backuppc
91                 doc/BackupPC.pod
92                 doc/BackupPC.html
93                 README
94                 LICENSE
95                 ChangeLog
96                 configure.pl
97         )) ) {
98     InstallFile("$file", "$DistDir/$file");
99 }
100 rmtree("doc", 0, 0);
101 system("cd dist ; tar zcf BackupPC-$Version.tar.gz BackupPC-$Version");
102
103 ###########################################################################
104 # Subroutines
105 ###########################################################################
106
107 sub InstallFile
108 {
109     my($file, $dest) = @_;
110
111     unlink($dest) if ( -d $dest );
112     if ( $file =~ /\.gif/ ) {
113         die("can't copy($file, $dest)\n") unless copy($file, $dest);
114     } else {
115         open(FILE, $file)   || die("can't open $file for reading\n");
116         open(OUT, ">$dest") || die("can't open $dest for writing\n");
117         while ( <FILE> ) {
118             s/^# *Version \d+\.\d+[\.\w]*, released \d+ \w+ \d{4}\.?/# Version __VERSION__, released __RELEASEDATE__./;
119             s/__VERSION__/$Version/g;
120             s/__RELEASEDATE__/$ReleaseDate/g;
121             if ( $file =~ /BackupPC\.html$/ && !/A NAME="item_%24Conf/ ) {
122                 s/\$Conf{([^}]*)}/
123                         defined($ConfName{$1})
124                             ? "<A HREF=\"#$ConfName{$1}\">\$Conf{$1}<\/A>"
125                             : "\$Conf{$1}"/eg;
126             }
127             if ( /__CONFIGPOD__/ ) {
128                 print OUT $ConfPod;
129             } elsif ( /^use lib ".*BackupPC\/lib";/
130                     || /^use lib "\/home\/pcbackup\/install\/lib";/ ) {
131                 print OUT "use lib \"__INSTALLDIR__/lib\";\n";
132             } elsif ( $file =~ /Lib.pm/ && /(.*TopDir *=> .*)'.*',/ ) {
133                 print OUT "$1'__TOPDIR__',\n";
134             } elsif ( $file =~ /Lib.pm/ && /(.*Version *=> .*)'[\w\d\.]+',/ ) {
135                 print OUT "$1'$Version',\n";
136             } elsif ( $file =~ /Lib.pm/ && /(.*BinDir *=> .*)'.*',/ ) {
137                 print OUT "$1'__INSTALLDIR__/bin',\n";
138             } elsif ( $file =~ /Lib.pm/ && /(.*LibDir *=> .*)'.*',/ ) {
139                 print OUT "$1'__INSTALLDIR__/lib',\n";
140             } else {
141                 print OUT;
142             }
143         }
144         close(FILE);
145         close(OUT);
146     }
147     if ( -x $file ) {
148         chmod(0555, $dest);
149     } else {
150         chmod(0444, $dest);
151     }
152 }
153
154 sub config2pod
155 {
156     open(C, "conf/config.pl") || die("can't open conf/config.pl");
157     my($str, $out, $getHdr, @conf);
158     my $first = 1;
159     while ( <C> ) {
160         chomp;
161         s/ +$//;
162         if ( /^#########################/ ) {
163             if ( $getHdr ) {
164                 $str =~ s/\n.*//sg;
165                 $out .= "=back\n\n" if ( !$first );
166                 $out .= "=head2 $str\n\n=over 4\n\n";
167                 $str = "";
168                 $first = 0;
169             }
170             $getHdr = !$getHdr;
171             next;
172         }
173         if ( /^#/ ) {
174             s/# ?//;
175             next if ( $str eq "" && /^$/ );
176             $str .= $_ . "\n";
177             $str .= "\n" if ( $str =~ /examples?:\n$/i );
178         } elsif ( /^\$Conf{([^}]*)/ ) {
179             my $var = $1;
180             s/  +/ /g;
181             s/;\s*#.*/;/;
182             if ( !s/\[$/[ ... ];/ && !s/<<'EOF'/.../ ) {
183                 s/([^;])\s*$/$1 .../;
184             }
185             push(@conf, $_);
186             my $text = $_;
187             $text =~ s/\s+/_/sg;
188             $text =~ s{(\W)}{sprintf("%%%02X", ord($1) )}gxe;
189             $text = substr($text, 0, 50);
190             $ConfName{$var} = "item_$text";
191         } elsif ( /^$/ ) {
192             if ( $str ne "" && @conf ) {
193                 $out .= "=item " . join("\n\n=item ", @conf) . "\n\n";
194                 $out .= $str;
195                 $out .= "\n" if ( $str !~ /\n$/ );
196             }
197             $str = "";
198             @conf = ();
199         }
200     }
201     if ( $str ne "" && @conf ) {
202         $out .= "=item " . join("\n\n=item ", @conf) . "\n\n";
203         $out .= $str;
204         $out .= "\n" if ( $str !~ /\n$/ );
205     }
206     $out .= "=back\n\n" if ( !$first );
207     return $out;
208 }
209
210 sub CheckConfigParams
211 {
212     my($file, $vars, $check) = @_;
213
214     open(F, $file) || die("can't open $file\n");
215     if ( $check ) {
216         while ( <F> ) {
217             s/\$self->{Conf}{([^}\$]+)}/if ( !defined($vars->{$1}) ) {
218                     print("Unexpected Conf var $1 in $file\n");
219                     exit(1);
220                 } else {
221                     $vars->{$1}++;
222                 }/eg;
223             s/\$[Cc]onf(?:->)?{([^}\$]+)}/if ( !defined($vars->{$1}) ) {
224                     print("Unexpected Conf var $1 in $file\n");
225                     exit(1);
226                 } else {
227                     $vars->{$1}++;
228                 }/eg;
229         }
230     } else {
231         while ( <F> ) {
232             s/^[^#]*\$self->{Conf}{([^}]*)/$vars->{$1} = 1;/eg;
233             s/^[^#]*\$Conf{([^}]*)/$vars->{$1} = 1;/eg;
234         }
235     }
236     close(F);
237 }