8 Getopt::Long::Configure('no_ignore_case');
10 our $LastUpdate = -M $0;
14 my $file = shift or die "Need file\n". usage();
15 my $outfile = shift || '';
16 my $mode = (stat($file))[2] & 07777;
18 open my $fh, "<", $file
19 or die "Could not open input file $file: $!";
21 my $str = do { local $/; <$fh> };
28 $outfile =~ s/\.packed//;
30 my ($head, $body) = split /__UU__\n/, $str;
31 die "Can't unpack malformed data in '$file'\n"
33 $outstr = unpack 'u', $body;
36 $outfile ||= $file . '.packed';
38 my $me = basename($0);
40 $outstr = <<"EOFBLURB" . pack 'u', $str;
41 #########################################################################
42 This is a binary file that was packed with the 'uupacktool.pl' which
43 is included in the Perl distribution.
45 To unpack this file use the following command:
49 To recreate it use the following command:
53 Created at @{[scalar localtime]}
54 #########################################################################
63 print "Writing $file into $outfile\n" if $opts->{'v'};
64 open my $outfh, ">", $outfile
65 or die "Could not open $outfile for writing: $!";
67 ### $outstr might be empty, if the file was empty
68 print $outfh $outstr if $outstr;
71 chmod $mode, $outfile;
74 ### delete source file?
75 if( $opts->{'D'} and $file ne $outfile ) {
82 my $Manifest = $opts->{'m'};
84 open my $fh, "<", $Manifest or die "Could not open '$Manifest':$!";
86 print "Reading $Manifest\n"
91 while( my $line = <$fh> ) {
93 my ($file) = split /\s+/, $line;
97 next unless $file =~ /\.packed/;
102 $out =~ s/\.packed//;
105 if( !$opts->{'c'} ) {
106 ( $out, $file ) = ( $file, $out ) if $opts->{'p'};
109 if ($changed < $LastUpdate and $changed < -M $file) {
110 print "Skipping '$file' as '$out' is up-to-date.\n"
115 handle_file($opts, $file, $out);
116 print "Converted '$file' to '$out'\n"
124 print "File '$file' was not unpacked into '$out'. Can not remove.\n";
128 print "Removing '$out'\n";
133 print "Found $count files to process out of $lines in '$Manifest'\n"
139 Usage: $^X $0 [-d dir] [-v] [-c] [-D] -p|-u [orig [packed|-s] | -m [manifest]]
141 Handle binary files in source tree. Can be used to pack or
142 unpack files individiually or as specified by a manifest file.
145 -u Unpack files (defaults to -u unless -p is specified)
147 -c Clean up all unpacked files. Implies -m
149 -D Delete source file after encoding/decoding
151 -s Output to STDOUT rather than OUTPUT_FILE
152 -m Use manifest file, if none is explicitly provided defaults to 'MANIFEST'
154 -d Change directory to dir before processing
157 -h Display this help message
162 GetOptions($opts,'u','p','c', 'D', 'm:s','s','d=s','v','h');
164 die "Can't pack and unpack at the same time!\n", usage()
165 if $opts->{'u'} && $opts->{'p'};
166 die usage() if $opts->{'h'};
168 if ( $opts->{'d'} ) {
170 or die "Failed to chdir to '$opts->{'d'}':$!";
172 $opts->{'u'} = 1 if !$opts->{'p'};
173 binmode STDOUT if $opts->{'s'};
174 if ( exists $opts->{'m'} or exists $opts->{'c'} ) {
175 $opts->{'m'} ||= "MANIFEST";
180 handle_file($opts, @ARGV);
182 die "No file to process specified!\n", usage();