#!/bin/sh
# Original copyright (C) 2002, Earnie Boyd
#   mailto:earnie@users.sf.net
# This implementation copyright (C) 2006, Keith Marshall
#   mailto:keithmarshall@users.sf.net
#
# This file is part of MSYS
#   http://www.mingw.org/msys.shtml
#
# File: which
# $Id: which,v 1.2 2006/02/11 10:43:06 keithmarshall Exp $

CMD=`IFS='\\/:'; set CMD $0; eval echo \$\{$#\}`
if test $# -lt 1
then
  echo >&2 "$CMD: syntax error: missing argument"
  echo >&2 "Usage: $CMD [ -a | --all ] cmd ..."
  exit 1
fi

break=break
for PROG
do
  if test x"$PROG" = x-a || test x"$PROG" = x--all
  then
    break=""
  else
    WHICH=""
    # need `type -ap -- "$PROG" || type -p -- "$PROG"'
    # because `type -ap foo' reports nothing, if both `foo' and `foo.exe'
    # are present, and are distinct.
    for LIST in `type -ap -- "$PROG" || type -p -- "$PROG"`
    do
      if test -f "$LIST"
      then
	# preserve `.exe' extension
        WHICH="$LIST"`test -f "$LIST.exe" && echo '.exe'`
	if test "$LIST" != "$WHICH"
	then
	  # detect distinct `foo' and `foo.exe'
	  INODE1=`ls -id "$LIST"` INODE2=`ls -id "$WHICH"`
	  if test `set ref $INODE1; echo $2` != `set ref $INODE2; echo $2`
	  then
	    # `foo' matches first, followed by `foo.exe'
	    test -z "$break" && echo "$LIST" || WHICH="$LIST"
	  fi
	fi
	echo "$WHICH"
	$break
      fi
    done
    test x"$WHICH" = x && echo >&2 "$CMD: $PROG: "${ERROR="unknown command"}
  fi
done
test ${ERROR+set} && exit 1
exit 0

# $RCSfile: which,v $: end of file
