#!/usr/bin/env sh


usage()
{
	echo -e "Usage: $0 [COMMAND] [OPTION]"
	echo -e "This script is a command line interface to the karma madwifi patches"
	echo
	echo -e "  -s <on|off>       Set status"
	echo -e "  -c <black|white>  Set blacklist or whitelist mode"
	echo -e "  -a <SSID>         Add an SSID to the list"
	echo -e "  -r                Reset the list"
	echo -e "  -h                This help"
	echo
}

add_ssid()
{
	echo "Adding the SSID $1 to the list"
	iwpriv ath0 addkarmassid $1
}

clear_ssid()
{
	echo "Clearing the SSID list"
	iwpriv ath0 clrkarmalist 0
}

colour()
{
	if [[ "$1" == "white" ]]
	then
		echo "Enabling Whitelisting"
		iwpriv ath0 karma_bw 1
	elif [[ "$1" == "black" ]]
	then
		echo "Enabling Blacklisting"
		iwpriv ath0 karma_bw 0
	else
		usage
		exit 1
	fi
}

status()
{
	if [[ "$1" == "on" ]]
	then
		echo "Turning Karma On"
		iwpriv ath0 karma 1
	elif [[ "$1" == "off" ]]
	then
		echo "Turning Karma Off"
		iwpriv ath0 karma 0
	else
		usage
		exit 1
	fi
}

if [ -z "$1" ]
then
	usage
	exit 1 
fi

while getopts "hrs:c:a:" flag
do
	case $flag in
		s) status $OPTARG ;;
		c) colour $OPTARG ;;
		a) add_ssid $OPTARG ;;
		r) clear_ssid ;;
	   	h) usage; exit 1 ;;
	esac
done

