#!/bin/bash
#
# Copyright (c) 2003-2004 Linuxant inc.
#
# NOTE: The use and distribution of this software is governed by the terms in
# the file LICENSE, which is included in the package. You must read this and
# agree to these terms before using or distributing this software.
# 
# This script tries to unload all @CNXTTARGET@-related modules present in the system
#
PATH=/usr/sbin:/sbin:/usr/bin:/bin:/usr/local/sbin:/usr/local/bin
export PATH

for mod in @CNXTMODS@; do
	if [ "${mod}" = "dgcusbdcp" ]; then
		killall dgcdcpd 2>/dev/null
	fi

	if [ "${mod}" = "hsfhda" ]; then
		# need to rmmod bus controller module first
		busmod="`lsmod | grep '^snd_hda_codec ' | sed 's/.* //' | tr ',' '\012' | grep '^snd_hda_' | head -n 1`"
		if [ -n "${busmod}" ]; then
			rmmod ${busmod} 2>&1 | sed 's/^ERROR:/Warning:/' 1>&2
		fi
	fi

	if lsmod | grep -q "^${mod} "; then
		rmmod ${mod} 2>&1 | sed 's/^ERROR:/Warning:/' 1>&2
		if [ ${PIPESTATUS[0]} -ne 0 ]; then
			exit 1
		fi
	fi

	if [ "${mod}" = "dgcusbdcp" ]; then
		# need to rmmod cdc_acm module
		acmmod="`lsmod | grep '^cdc_acm ' | sed 's/ .*//' | head -n 1`"
		if [ -n "${acmmod}" ]; then
			rmmod ${acmmod} 2>&1 | sed 's/^ERROR:/Warning:/' 1>&2
		fi
	fi
done

if [ -n "${busmod}" ]; then
		rmmod snd_hda_codec 2>&1 | sed 's/^ERROR:/Warning:/' 1>&2
fi

exit 0
