if ( ! exists $ENV{'ENVI_HOME'} || ! exists $ARGV[0] ) {
	die "please run 'envi', not 'rt.pl'\n";
}

do "$ENV{'ENVI_HOME'}/lib/util.pl";

my $task = $ARGV[0]; shift(@ARGV);
my $task_executable = task_location($task) . "/task.pl";
my $task_options = task_location($task) . "/options.txt";

#set task home and name for the benefit of the task
$ENV{'TASK_HOME'} = task_location($task);
$ENV{'TASK_NAME'} = $task;

#setup the task environment
if ( -f system_task_environment_script($task) ) {
	envi_message( "getting $task system env");
	my $v = system_task_environment_script($task);
	do $v;
}
if ( -f user_task_environment_script($task) ) {
	envi_message( "getting $task user env");
	my $v = user_task_environment_script($task);
	do $v;
}

#do what they asked:
if ( $ARGV[0] eq "env" ) {

	shift(@ARGV);
	
	# - run something in the task 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";
		}
	}

# - show task options
} elsif ( $ARGV[0] eq "options" ) {
	if ( -f "$task_options" ) {
	    open( FILE, "< $task_options" ) or die "Can't open $filename : $!";
	    while( <FILE> ) {
	        print;
    	}
	    close FILE;
	}

# - run the task
} else {
	my $command = "perl \"$task_executable\"";
	for my $a ( @ARGV ) {
		$command .= " \"$a\"";
	}

	envi_message( "task command: $command" );
	system( $command );
}
