#!/usr/bin/perl -w

use strict;
use File::Spec::Functions qw(rel2abs);
use File::Basename;
use Getopt::Long;

#unset the environment
BEGIN {
	if ( $^O eq "MSWin32" && exists $ENV{'HOMEDRIVE'} && exists $ENV{'HOMEPATH'} ) {
		$ENV{'HOME'} = $ENV{'HOMEDRIVE'} . $ENV{'HOMEPATH'};
	} elsif ( $^O eq "MSWin32" ) {
		$ENV{'HOME'} = "";
	} else {
		foreach my $key (keys %ENV) {
			#if ( $^O eq "MSWin32" && $key !~ "^(HOME|USERNAME|USERPROFILE|COMMONPROGRAMFILES|APPDATA|PROGRAMFILES|LOCALAPPDATA|HOMEPATH|USERDOMAIN|PROGRAMDATA|SYSTEMDRIVE|SYSTEMROOT|WINDIR|VS71COMNTOOLS|NUMBER_OF_PROCESSORS|OS|PATHEXT|PROCESSOR_ARCHITECTURE|PROCESSOR_LEVEL|PROCESSOR_REVISION|TEMP|TMP|PATH)\$" ) {
			#	delete($ENV{$key});
			#} elsif ( $key !~ "^(USER|HOME)\$" ) {
			if ( $key !~ "^(USER|HOME)\$" ) {
				delete($ENV{$key});
			}
		}
	}
}

#options and arguments
#my $envi_verbose = 0;
our $envi_verbose = 2; # set to 0 for no verboseness
my $action = "";
our $bitness;# = "32";
#figure out envi home
$ENV{'ENVI_HOME'}=dirname(dirname(rel2abs($0)));
print STDERR "ENVI_HOME: $ENV{'ENVI_HOME'}\n";

#import utility functions
do "$ENV{'ENVI_HOME'}/lib/util.pl";

#parse options
Getopt::Long::Configure('require_order'); #don't read options after the first argument
GetOptions (
	'v|verbose' => \$envi_verbose
	);

#parse arguments
if ( @ARGV > 0 ) {
	$action = $ARGV[0];
	shift(@ARGV);
}

#show options / arguments
envi_message( "verbose:    '$envi_verbose'" );
envi_message( "action:     $action");

if ($action =~ /-x64$/) {
    envi_message("64bit version");
    $bitness = "64";
}
#if ($action =~ /diffcol/) {
#    envi_message("running diffcol - assuming 64bit version");
#    $bitness = "64";
#}
else { # task could be 'release' or 'diffcol' and will not indicate bitness, as that is decided by the env
    my $os_name = $^O;
    $bitness = "64"; # default is true for darwin
    if ($os_name =~ "MSWin32") {
	$bitness = "32"; # assume 32 bit for windows unless determined to be otherwise
	my $curr_dir = $ENV{'ENVI_HOME'}; #dirname(rel2abs($0));
	while($curr_dir =~ m/envi/) { # test the folder name when we're one level above the envi folder
	    #print STDERR "**** Current dir is now $curr_dir\n";
	    $curr_dir = dirname(rel2abs($curr_dir));
	}
	#print STDERR "**** Current dir is now $curr_dir\n";
	if($curr_dir =~ m/-64bit$/) {
	    $bitness = "64";
	}

    } elsif ($os_name =~ "linux") {
	$bitness = `getconf LONG_BIT`;
    }
}

#set the environment
if ( -f "$ENV{'ENVI_HOME'}/etc/environment.pl" ) {
	envi_message( "Sourcing system environment" );
	do "$ENV{'ENVI_HOME'}/etc/environment.pl";
}

if ( -f "$ENV{'HOME'}/.envi/environment.pl" ) {
	envi_message( "Sourcing user environment");
	do "$ENV{'HOME'}/.envi/environment.pl";
}

#if no argument given, run default
if ( "$action" eq "" ) {
	print "------------------------------------------------------------\n";
	print "EnvI Version 1.0\n";
	print "Installed at: $ENV{'ENVI_HOME'}\n";
	print "Run 'envi tasks' for a list of tasks\n";
	print "Run 'envi TASKNAME' to run task with task with name TASKNAME\n";
	print "------------------------------------------------------------\n";

#if 'tasks' given as argument, show all available tasks
} elsif ( "$action" eq "tasks" ) {
	list_tasks();

#if 'playlists' given as argument, show all available playlists
} elsif ( "$action" eq "playlists" ) {
	list_playlists();

#if 'home' given as argument, show envi home
} elsif ( "$action" eq "home" ) {
	print "$ENV{'ENVI_HOME'}\n";

#if 'env' given as argument, show or use the envi environment
} elsif ( "$action" eq "env" ) {
	
	# - run something in the envi environment
	if ( exists $ARGV[0] ) {
		my $command = $ARGV[0] . " ";
		shift(@ARGV);
		for my $a ( @ARGV ) {
			$command .= " \"$a\"";
		}
		system( $command );

	# - show task environment
	} else {
		foreach my $key (keys %ENV) {
			print "$key=$ENV{$key}\n";
		}
	}

#if default specified explicitly, run it
} elsif ( "$action" eq "default" ) {
	run_default_tasks();

#if other argument given, run the specified task or playlist
} else {
    #if an existent playlist was specified, run it
    if ( playlist_exists($action) ) {
		envi_message( "Running playlist $action");
		run_playlist($action);

     #if an existent task was specified, run it
    } elsif ( task_exists($action) ) {
		envi_message( "Running task $action");
		run_task( $action );
	
	#otherwise, report no such task
	} else {
		print "no such task, playlist or option as '$action'\n"
	}

}


