#!/bin/sh
. /etc/functions.sh

load_kmodules_config_options()
{
	MFILE="/tmp/modules_skip"
	config_cb() 
	{
		if [ "$2" = "disable" ]; then
			option_cb()
			{
				ALL_DISABLE_VARIABLES="$ALL_DISABLE_VARIABLES $2"
			}
		else
			option_cb() { return 0; }
		fi
	}

	rm $MFILE 2>/dev/null
	touch $MFILE
	config_load kmodules
	for var in $ALL_DISABLE_VARIABLES
	do
		echo "$var" >> $MFILE	#output the modules to be skipped as a pattern file
		lsmod | grep -q $var && rmmod $var	&& logger -t kmodule_ctl "remove module: $var" # remove module
	done
	echo "===end_of_modules_to_be_remove_from_sbin_kmodule_ctl===" >> $MFILE
}
	
case "$2" in
        disable)
			sed 's/^[^#]/&/' /etc/modules.d/* | grep -q $1 || { echo "$1 not found"; exit 1; }
			uci -q set kmodules.disable.$1=$1
			uci -q del kmodules.enable.$1
			uci commit kmodules
			lsmod | grep -q $1 && rmmod $1
			;;
        enable)
			sed 's/^[^#]/&/' /etc/modules.d/* | grep -q $1 || { echo "$1 not found"; exit 1; }
			uci -q del kmodules.disable.$1
			uci -q set kmodules.enable.$1=$1
			uci commit kmodules
			lsmod | grep -q $1 || insmod $1			
			;;
        *) load_kmodules_config_options ;;	# remove modules found in config file
esac
	

#	config 'rm_modules' 'disable'
#		option 'nf_nat_sip' 'nf_nat_sip'
#		option 'nf_nat_sip' 'nf_conntrack_sip'
#	config 'ins_modules' 'enable'
   
