#!perl

# Quickie utility for squashing together the parts for the standalone version

use warnings;
use strict;

use File::Next;

for my $arg ( @ARGV ) {
    my $filename = $arg;
    if ( $arg =~ /::/ ) {
        my $key = "$arg.pm";
        $key =~ s{::}{/}g;
        $filename = $INC{$key} or die "Can't find the file for $arg";
    }

    warn "Reading $filename\n";
    open( my $fh, '<', $filename ) or die "Can't open $filename: $!";

    while ( <$fh> ) {
        next if /^use (File::Next|App::Ack)/;

        # See if we're in module POD blocks
        my $skip = ($filename ne 'ack') && (/^=/ .. /^=cut/);
        if ( !$skip ) {
            # Replace the shebang line
            s{^#!.+}{#!/usr/bin/env perl};

            # Remove Perl::Critic comments.
            # I'd like to remove all comments, but this is a start
            s{\s*##.+critic.*}{};

            print;
        }
    }
    close $fh;
}

exit 0;
