#!/usr/bin/perl

use strict;
use warnings;

use File::Find;

my $target = 'termpub';

open( my $standalone, '>', $target ) or die "Can't open target: $!\n";

print {$standalone} "#!/usr/bin/perl\n";

my @libraries = (
    qw(
      lib/App/termpub/Pager.pm
      lib/App/termpub/Epub.pm
      lib/App/termpub/Pager/HTML.pm
      lib/App/termpub/Pager/Text.pm
      lib/App/termpub/Epub/Chapter.pm
      lib/App/termpub/Hyphen.pm
      lib/App/termpub/NavDoc.pm
      lib/App/termpub.pm
      )
);

LIB:
for (@libraries) {
    open( my $fh, '<', $_ )
      or die "Can't open $_: $!\n";
    while (<$fh>) {
        next     if /^use App::termpub/;
        next     if /^eval "require/;
        next LIB if /^__END__/;
        s/^use parent 'App/use parent -norequire, 'App/g;
        next if /^=pod/ ... /^=cut/;
        print {$standalone} $_;
    }
}

my $script = 'script/termpub';

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

print {$standalone} "package main;\n";

while (<$fh>) {
    next if /^#!/;
    next if /^use App::termpub/;
    print {$standalone} $_;
}

chmod 0755, $target;

exit 0;
