#!/bin/sh
# date 20121022
#
#
ARG1=$1
IS_K4=0

check_kindle_version()
{
	KINDLE_SN=`cat /proc/usid`

	temp=$(expr substr "$KINDLE_SN" 1 4)	
	case $temp in
	B00E)
		IS_K4=1
		KINDLE_INFO="Kindle 4 WiFi (K4)"
    ;;
	B023)
	IS_K4=1
	KINDLE_INFO="Kindle 4 WiFi Black (K4)"
    ;;
	9023)
	IS_K4=1
	KINDLE_INFO="Kindle 4 WiFi Black (K4)"
    ;;
	*)
		IS_K4=0
    ;;
	esac
}

stop_wifi_k4()
{
	ifconfig wlan0 down
	modprobe -r ar6003
	/etc/init.d/wifi stop
	/etc/init.d/wifid stop
}

start_wifi_k4()
{
	modprobe ar6003
	sleep 1
	ifconfig wlan0 up
}

wifi_k4()
{
	if [ "$ARG1" = "stop" ];then
		echo "stop_wifi_k4"
		stop_wifi_k4
	else
		echo "start_wifi_k4"
		start_wifi_k4
	fi
}

stop_wifi_k3()
{
	ifconfig wlan0 down
	modprobe -r ar6000
	/etc/init.d/wifi stop
	/etc/init.d/wifid stop
}

start_wifi_k3()
{
	/etc/init.d/wifi
	modprobe ar6000
	sleep 1
	ifconfig wlan0 up
}

wifi_k3()
{
	if [ "$ARG1" = "stop" ];then
		echo "stop_wifi_k3"
		stop_wifi_k3
	else
		echo "start_wifi_k3"
		start_wifi_k3
	fi
}

wifi_handle()
{
	check_kindle_version

	if [ $IS_K4 -ne 0 ] ; then
		wifi_k4
	else
		wifi_k3
	fi
}

wifi_handle
