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