#!/bin/env perl
use lib './lib';

use App::Packer::Temp;
use Data::Dumper;

use Getopt::Long;

Getopt::Long::Configure("no_ignore_case");

my $opt = {};

GetOptions
(
	$opt,
	'a|extra:s@',			# files/directory to add to pack
	'A|listextra',				# list of files to add
	'e|extractdir',				# extract directory (without it, is input from directory?)
	'o|ouptut:s'				# output file
);

$opt->{a} ||= [];

push(@{$opt->{a}}, @ARGV ) if (@ARGV);
@ARGV = ();
die(<<"EOF") if (!@{$opt->{a}} && !$opt->{A});

Usage: 

	$0 <filelist>
		-A <file_with_filenames> 
		-a <file1> -a <file2> 

where each file listed is of the format: '<real_file> <entry_in_archive>' or 
'<real_file>'. If the first format, the archive will use <entry_in_archive> 
as the entry list, if the second, it will use <real_file>

If the file is a directory, then all files inside that directory will be added.
All links in directories you add will become files.

EOF

my $pack = new App::Packer::Temp
					(
						frontend 	=> 'Module::ScanDeps',
						backend	 	=> 'App::Packer::Backend::PAR',
						backopts	=> 
							{
								'compile' 	=> 1,
								'noscan'	=> 1,
								'extra'		=> $opt->{a},
								'listextra'	=> $opt->{A},
								'output'	=> $opt->{o} || 'ext.out'
							},
						args		=> \@ARGV
					);

my $af = $pack->add_manifest();
my $string = _getstring($af, $opt);

$pack->add_back_options(eval => $string);
$pack->generate_pack();

sub _getstring
{
	my ($af, $opt) = @_;

	
	my $files = "@$af";

	my $code = <<"EOF";
use PAR;
use Archive::Zip;

	my \$prefix = \$ARGV[0] || ".";

	my \$zip = PAR::par_handle(\$0);
	my \$file;
	foreach \$file (qw ($files))
	{
		print STDERR "Extracting \$file => \$prefix/\$file\n";
		\$zip->extractMember("\$file", "\$prefix/\$file");
	}
EOF
}
