#!/usr/bin/perl

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

local $| = 1;
local $SIG{__DIE__} =
	$ENV{PADRE_DIE}
	? sub { print STDERR Carp::longmess "\nDIE: @_\n" . ( "-" x 80 ) . "\n" }
	: $SIG{__DIE__};

# Must run using wxPerl on OS X.
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 OS X\n";
		system( $perl, '-S', $0, @ARGV );
	} else {
		warn "padre cannot find wxPerl executable (which it requires on OS X)\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 $PRELOAD     = undef;
my $DESKTOP     = undef;
my $ACTIONQUEUE = undef;
my $getopt      = Getopt::Long::GetOptions(
	'help|usage'    => \$USAGE,
	'version'       => \$VERSION,
	'home=s'        => \$HOME,
	'session=s'     => \$SESSION,
	'desktop'       => \$DESKTOP,
	'actionqueue=s' => \$ACTIONQUEUE,

	# Keep this secret for now --ADAMK
	'preload' => \$PRELOAD,
);

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
--desktop           Integrate Padre with your desktop
--actionqueue=list  Run a list of comma-seperated actions after Padre startup

END_USAGE
	exit(1);
}

local $ENV{PADRE_HOME} = defined($HOME) ? $HOME : $ENV{PADRE_HOME};

# Special execution modes
if ($VERSION) {
	my $msg = "Perl Application Development and Refactoring Environment $Padre::VERSION\n";
	if ( $^O eq 'MSWin32' and $^X =~ /wperl\.exe/ ) {

		# Under wperl, there is no console so we will use
		# a message box
		require Padre::Wx;
		Wx::MessageBox(
			$msg,
			Wx::gettext("Version"),
			Wx::wxOK(),
		);
	} else {
		print $msg;
	}
	exit(0);
}

if ($DESKTOP) {
	require Padre::Desktop;
	unless ( Padre::Desktop::desktop() ) {
		warn "--desktop not implemented for " . $^O . "\n";
	}
	exit(1);
}

require threads;
require threads::shared;

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

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

# If we have an action queue then we are running for automation reasons.
# Avoid the startup logic and continue to the main startup.
unless ( defined $ACTIONQUEUE ) {

	# Run the Padre startup sequence before we load the main application
	require Padre::Startup;
	unless ( Padre::Startup::startup() ) {

		# Startup process says to abort the main load and exit now
		exit(0);
	}
}

require Padre;

if ($PRELOAD) {

	# Load the entire application into memory immediately
	Padre->import(':everything');
}

# Build the application
my $app = Padre->new(%opts);
unless ($app) {
	die "Failed to create Padre instance";
}

# Start the application
$app->run;

# Copyright 2008-2010 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.
