#!/bin/sh -e

# Find the most suitable version of ndiswrapper-utils to use:
# - first check what API is required by currently installed kernel module
# - fallback to detection of latest utility version
utils_version() {
	UTILS_VERSION=$(/sbin/modinfo -F parm ndiswrapper 2>/dev/null | \
			sed -n 's/^utils_version:.*([^:]\+: \([0-9\.]\+\))$/\1/p')

	if [ "${UTILS_VERSION}" ]; then
		echo ${1}-${UTILS_VERSION}
	else
		for file in ${1}-[\.0-9][\.0-9]*; do
			[ -x "${file}" ] && echo ${file}
		done | sort -n -t - -k 2 | tail -1
	fi
}

NDISWRAPPER=$(utils_version /usr/sbin/ndiswrapper)
if [ -x "${NDISWRAPPER}" ]; then
	exec ${NDISWRAPPER} "${@}"
fi

if [ "${NDISWRAPPER}" ]; then
	echo "Error: ndiswrapper-utils-${NDISWRAPPER##*-} not installed!" 1>&2
else
	echo "Error: unable to find a version of ndiswrapper!" 1>&2
fi

exit 1
