#!/bin/sh
#####
# NAME
#	search-exec - search executable file
# VERSION
#	$Id$
# CHANGELOG
#	$Log$
# SYNOPSYS
#	search-exec [option] file
#  OPTION
#	-n : do not output any messages
# NOTE
#	This command replace "which" command in shell script.
#	The which command does not return error code, when the executable
#	file is not found and display needless error message.

PRINT="YES"

error(){
	if test $PRINT = "YES" ; then
		echo "search-exec [ERROR] $1" 1>&2 
	fi
	exit 1 
}

if test "X$1" = "X-n" ; then
	PRINT="NO"
	BINNAME=$2
else
	BINNAME=$1
fi

if test "X${BINNAME}" = "X" ; then
	error "file name to search is required"
fi

PATH_LIST=`echo $PATH | sed 's/:/ /g'` 
for ONEPATH in $PATH_LIST ; do
	# echo "search $ONEPATH/$BINNAME"
	if test -x $ONEPATH/$BINNAME ; then
		if test $PRINT = "YES" ; then
			echo $ONEPATH/$BINNAME
		fi
		exit 0 ;
	fi
done

# the target file is not found
exit 1 ;

