592851
#!/usr/bin/perl
592851
use strict;
592851
use warnings;
592851
use File::Path;
592851
use File::Spec;
592851
use Getopt::Long;
592851
592851
my $libdir = 'lib';
592851
my $filter = '';
592851
592851
GetOptions('libdir=s' => \$libdir, 'filter=s' => \$filter) or
592851
    die "Could not parse arguments\n";
592851
if ($filter eq '') {
592851
    # Empty pattern passes previous result by definition. Do not use it.
592851
    # Interpolared compilation is fixed in perl 5.18.0. RT#119095.
592851
    $filter = qr/(?:)/;
592851
}
592851
eval { $filter = qr{$filter}; 1} or
592851
    die "Could not compile filter as a regular expression: $@\n";
592851
592851
my ($file, $filename, $delimiter);
592851
while (<>) {
592851
    if (/^\$fatpacked\{\s*"([^"]*)"\s*\}\s*=.*<<\s*'([^']*)'\s*;/) {
592851
        # Packed module beginning found
592851
        $filename = $1;
592851
        $delimiter = $2;
592851
        if ($filename =~ $filter) {
592851
            print STDERR "Extracting `$filename'\n";
592851
            my $directory = (File::Spec->splitpath($filename))[1];
592851
            File::Path::make_path(File::Spec->catfile($libdir, $directory));
592851
            if ($file) {
592851
                die "Unballanced fat-packed module at line $.\n";
592851
            }
592851
            open($file, '>', File::Spec->catfile($libdir, $filename)) or
592851
                die "Could not create `",
592851
                    File::Spec->catfile($libdir, $filename), "': $!\n";
592851
        } else {
592851
            print STDERR "Removing `$filename'\n";
592851
        }
592851
    } elsif (defined $delimiter and /^\Q$delimiter\E$/) {
592851
        # Packed module end found
592851
        if (defined $file) {
592851
            close($file) or
592851
                die "Could not close `",
592851
                    File::Spec->catfile($libdir, $filename), "': $!\n";
592851
            $file = undef;
592851
        }
592851
        $filename = undef;
592851
        $delimiter = undef;
592851
    } elsif (defined $file) {
592851
        # Packed module to extract
592851
        s/^  //;    # de-escape recursive here-documents
592851
        print $file $_;
592851
    } elsif (! defined $delimiter) {
592851
        # Rest of code to output
592851
        print STDOUT $_;
592851
    }
592851
}
592851
592851
__END__
592851
592851
=encoding utf8
592851
592851
=head1 NAME
592851
592851
fatunpack - Unpacker for App::FatPacker packets
592851
592851
=head1 SYNOPSYS
592851
592851
fatunpack [OPTION…] [PACKED_SCRIPT…]
592851
592851
=head1 DESCRIPTION
592851
592851
This tool unpacks scripts packed with App::FatPacker.
592851
592851
Packed script's file names are specified as positional arguments. If no
592851
argument is given, a script from standard intput will be processed.
592851
592851
The content of packed script stripped of all bundled modules is written to
592851
standard output.
592851
592851
=head1 OPTIONS
592851
592851
=over 8
592851
592851
=item B<--libdir DIRECTORY>
592851
592851
Directory to output unpacked modules to that where bundled into the input
592851
script. Default value is C<lib>.
592851
592851
=item B<--filter REGULAR_EXPRESSION>
592851
592851
Save only modules whose file name matches the B<REGULAR_EXPRESSION>. The file
592851
names are compared without B<--libdir> prefix. The expession is not anchored
592851
by default. Empty expression matches any file name. Default value is empty
592851
regular expression, i.e. to save all modules.
592851
592851
=back
592851
592851
=head1 VERSION
592851
592851
This is version 2.
592851
592851
=head1 COPYRIGHT
592851
592851
Copyright © 2013, 2014  Petr Písař <ppisar@redhat.com>.
592851
592851
=head1 LICENSE
592851
592851
This is free software.  You may redistribute copies of it under the terms of
592851
the GNU General Public License L<http://www.gnu.org/licenses/gpl.html>.
592851
There is NO WARRANTY, to the extent permitted by law.
592851
592851
=cut