#!/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;
my $action = "";

#figure out envi home
$ENV{'ENVI_HOME'}=dirname(dirname(rel2abs($0)));

#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");

#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) ) {
		run_playlist($action);

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

}


