print "${BGreen}Loading action.pl\n";

$ActionSyntax = 
	"${BBlue}list        -- ${White}action\n".
	"${BBlue}create      -- ${White}action \"<trigger>\" <action>\n".
	"${BBlue}remove      -- ${White}action <number> d\n".
	"${BBlue}disable one -- ${White}action <number> off\n".
	"${BBlue}enable one  -- ${White}action <number> on\n".
	"${BBlue}toggle all  -- ${White}action <off|on>\n";

$bActionsEnabled = 1;
$bMatchMultiple  = 1;
$booting = 1;

%Actions = ();

sub add_action($$) {
	($trigger, $action) = @_;

	if (($trigger eq "") || ($action eq "")) {
		print "$ActionSyntax" unless $booting;
		return;
	}
	$Actions{$trigger} = $action;
	print "\"$trigger\" ==> $action\n" unless $booting;	
}

sub rem_action($) {
	my $num = shift;
	$num--;
	my @act = sort keys %Actions;
	my $max = $#act;


	if ($num > $max || $num < 0) {
		$count = $max+1;
		print "You have only $count actions defined.\n";
		return;
	}
	else {
		$trigger = $act[$num];
		delete $Actions{$trigger};
		print "\"$trigger\" removed.\n";
	}
}

sub lst_action {
	print "${BBlue}DEFINED ACTIONS:\n";
	print "${BBlue}----------------\n";

	my $i = 1;
	foreach $trigger (sort keys %Actions) {
		if ($trigger =~ /^\#\#/) {
			my $trig = substr($trigger, 2, length($trigger));
			print "${BRed}$i] ${White}$trig ==> $Actions{$trigger}\n";
		}
		else {
			print "${BRed}$i] ${BGreen}$trigger ${BYellow}==> ${BBlue}$Actions{$trigger}\n";
		}
		$i++;
	}
	if ($bActionsEnabled == 0) {
		print "${BRed}Actions are currently disabled.\n";
	}
}

sub hook_action {
	return unless ($bActionsEnabled == 1);
	my $line = $_;
	$line =~ s/(\xEA.|\xEA\n)//g;
	foreach $trigger (sort keys %Actions) {
		if ($trigger =~ /^\#\#/) {
			# do nothing ## == disabled
		}
		elsif (/$trigger/) {			
			my $action = eval "\"$Actions{$trigger}\"";
			run("$action");
			return if ($bMatchMultiple == 0);
		}
	}
}

sub cmd_action {
	
	my $arg = $_;
	$arg =~ s/\n//g;
	
	if (/^$/) {
		lst_action();
	}
	elsif (/^off$/) {
		$bActionsEnabled = 0;
		save_actions() unless $booting;
		print "${BBlue}Actions ${BRed}disabled.\n";
	}
	elsif (/^on$/) {
		$bActionsEnabled = 1;
		save_actions() unless $booting;
		print "${BBlue}Actions ${BGreen}enabled.\n";
	}
	elsif (/^(\d+) d/) {
		rem_action($1);
		save_actions() unless $booting;
	}
	elsif (/^(\d+)\s+off$/) {
		my @temp = sort keys %Actions;
		if (($1 > $#temp+1) || ($1 < 1)) {
			print "Invalid action number.\n";
			return;
		}
		my $trig = $temp[$1 - 1];
		my $action = $Actions{$trig};
		delete $Actions{$trig};
		$Actions{"##$trig"} = $action;
		print "\"$trig\" disabled.\n";
		save_actions();
	}
	elsif (/^(\d+)\s+on$/) {
		my @temp = sort keys %Actions;
		if (($1 > $#temp+1) || ($1 < 1)) {
			print "Invalid action number.\n";
		    return;
		}
		my $trig = $temp[$1 - 1];
		my $action = $Actions{$trig};
		delete $Actions{$trig};
		$trig = substr($trig, 2, length($trig));
		$Actions{$trig} = $action;
		print "\"$trig\" enabled.\n";
		save_actions();
	}
	elsif (/^\"(.*)\"\s+(.+)$/) {
		add_action($1, $2);
		save_actions() unless $booting;
	}
	else {
		print "$ActionSyntax";
	}
}

sub save_actions {
	unlink("/home/estiann/.mcl/config.action");
    open(FH, ">/home/estiann/.mcl/config.action");
	if ($bActionsEnabled == 0) {
		print FH "off\n";
	}
    foreach (sort keys %Actions) {
        print FH "\"$_\" $Actions{$_}\n";
    }
	close FH;
}  

prompt_add(\&hook_action);
output_add(\&hook_action);

# LOADING ACTIONS:

if (open(FH, "</home/estiann/.mcl/config.action")) {
	while (<FH>) {
		if (/^off$/) {
			$bActionsEnabled = 0;
		}
		elsif (/^\"(.*)\"\s+(.+)$/) {
			add_action($1,$2);
		}
	}
	close FH;
}
$booting = 0;

1;



