#!/bin/ksh93

typeset -r FPROG=${.sh.file}
typeset -r PROG=${FPROG##*/}
SDIR=${FPROG%/*}

function showUsage {
	[[ -z $1 ]] && X='--man' || X='-?'
	getopts -a ${PROG} "${ print ${USAGE} ; }" OPT $X
}

USAGE='[-1.0 ]
[-author?Hans Fredrik Nordhaug <hans.fredrik@nordhaug.no> (original)]
[-author?Thomas Esser (2003, 2004).]
[-author?Jens Elkner (2015 ksh93 port).]
[-license?Public domain.]
[+NAME?'"${PROG}"' - where is an extension to kpsewhich]
[+DESCRIPTION?'"${PROG}"' is an extension to kpsewhich (as where is for which in tcsh). The intention is to provide a way to check for conflicts/shadowed \acommand\as. It uses \bkpsewhich\b(1) to iterate over each texmf tree listed in \b$TEXMF\b separately and emits the location if a given \acommand\a is found.]
[h:help?Print this help and exit.]
[+BUGS?conflicts/shadowed files whithin each texmf tree are not found.]
\n\n[\akpsewhich_option\a]... \acommand\a ...
'
unset OPTS; OPTS=
while [[ -n $1 ]]; do
	[[ $1 =~ ^-(h|-help) ]] && showUsage && exit 0
	[[ ${1:0:1} == '-' ]] && OPTS+=( "$1" ) || break
	shift
done

[[ -z $1 ]] && showUsage 1 && exit 1

while [[ -n $1 ]]; do
	ALL=${ kpsewhich --expand-path='$TEXMF' ; }
	for P in ${ALL//:/ } ; do
		TEXMF="$P" kpsewhich "${OPTS[@]}" "$1"
	done
	shift
done
exit 0
