2 eval "exec perl -S $0 $*"
3 if $running_under_some_shell;
5 # $Id: makedist.SH 1 2006-08-24 12:32:52Z rmanfredi $
7 # Copyright (c) 1991-1997, 2004-2006, Raphael Manfredi
9 # You may redistribute only under the terms of the Artistic Licence,
10 # as specified in the README file that comes with the distribution.
11 # You may reuse parts of this distribution only within the terms of
12 # that same Artistic Licence; a copy of which may be found at the root
13 # of the source tree for dist 4.0.
15 # $Log: makedist.SH,v $
16 # Revision 3.0.1.2 1994/01/24 13:58:20 ram
17 # patch16: modified call to manifake to trap exceptions manually
18 # patch16: removed final sed post-processing to allow 'make depend' target
19 # patch16: added ~/.dist_profile awareness
21 # Revision 3.0.1.1 1993/08/19 06:42:17 ram
22 # patch1: leading config.sh searching was not aborting properly
24 # Revision 3.0 1993/08/18 12:04:28 ram
25 # Baseline for dist 3.0 netwide release.
31 &profile; # Read ~/.dist_profile
33 &usage unless &Getopts('c:f:dhvqs:V');
35 $ENV{'DIST'} = '/dev/null'; # Disable ~/.dist_profile
38 print STDERR "makedist $version PL$patchlevel\n";
44 $MAXKITSIZE = 50000 unless $MAXKITSIZE = $opt_s;
47 $CHOPSIZE = $MAXKITSIZE - $KITOVERHEAD - $FILEOVERHEAD;
49 $NEWMANI = 'MANIFEST.new' unless $NEWMANI = $opt_f;
50 $MANI = 'MANIFEST' unless $opt_f;
51 $PACKLIST = 'PACKLIST';
52 $PACKNOTES = 'PACKNOTES';
54 $tmpdir = "/tmp/MKst$$"; # Where to copy distribution
55 $tmpdir = '.' if $opt_q; # Quick mode: no need to copy distribution
57 &set_sig('aborted'); # Make sure we clean up in case of emergency
62 eval '&manifake'; # Want to trap possible die and redirect to fatal
68 if ($opt_c) { # Copy distribution only, no shell archive
74 ©right'init($copyright) if -f $copyright;
76 unlink <$package.kit? $package.kit??>;
77 chop($curdir = `pwd`);
78 chdir $tmpdir || die "Can't chdir to $tmpdir.\n";
87 # Physically build the kits
96 for ($kitnum = 1; $kitnum <= $numkits; $kitnum++) {
97 $list = $list[$kitnum];
98 $kit = sprintf("$package.kit" . $sp,$kitnum);
99 print "*** Making $kit ***\n";
100 open(KIT,">$curdir/$kit") || do fatal("Can't create $curdir/$kit: $!");
104 @files = split(' ',$list);
108 while (s|^(.*)/.*$|$1|) {
109 push(@Xdirs,$_) unless $Xseen{$_}++;
112 print KIT "mkdir ",join(' ', sort @Xdirs)," 2>/dev/null\n";
114 foreach $file (@files) {
115 print "\t",$file,"\n" if $opt_v;
116 print KIT "echo Extracting $file\n";
117 print KIT "sed >$file <<'!STUFFY!FUNK!' -e 's/X//'\n";
119 ©right'reset; # Reset copyright for new file
121 # Use Lock[e]r as a pattern in case it is applied on ourselves
122 s|Lock[e]r:.*\$|\$|; # Remove locker mark
123 print KIT ©right'filter($_, 'X');
126 print KIT "!STUFFY!FUNK!\n";
127 -x "$file" && (print KIT "chmod +x $file\n");
135 for $filename (keys %comment) {
136 next if $filename =~ m|/$|; # Skip directories
137 next if -d $filename; # Better safe than sorry
138 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
139 $blksize,$blocks) = stat($filename);
141 # Make sure file is not larger than the CHOPSIZE limit. If it is,
142 # a split is attempted.
143 if ($size > $CHOPSIZE) {
144 print "Splitting $filename...\n" if $opt_v;
145 $file_comment = $comment{$filename};
146 open(FILE, $filename) || die "Can't open $filename: $!\n";
148 ($dir, $name) = ('.', $filename)
149 unless ($dir, $name) = ($filename =~ m|(.*)/(.*)|);
150 $chopped = $dir . '/' . substr($name, 0, 11);
151 $chopped =~ s|^\./||;
152 &fatal("There is already a split file named $chopped")
153 if defined $Chopped{$chopped};
154 $Chopped{$chopped} = $filename; # Association split <-> real file
156 open(CURPIECE, ">$chopped:$piece") ||
157 &fatal("Can't create $chopped:$piece: $!");
159 if ($size + length($_) > $CHOPSIZE) {
161 $size{"$chopped:$piece"} = $size;
162 $comment{"$chopped:$piece"} = "$file_comment (part $piece)";
163 push(@files, "$chopped:$piece");
164 print "\t$chopped:$piece ($size bytes)\n" if $opt_v;
166 $piece++; # AA -> AB, etc...
167 open(CURPIECE, ">$chopped:$piece") ||
168 &fatal("Can't create $chopped:$piece: $!");
175 $size{"$chopped:$piece"} = $size;
176 $comment{"$chopped:$piece"} = "$file_comment (part $piece)";
177 push(@files, "$chopped:$piece");
178 print "\t$chopped:$piece ($size bytes)\n" if $opt_v;
179 delete $comment{$filename}; # File split, not in PACKLIST
181 $size += 1000000 if $filename =~ /README/;
182 $size{$filename} = $size;
183 push(@files, "$filename");
187 # Build a file PACKNOTES to reconstruct split files
188 if (defined %Chopped) {
189 open(PACKNOTES, ">$PACKNOTES") || &fatal("Can't create PACKNOTES: $!");
190 foreach (keys %Chopped) {
191 print PACKNOTES <<EOC;
192 echo 'Building $Chopped{$_}...'
193 cat $_:[A-Z][A-Z] > $Chopped{$_}
198 push(@files, $PACKNOTES);
199 $comment{$PACKNOTES} = 'Script to reconstruct split files';
200 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
201 $blksize,$blocks) = stat($PACKNOTES);
202 $size{$PACKNOTES} = $size;
205 # Currently, file PACKLIST does not exist, so its size is unknown and
206 # it cannot be correctly put in one archive. Therefore, we take the
207 # size of MANIFEST.new, which will give us a good estimation.
208 push(@files, 'PACKLIST');
210 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
211 $blksize,$blocks) = stat($NEWMANI);
212 $size{$PACKLIST} = $size;
214 sub revnum { $size{$a} < $size{$b} ? 1 : $size{$a} > $size{$b} ? -1 : 0; }
215 @files = sort revnum @files;
219 $size -= 1000000 if /README/;
221 while (($newtot = int($tot[$i] + $size + $size/40 + $FILEOVERHEAD)) >
222 $MAXKITSIZE-$KITOVERHEAD && $tot[$i]) {
226 print "Adding $_ to kit $i giving $newtot bytes\n" if $opt_d;
232 # Read manifest file and initialize the %comment array.
234 do fatal("You don't have a $NEWMANI file. Run manifake")
235 unless -f "$NEWMANI";
236 open(NEWMANI,$NEWMANI) || do fatal("Can't read $NEWMANI: $!");
238 ($key,$val) = split(' ',$_,1) unless ($key,$val) = /^(\S+)\s+(.*)/;
239 $comment{$key} = $val;
244 # MANIFEST and MANIFEST.new must say the same thing. Create the
245 # PACKLIST file (thus avoiding kit numbers in MANIFEST, which causes big
246 # patches when only re-ordering occurred). Note that PACKLIST should
247 # not appear in MANIFEST.new (the user may remove it).
250 $comment{$PACKLIST} = 'Which files came with which kits';
252 open(PACKLIST, ">$PACKLIST") || do fatal("Can't create $PACKLIST: $!");
254 "After all the $package kits are run you should have the following files:
256 Filename Kit Description
257 -------- --- -----------
259 for (sort keys(comment)) {
260 printf PACKLIST "%-27s %2s %.47s\n", $_, $kit{$_}, $comment{$_};
267 $plevel = " at patchlevel $patch_level" if $patch_level ne '';
271 # This is $package version $baserev$plevel.
272 # Make a new directory for the $package sources, cd to it, and run kits 1 up
273 # to $numkits through sh. When all $numkits kits have been run, read README.
277 This is $package $baserev$plevel, kit $kitnum (of $numkits):
278 If this shell archive is complete, the line "End of kit $kitnum (of $numkits)"
279 will echo at the end.
281 export PATH || (echo "Please use sh to unpack this archive." ; kill \$\$)
287 for ($i = 1; $i <= $numkits; $i++) {
288 $rangelist .= ' ' . $i;
291 echo \"End of kit $kitnum (of $numkits)\"
293 cat /dev/null >kit${kitnum}isdone
296 for iskit in$rangelist; do
297 if test -f kit\${iskit}isdone; then
298 run=\"\$run \$iskit\"
300 todo=\"\$todo \$iskit\"
305 echo \"You have run all your kits.\"
307 if (defined %Chopped) { # Some splitting occurred
309 if test -f $PACKNOTES; then
312 echo \"You have to rebuild split files by hand (see $PACKLIST).\"
316 if (-f "README" && -f "Configure") {
318 " echo \"Please read README and then type Configure.\"
319 chmod 755 Configure\n";
320 } elsif (-f "README") {
322 " echo \"Please read README first.\"\n";
323 } elsif (-f "Configure") {
325 " echo \"Please run Configure first.\"
326 chmod 755 Configure\n";
331 *) echo \"You have run\$run.\"
332 echo \"You still need to run\$todo.\"
335 : Someone might mail this, so exit before signature...
342 if (-f 'patchlevel.h') {
343 open(PL, 'patchlevel.h');
345 /^#define\s+PATCHLEVEL\s+(\w+)/ && ($patch_level = $1);
354 $sw = 's' unless $opt_v;
355 mkdir($tmpdir, 0700) || die "Can't create directory $tmpdir.\n";
356 print "Building a copy of distribution in $tmpdir...\n" if $opt_v;
357 system 'perl', '-S', 'patcol', "-a$sw", '-f', $NEWMANI, '-d', $tmpdir;
358 system 'cp', $NEWMANI, "$tmpdir/$NEWMANI"
359 unless -f "$tmpdir/$NEWMANI" && !$opt_f;
363 local($sw); # Switch to force patcol to copy checked out files
365 print "Building a copy of distribution in $opt_c...\n" if $opt_v;
367 $sw .= 's' unless $opt_v;
368 system 'perl', '-S', 'patcol', "-aRC$sw", '-f', $NEWMANI, '-d', $opt_c;
373 print "Removing distribution in $tmpdir...\n" if $opt_v;
374 chdir "/"; # Do not stay in removed directory...
375 system '/bin/rm', '-rf', "$tmpdir";
379 foreach $base (keys %Chopped) {
380 print "Removing split files for $base:\n" if $opt_v;
382 while (-f "$base:$piece") {
383 print "\t$base:$piece\n" if $opt_v;
384 unlink "$base:$piece";
385 $piece++; # AA -> AB, etc...
391 &distrm if -d $tmpdir;
393 &splitrm; # Remove in-place split files
394 unlink $PACKLIST, $PACKNOTES;
399 local($reason) = shift(@_);
405 local($handler) = @_;
406 $SIG{'HUP'} = $handler;
407 $SIG{'INT'} = $handler;
408 $SIG{'QUIT'} = $handler;
409 $SIG{'TERM'} = $handler;
414 $opt_v = 1; # Force verbose message in distrm
422 Usage: makedist [-dhqvV] [-c dir] [-s size] [-f manifest]
423 -c : copy files in dir, do not build any shell archive.
425 -f : use this file as manifest.
426 -h : print this help message and exits.
427 -q : quick mode: use checked-out files.
428 -s : set maximum pack size.
430 -V : print version number and exits.
436 if (! -f '.package') {
439 -f '../../.package' ||
440 -f '../../../.package' ||
441 -f '../../../../.package'
443 die "Run in top level directory only.\n";
445 die "No .package file! Run packinit.\n";
448 open(PACKAGE,'.package');
452 if (($var,$val) = /^\s*(\w+)=(.*)/) {
453 $val = "\"$val\"" unless $val =~ /^['"]/;
454 eval "\$$var = $val;";
461 # make MANIFEST and MANIFEST.new say the same thing
464 open(IN,$MANI) || die "Can't open $MANI";
465 open(OUT,">$NEWMANI") || die "Can't create $NEWMANI";
468 # Everything until now was a header...
470 open(OUT,">$NEWMANI") ||
471 die "Can't recreate $NEWMANI";
474 s/^\s*(\S+\s+)[0-9]*\s*(.*)/$1$2/;
476 print OUT "\n" unless /\n$/; # If no description
482 die "You need to make a $NEWMANI file, with names and descriptions.\n";
489 # Read in copyright file
491 local($file) = @_; # Copyright file
493 open(COPYRIGHT, $file) || die "Can't open $file: $!\n";
494 chop(@copyright = <COPYRIGHT>);
498 # Reset the automaton for a new file.
500 $copyright_seen = @copyright ? 0 : 1;
504 # Filter file, line by line, and expand the copyright string. The @COPYRIGHT@
505 # symbol may be preceded by some random comment. A leader can be defined and
506 # will be pre-pended to all the input lines.
508 local($line, $leader) = @_; # Leader is optional
509 return $leader . $line if $copyright_seen || $marker_seen;
510 $marker_seen = 1 if $line =~ /\$Log[:\$]/;
511 $copyright_seen = 1 if $line =~ /\@COPYRIGHT\@/;
512 return $leader . $line unless $copyright_seen;
513 local($comment, $trailer) = $line =~ /^(.*)\@COPYRIGHT\@\s*(.*)/;
514 $comment = $leader . $comment;
515 $comment . join("\n$comment", @copyright) . "\n";
518 # Filter output of $cmd redirected into $file by expanding copyright, if any.
520 local($cmd, $file) = @_;
522 open(CMD,"$cmd|") || die "Can't start '$cmd': $!\n";
523 open(OUT, ">$file") || die "Can't create $file: $!\n";
527 print OUT &filter($_);
532 system "$cmd > $file";
533 die "Command '$cmd' failed!" if $?;
539 # Make directories for files
540 # E.g, for /usr/lib/perl/foo, it will check for all the
541 # directories /usr, /usr/lib, /usr/lib/perl and make
542 # them if they do not exist.
546 if (!-d && $_ ne '') {
548 do makedir($_) if s|(.*)/.*|\1|;
549 mkdir($dir, 0700) if ! -d $dir;
553 # Perform ~name expansion ala ksh...
554 # (banish csh from your vocabulary ;-)
557 return $path unless $path =~ /^~/;
558 $path =~ s:^~([^/]+):(getpwnam($1))[$[+7]:e; # ~name
559 $path =~ s:^~:$ENV{'HOME'} || (getpwuid($<))[$[+7]:e; # ~
563 # Set up profile components into %Profile, add any profile-supplied options
564 # into @ARGV and return the command invocation name.
566 local($profile) = &tilda_expand($ENV{'DIST'} || '~/.dist_profile');
567 local($me) = $0; # Command name
568 $me =~ s|.*/(.*)|$1|; # Keep only base name
569 return $me unless -s $profile;
570 local(*PROFILE); # Local file descriptor
571 local($options) = ''; # Options we get back from profile
572 unless (open(PROFILE, $profile)) {
573 warn "$me: cannot open $profile: $!\n";
579 next if /^\s*#/; # Skip comments
581 if (s/^$me://o) { # progname: options
583 $options .= $_; # Merge options if more than one line
585 elsif (s/^$me-([^:]+)://o) { # progname-component: value
588 s/^\s+//; # Trim leading and trailing spaces
590 $Profile{$component} = $_;
594 return unless $options;
595 require 'shellwords.pl';
597 eval '@opts = &shellwords($options)'; # Protect against mismatched quotes
598 unshift(@ARGV, @opts);
599 return $me; # Return our invocation name