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