#!/usr/bin/perl -w

# Be strict so we dont write messy code
use strict;

# Use FindBin module to get script directory
use FindBin;

# Use the Cwd module so we can find the current working directory
use Cwd;

# Get the $scriptdir
my $scriptdir = $FindBin::RealBin;

# Get the $cwd
my $cwd = getcwd;

# Load the required Wx modules
eval "use Wx::Perl::Packager";
eval "use Wx qw[:everything]"; print STDERR "WARNING: Wx module not found, API calls using WxWidgets will not work\n\n" if $@ && "@ARGV" !~ /--unixquery/;
eval "use Wx::XRC";
eval "use Wx::Html";
eval "use Wx::WebView"; print STDERR "WARNING: Wx::WebView module not found, API calls using Wx::WebView will not work\n\n" if $@ && "@ARGV" !~ /--unixquery/;

# Use LWP::Simple module to get website content (crossplatform) if it is available
eval "use LWP::Simple"; print STDERR "WARNING: LWP::Simple module not found, API calls using LWP::Simple will either use alternative methods\n\n" if $@ && "@ARGV" !~ /--unixquery/;

# Include the byte encryption so that the script will work on all localizations of windows or other systems
# Odd I know, but it is caused by the windows codepages (like chcp 1252 which is scandinavian)
use Encode::Byte;

# Include List::MoreUtils, IO::Stringy and Config::IniFiles for proper config filehandling
eval "use List::MoreUtils"; print STDERR "WARNING: List::MoreUtils module not found on the system,\nusing the version provided by the client instead!\n\n" if $@;
eval "use IO::Stringy"; print STDERR "WARNING: IO::Stringy module not found on the system,\nusing the version provided by the client instead!\n\n" if $@;
eval "use Config::IniFiles"; print STDERR "WARNING: Config::IniFiles module not found on the system,\nusing the version provided by the client instead!\n\n" if $@ && "$^O" !~ /darwin/;

# Include the module for extracting archives
eval "use Archive::Extract"; print STDERR "WARNING: Archive::Extract not module not found on the system,\nusing the version provided by the client instead!\n\n" if $@;

# If we are on windows
if ("$^O" =~ /MSWin32/)
{
	# Make sure some windows modules gets included
	eval "use Win32::API";
	eval "use File::DosGlob";
	eval "use Tie::Scalar";
	eval "use Win32::FileOp";
	
	# Hide the commandline windows
	eval "use Win32::GUI()";
	
	# If the arguments passed do not contain --showcmd=true
	#if ("$ARGV[-1]" =~ /(-|--)showcmd=false/)
	#{
		# Get the handle for the perl window
	#	my $loader_cmdwindow = Win32::GUI::GetPerlWindow();
		# Hide the cmd window
	#	Win32::GUI::Hide($loader_cmdwindow);
	#}
	# Else
	#else
	#{
		# Remove --showcmd=true or -showcmd=true from the arguments array
	#	s/(-|--)showcmd=true(\s)//g for @ARGV;
	#}
	
	# Move to the script directory
	chdir("$scriptdir");
	
	# Update $cwd
	$cwd = getcwd;
	
	# If $ARGV[0] does not contain any calls
	if("$ARGV[0]" !~ /(\.|::)/)
	{
		# Move first argument to l
		$ARGV[1] = $ARGV[0];
		
		# Make a call for the launcher
		$ARGV[0] = "client.launch.launcher";
	}
}
# Else
else
{
	# Move to parent directory
	chdir("$scriptdir/..");
	
	# Update $cwd
	$cwd = getcwd;
}

# "Chainload" the apiquery.pm
require "$cwd/apiquery.pm";
