#!/afs/athena/contrib/perl/p

require '/mit/mkgray/perl/line_edit.pl';

@path_items = split(':', $ENV{'PATH'});

$prompt = '; ';
while(1){
    $evaled=0;	
    $foo = &line_edit'get("$prompt");
    while($foo =~ /{{(.*)}}/g){
	$peval_me = $1;
	$result = eval($peval_me);
	$foo =~ s/{{(.*[^}])}}/$result/;
	$evaled = 1;
    }
    ($cmd, @options) = split(/[ \n\t]+/, $foo);
    if(defined(&$cmd)){
	&$cmd(@options);
    }
    elsif($pcmd=&inpath($cmd)){
	if($pid=fork()){
	    wait;
	}
	else{
	    exec($pcmd, @options);
	}
    }
    elsif(-e $cmd){
	if($pid=fork()){
	    wait;
	}
	else{
	    exec($path{$cmd}, @options);
	}
    }
    else{
	print("Say what?\n") unless $evaled;
	print("\nOK.\n") if $evaled;
    }
}

sub cd {
    if($#_>0){
	print "Too many arguments\n";
    }
    elsif(!chdir($_[0])){
	if(!chdir(readlink($_[0])||'/nowhere')){
	    if(-e $_[0]){
		print "$_[0] is not a direcotry\n";
	    }
	    else{
		print "Huh?\n";
	    }
	}
    }
}

sub where {
    local(@items) = @_;
    local($whereitem);
    for $item (@items){
	print(&inpath($item)."\n");
    }
}	


sub inpath {
    local($name) = @_;

    for $path_dir (@path_items){
	if(stat("$path_dir/$name")){
	    return("$path_dir/$name");
	}
    }	
}
