#!/usr/bin/perl

############################################################
#
# Simple assistant for use with wxPerl and PAR
#
# Copyright (c) 2006 Mark Dootson mdootson@cpan.org
#
############################################################

=head1 NAME

wxpar

=head1 VERSION

Version 0.03

=cut

=head1 SYNOPSIS

    PAR assistant

    At the start of your script ...

    #!c:path/to/perl.exe
    BEGIN { use Wx::Package::Win32; }
    .....

    Then to start pp run 'wxperlpp' exactly as you would run pp.
    
    e.g.  wxperlpp --gui --icon=myicon.ico -o myprog.exe myscript.pl
    
    
=head1 DESCRIPTION

    A module to assist packaging Wx based applications with PAR, 
    ActiveState PerlApp / PDK and Perl2Exe. All that is needed is 
    that you include a 'use' statement as the first item in your 
    BEGIN blocks. For Perl2Exe, an additional 'use' statement 
    outside any BEGIN block ensures correct object cleanup.
    

=cut

my @args = @ARGV;
require Wx;
if($Wx::VERSION < 0.40) {
	my ($wxdir,$wx1,$wx2, @wx28dlls);
	foreach my $path(@INC) {
	    if(-f $path . '/auto/wx/Wx.dll') {
		$wxdir = $path . '/auto/wx/';

		$wxdir =~ s/\\/\//g;
		my $lib = ( glob "$wxdir/wx*_html*.dll" )[0];
		$lib =~ s{.*[/\\]([^/\\]+)$}{$1};
		$lib =~ m/^wx(?:msw)([^_]+)_html_([^\.]+)\.dll/i;
		$wx1 = $1;
		$wx2 = $2;
		last;
	    }
	}

	$wxdir =~ s/\/$//; 
	opendir(DIR, $wxdir);
	@wxdlls = grep { /^wx(base|msw)${wx1}(.+)${wx2}\.dll$/ } readdir(DIR);
	closedir(DIR);
	for my $dllname(@wxdlls) {
	    my $filepath = $wxdir . '/' . $dllname;
	    $filepath =~ s/\\/\//g;
	    unshift(@args, $filepath);
	    unshift(@args, '-l');

	}

	
} else {
	require Wx::Mini;

	my $wxdir = $Wx::wx_path;
	my $wxdlls = $Wx::dlls;

	foreach my $dllname(keys(%$wxdlls)) {
	    my $filepath = $wxdir . '/' . $wxdlls->{$dllname};
	    $filepath =~ s/\\/\//g;
	    unshift(@args, $filepath);
	    unshift(@args, '-l');

	}
}

my $command = 'pp';
my $arglist = '';

for my $argument (@args) {
    if($argument =~ /\s/) {
        $arglist .= '"' . $argument . '" ';
    } else {
        $arglist .= $argument . ' ';
    }
}

print qq(\nRunning Command .....\n\n);
print qq($command $arglist\n);
print qq(\n PLEASE WAIT .....\n);

system $command, @args;

1;

__END__

