#!/afs/athena/contrib/perl5/perl

$prompt = "; ";
$readline::rl_completion_function = "main::complete";

use Term::ReadLine;

@path = split(":", $ENV{'PATH'});
$term = new Term::ReadLine 'readlineish';
while(1){
    $inp = $term->readline("$prompt");
    if(substr($inp, 0, 2) eq ": "){
	eval(substr($inp, 2));
    }
    elsif(&internal_cmd($inp)){
    }
    elsif(!&trytoexec($inp)){
	print("I don't think so.\n");
    }
}

sub complete {
    my($t, $a, $s) = @_;
    "wowzers $t, $a, $s";
}

sub trytoexec {
    my($c) = @_;
    my($first, $second);

    (@cmd) = split(" ", $c);
   
    foreach $p (@path){
	if(-e "$p/$cmd[0]"){
	    $cmd[0] = "$p/$cmd[0]";
	    if(fork()) {
		wait;
		return 1;
	    }
	    else {
		exec(@cmd);
		exit;
	    }
	}
    }
}

sub internal_cmd {
    my($c) = @_;

    ($cmd, @opts) = split(" ", $c);
    if($cmd eq "cd"){
	if($opts[0]){
	    $opts[0] =~ s/\~/$ENV{'HOME'}/;
	    chdir($opts[0]);
	}
	else{
	    chdir($ENV{'HOME'});
	}
	return 1;
    }
    if($cmd eq "exit"){
	exit;
    }
}


