#!/usr/bin/csh -f
set prompt = "% "
if ( -f ~/.cshrc) then
	source ~/.cshrc
endif
if ( $#argv == 0 ) then
	set errmsg=`/usr/bin/dspmsg which.cat 3 'usage: which command...\n'`
	echo $errmsg | /usr/bin/sh -c '/usr/bin/cat - 1>&2'
	exit 1
endif
set noglob
set _exit_val = 0
foreach arg ( $argv )
    (alias $arg >& /dev/null)
    if ( $status == 0 ) then
	set alius = `alias $arg`
    else
	set alius = `;`
    endif
    switch ( $#alius )
	case 0 :
	    breaksw
	case 1 :
	    set arg = $alius[1]
	    breaksw
        default :
	    echo ${arg}: "	" aliased to $alius
	    continue
    endsw
    unset found
    if ( $arg:h != $arg:t ) then
	if ( -e $arg ) then
	    echo $arg
	else
	    set errmsg=`/usr/bin/dspmsg which.cat 1 '%1$s not found\n' "$arg"`
	    echo $errmsg | /usr/bin/sh -c '/usr/bin/cat - 1>&2'
	    set _exit_val = 1
	endif
	continue
    else
	foreach i ( $path )
	    if ( -x $i/$arg && ! -d $i/$arg ) then
		echo $i/$arg
		set found
		break
	    endif
	end
    endif
    if ( ! $?found ) then
	set errmsg=`/usr/bin/dspmsg which.cat 2 'no %1$s in %2$s\n' "$arg" "$path"`
	echo $errmsg | /usr/bin/sh -c '/usr/bin/cat - 1>&2'
	set _exit_val = 1
    endif
end
exit $_exit_val
