#!/bin/sh
# $Id: pathsearch,v 1.3 2000/01/18 19:01:14 moritz Exp $
# Search for an executable in PATH and some more directorys.
# Output a line in the form 'NAME path' for a subst config file
# Arguments: $1 is the program we search, $2 is a NAME for the subst
# config file, $3 - optional - an output file (default: stdout)

PATH=/bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin:/usr/local/sbin:$PATH
export PATH

# do not continue unless at least two non-zero length arguments are given
if [ -z "$1" ] || [ -z "$2" ] ; then
	# print error message to stderr
	echo "$0: Usage: $0 program NAME [subst-config-file]" >&2
	exit 1
fi

# "which" and "type -path" are both bash - we want to be sh compatible
IFS=":"
PROG=`for p in $PATH; do if [ -x $p/$1 ]; then echo "$p/$1"; break; fi; done`

if [ -z "$3" ] ; then
	# output to stdout
	echo "$2 $PROG"
else
	# append to subst-config-file
	echo "$2 $PROG" >>$3
fi
exit 0
