#!/bin/sh
# $Id: vile-libdir-path,v 1.3 2018/10/31 00:11:51 tom Exp $
# Find the given vile helper-program, either in $PATH, or in a related
# lib-directory.  If the program is already in $PATH, echo the existing
# $PATH, but if it is found in a related lib-directory, prepend that to $PATH

failed() {
	echo "?? $*" >&2
	exit 1
}

HELPER=vile-manfilt
test $# != 0 && HELPER="$1"

OK_BIN=no
OK_LIB=

SAVE="$IFS"
IFS=':'
for dir in ${VILE_LIBDIR_PATH:-$PATH}
do
	if test -f $dir/$HELPER
	then
		OK_BIN=yes
		break
	elif test -z "$OK_LIB"
	then
		test -f $dir/vile || continue
		head=${dir%/*}
		for libs in \
			$head/lib \
			$head/lib[1-9]* \
			$head/lib/*vile \
			$head/lib[1-9]*/*vile
		do
			test -d "$libs" || continue
			if test -f $libs/$HELPER
			then
				OK_LIB=$libs
			fi
		done
	fi
done
IFS="$SAVE"

if test $OK_BIN = yes
then
	echo "$PATH"
elif test -n "$OK_LIB"
then
	echo "$OK_LIB:$PATH"
else
	failed "cannot find $HELPER"
fi
