#!/bin/sh

############################################################
# Copyright (c) 2009-2012 by Aleksey Cheusov
#
# See LICENSE file in the distribution.
############################################################

# Portable replacement for which(1)

failure=1
while getopts x f; do
    case "$f" in
	x)
	    failure=0;;
	*)
	    exit 2;;
    esac
done
shift `expr $OPTIND - 1`

if test $# -ne 1; then
    cat <<EOF
usage: mkc_which [-x] program
  -x -- exit status is always 0
EOF
    exit 1
fi

if echo "$1" | grep '^/' > /dev/null; then
    if test -x "$1"; then
	echo "$1"
	exit 0
    fi
else
    for i in `echo $PATH|tr : ' '`; do
	if test -x "$i/$1" -a -f "$i/$1"; then
	    echo "$i/$1"
	    exit 0
	fi
    done
fi

echo "Cannot find $1" >&2
exit "$failure"
