#!/bin/sh

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

# Portable replacement for which(1)

if test "$1" = "-x"; then
    failure=0
    shift
else
    failure=1
fi

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
    echo $1
    exit 0
fi
for i in `echo $PATH|tr : ' '`; do
    if test -x "$i/$1" -a -f "$i/$1"; then
	echo $i/$1
	exit 0
    fi
done

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