#!/bin/bash
#< Print versions of multiple programs passed as arguments

ECHO="/bin/echo"
EGREP="/usr/bin/egrep"
GREP="/usr/bin/grep"
SED="/usr/bin/sed"
WHICH="/usr/bin/which"

function print_error {
   ${ECHO} "Error: $@" >&2
}

for FILE in $@; do
	${ECHO} "${FILE}" | ${EGREP} -q '^(/|\.).*$'
	if [ "$?" -ne "0" ]; then
		${WHICH} ${FILE} 2>&1 | ${GREP} -q '^no'
		if [ "$?" -eq "0" ]; then
			print_error "${FILE}: not in path"
			exit 1
		else
		FILE=$( ${WHICH} ${FILE} )
		fi
	fi
   if [ ! -x "${FILE}" ]; then
      print_error "${FILE}: not executable" 
      exit 1
   fi
   # whether we have an absolute or relative path (or neither), we can just access ${FILE}
	# and it'll be available if we've got this far

	# only supports --version - as a lot of commands use -v for verbose
	RESULT=$( ${FILE} --version 2>&1 )
	if [ "$?" -ne "0" ]; then
		${ECHO} "Warning: ${FILE}: cannot get version" >&2
	else
		# print what we've got...
		${ECHO} "${RESULT}" | ${SED} -n '1p'
	fi
done

exit 0