11 require VMS::Filespec;
16 Getopt::Long::Configure('no_ignore_case');
18 our $LastUpdate = -M $0;
22 my $file = shift or die "Need file\n". usage();
23 my $outfile = shift || '';
24 $file = vms_check_name($file) if $^O eq 'VMS';
25 my $mode = (stat($file))[2] & 07777;
27 open my $fh, "<", $file
28 or do { warn "Could not open input file $file: $!"; exit 0 };
30 my $str = do { local $/; <$fh> };
37 $outfile =~ s/\.packed\z//;
39 my ($head, $body) = split /__UU__\n/, $str;
40 die "Can't unpack malformed data in '$file'\n"
42 $outstr = unpack 'u', $body;
45 $outfile ||= $file . '.packed';
47 my $me = basename($0);
49 $outstr = <<"EOFBLURB" . pack 'u', $str;
50 #########################################################################
51 This is a binary file that was packed with the 'uupacktool.pl' which
52 is included in the Perl distribution.
54 To unpack this file use the following command:
58 To recreate it use the following command:
62 Created at @{[scalar localtime]}
63 #########################################################################
72 $outfile = VMS::Filespec::vmsify($outfile) if $^O eq 'VMS';
73 print "Writing $file into $outfile\n" if $opts->{'v'};
74 open my $outfh, ">", $outfile
75 or do { warn "Could not open $outfile for writing: $!"; exit 0 };
77 ### $outstr might be empty, if the file was empty
78 print $outfh $outstr if $outstr;
81 chmod $mode, $outfile;
84 ### delete source file?
85 if( $opts->{'D'} and $file ne $outfile ) {
92 my $Manifest = $opts->{'m'};
94 open my $fh, "<", $Manifest or die "Could not open '$Manifest':$!";
96 print "Reading $Manifest\n"
101 while( my $line = <$fh> ) {
103 my ($file) = split /\s+/, $line;
107 next unless $file =~ /\.packed/;
112 $out =~ s/\.packed\z//;
113 $out = vms_check_name($out) if $^O eq 'VMS';
116 if( !$opts->{'c'} ) {
117 ( $out, $file ) = ( $file, $out ) if $opts->{'p'};
120 if ($changed < $LastUpdate and $changed < -M $file) {
121 print "Skipping '$file' as '$out' is up-to-date.\n"
126 handle_file($opts, $file, $out);
127 print "Converted '$file' to '$out'\n"
135 print "File '$file' was not unpacked into '$out'. Can not remove.\n";
139 print "Removing '$out'\n";
144 print "Found $count files to process out of $lines in '$Manifest'\n"
150 Usage: $^X $0 [-d dir] [-v] [-c] [-D] -p|-u [orig [packed|-s] | -m [manifest]]
152 Handle binary files in source tree. Can be used to pack or
153 unpack files individiually or as specified by a manifest file.
156 -u Unpack files (defaults to -u unless -p is specified)
158 -c Clean up all unpacked files. Implies -m
160 -D Delete source file after encoding/decoding
162 -s Output to STDOUT rather than OUTPUT_FILE
163 -m Use manifest file, if none is explicitly provided defaults to 'MANIFEST'
165 -d Change directory to dir before processing
168 -h Display this help message
174 # Packed files tend to have multiple dots, which the CRTL may or may not handle
175 # properly, so convert to native format. And depending on how the archive was
176 # unpacked, foo.bar.baz may be foo_bar.baz or foo.bar_baz. N.B. This checks for
177 # existence, so is not suitable as-is to generate ODS-2-safe names in preparation
182 $file = VMS::Filespec::vmsify($file);
183 return $file if -e $file;
185 my ($vol,$dirs,$base) = File::Spec->splitpath($file);
187 1 while $tmp =~ s/([^\.]+)\.(.+\..+)/$1_$2/;
188 my $try = File::Spec->catpath($vol, $dirs, $tmp);
189 return $try if -e $try;
192 1 while $tmp =~ s/(.+\..+)\.([^\.]+)/$1_$2/;
193 $try = File::Spec->catpath($vol, $dirs, $tmp);
194 return $try if -e $try;
200 GetOptions($opts,'u','p','c', 'D', 'm:s','s','d=s','v','h');
202 die "Can't pack and unpack at the same time!\n", usage()
203 if $opts->{'u'} && $opts->{'p'};
204 die usage() if $opts->{'h'};
206 if ( $opts->{'d'} ) {
208 or die "Failed to chdir to '$opts->{'d'}':$!";
210 $opts->{'u'} = 1 if !$opts->{'p'};
211 binmode STDOUT if $opts->{'s'};
212 if ( exists $opts->{'m'} or exists $opts->{'c'} ) {
213 $opts->{'m'} ||= "MANIFEST";
218 handle_file($opts, @ARGV);
220 die "No file to process specified!\n", usage();