#!/usr/bin/perl 

use 5.008005;
use strict;
use warnings;
use File::Which  ();
use Getopt::Long ();
use Carp         ();

$| = 1;

if ($ENV{PADRE_DIE}) {
	$SIG{__DIE__} = sub { print STDERR Carp::longmess "\nDIE: @_\n" . ("-" x80) . "\n" };
}

# Must run using wxPerl on OSX.
if ( $^O eq 'darwin' and $^X !~ m{/wxPerl\.app/} ) {
    my $perl = scalar File::Which::which('wxPerl');
    chomp($perl);
    if ( -e $perl ) {
        warn "spawning 'wxPerl' interpreter for OSX\n";
        system( $perl, '-S', $0, @ARGV );
    } else {
        warn "padre cannot find wxPerl executable (which it requires on OSX)\n" ;
    }
    exit 0;
}

# Handle special command line cases early, because options like --home
# MUST be processed before the Padre.pm library is loaded.
my $USAGE   = '';
my $VERSION = '';
my $HOME    = undef;
my $session = undef;
my $getopt  = Getopt::Long::GetOptions(
	'help'      => \$USAGE,
	'home=s'    => \$HOME,
	'session=s' => \$session,
	'version'   => \$VERSION,
);
if ( $USAGE or ! $getopt ) {
	print <<"END_USAGE";
Usage: $0 [FILENAMES]

--home=dir        Forces Padre's "home" directory to a specific location
--help            Shows this help message
--session=name    Open given session during Padre startup
--version         Prints Padre version and quits

END_USAGE
	exit(1);
}
if ( defined $HOME ) {
	$ENV{PADRE_HOME} = $HOME;
}

require threads;
require threads::shared;

$ENV{PADRE_PAR_PATH} = $ENV{PAR_TEMP}||'';

my %opts = (
    files   => \@ARGV,
    session => $session,
);

require Padre;

if ( $VERSION ) {
	print "Perl Application Development and Refactoring Environment $Padre::VERSION\n";
	exit(0);
}

my $app = Padre->new(%opts);
unless ( $app ) {
    # Single instance worked correctly
    exit(0);
}

# Start the application
$app->run;

# Copyright 2008-2009 The Padre development team as listed in Padre.pm.
# LICENSE
# This program is free software; you can redistribute it and/or
# modify it under the same terms as Perl 5 itself.
