#!/bin/sh

if [ -z "$1" ]; then
	if [ -e /lib/modules/`uname -r`/source ]; then
		KERN=/lib/modules/`uname -r`/source
	else
		KERN=/lib/modules/`uname -r`/build
	fi
else
	KERN="$1"
fi

echo ${KERN} | grep "/\$" || KERN="${KERN}/"

ask_comment()
{
        while true; do
		echo -n "Above definitions found.  Comment out? [y], n "
		read REPLY
                case ${REPLY} in

		n) return 1;;

		""|y) 	sed -i \
				-e "s:^\(CONFIG_IPW2.*\):#\1:" \
				${KERN}/.config || return 1
			sed -i -r \
				-e "s:^(#(un)?def.*CONFIG_IPW2.*):/*\1*/:" \
				${KERN}/include/linux/autoconf.h || return 1
			return 0;;

		*) continue;;
                esac
	done || return 1
}

ask_remove()
{
        while true; do
		echo -n "Above files found.  Remove? [y],n "
		read REPLY

                case ${REPLY} in

		n) return 1;;

		""|y)	find ${KERN} -type f -name 'ipw2*' | \
			while read fn; do
				[ ! -d $fn ] && (rm -f ${fn} || return 1)
			done || return 1
			return 0;;

		*) continue;;
                esac
	done || return 1
}

do_check()
{
	echo "Checking in ${1} for ipw2x00 components..."

	FILES=`find ${1} -type f -name 'ipw2*'`

	if [ -n "${FILES}" ]; then
		for file in $FILES; do
			echo "${file}"
		done
		if [ -x unload ]; then
			./unload
		fi
		ask_remove || return 1
	fi

	( egrep "^(CONFIG_IPW2.*)" ${1}/.config || \
		egrep "^#(un)?def.*(CONFIG_IPW2.*)" \
			${1}/include/linux/autoconf.h ) && \
			(ask_comment || return 1)

	return 0
}

do_check ${KERN}
