#! /bin/sh

if [ "$1" = "check" ]; then
	CHECK=`nvram get wl_country`
	if [ "$CHECK" != "" ]; then
		echo "Don't run script"
		exit 0
	fi
fi

. /etc/linuxigd/WLANConfig11b

APSSID="$SSID"
APCHANNEL="$Channel"
if [ "$DataRate" = "1 & 2 Mbps" ]; then
	AP_RATESET=default
else
	AP_RATESET=all
fi
if [ "$WEPType" = "64bits" ] || [ "$WEPType" = "128bits" ]; then
	AP_WEP=restricted
else
	AP_WEP=off
fi

if [ "$WEPDefaultKey" = "Key1" ]; then
	AP_KEY=1
elif [ "$WEPDefaultKey" = "Key2" ]; then
	AP_KEY=2
elif [ "$WEPDefaultKey" = "Key3" ]; then
	AP_KEY=3
elif [ "$WEPDefaultKey" = "Key4" ]; then
	AP_KEY=4
else
	AP_KEY=1		#Rotation (Not supported in BCM4301)
fi

if [ "$AuthenticationMethod" = "Shared Key" ]; then
	AP_AUTH=1		# shared key only
else
	AP_AUTH=0		# Both
fi

AP_CLOSED="$x_BlockBCSSID"


WL_SSID=`nvram get wl_ssid`
[ "$WL_SSID" != "$APSSID" ] && nvram set wl_ssid="$APSSID"
WL_CLOSED=`nvram get wl_closed`
[ "$WL_CLOSED" != "$AP_CLOSED" ] && nvram set wl_closed="$AP_CLOSED"
WL_CHANNEL=`nvram get d11b_channel`
[ "$WL_CHANNEL" != "$APCHANNEL" ] && nvram set d11b_channel="$APCHANNEL"
WL_WEP=`nvram get wl_wep`
[ "$WL_WEP" != "$AP_WEP" ] && nvram set wl_wep="$AP_WEP"
WL_KEY=`nvram get wl_key`
[ "$WL_KEY" != "$AP_KEY" ] && nvram set wl_key="$AP_KEY"
WL_KEY1=`nvram get wl_key1`
[ "$WL_KEY1" != "$WEPKey1" ] && nvram set wl_key1="$WEPKey1"
WL_KEY2=`nvram get wl_key2`
[ "$WL_KEY2" != "$WEPKey2" ] && nvram set wl_key2="$WEPKey2"
WL_KEY3=`nvram get wl_key3`
[ "$WL_KEY3" != "$WEPKey3" ] && nvram set wl_key3="$WEPKey3"
WL_KEY4=`nvram get wl_key4`
[ "$WL_KEY4" != "$WEPKey4" ] && nvram set wl_key4="$WEPKey4"
WL_AUTH=`nvram get wl_auth`
[ "$WL_AUTH" != "$AP_AUTH" ] && nvram set wl_auth="$AP_AUTH"
WL_RATESET=`nvram get wl_rateset`
[ "$WL_RATESET" != "$AP_RATESET" ] && nvram set wl_rateset="$AP_RATESET"


### Static Variables

WL_COUNTRY=`nvram get wl_country`
[ "$WL_COUNTRY" != "Worldwide" ] && nvram set wl_country="Worldwide"
D11B_RATE=`nvram get d11b_rate`
[ "$D11B_RATE" != "0" ] && nvram set d11b_rate=0
D11B_FRAG=`nvram get d11b_frag`
[ "$D11B_FRAG" != "2346" ] && nvram set d11b_frag=2346
D11B_RTS=`nvram get d11b_rts`
[ "$D11B_RTS" != "2347" ] && nvram set d11b_rts=2347
D11B_DTIM=`nvram get d11b_dtim`
[ "$D11B_DTIM" != "3" ] && nvram set d11b_dtim=3
D11B_BCN=`nvram get d11b_bcn`
[ "$D11B_BCN" != "100" ] && nvram set d11b_bcn=100
D11B_PLCPHDR=`nvram get d11b_plcphdr`
[ "$D11B_PLCPHDR" != "long" ] && nvram set d11b_plcphdr=long

. /etc/linuxigd/DeviceSecurity11a

if [ "$ApplyDevices" = "Both" ] || [ "$ApplyDevices" = "802.11b only" ]; then
	WL_MACMODE=`nvram get wl_macmode`
	if [ "$ACLMethod" = Accept ]; then
		[ "$WL_MACMODE" != "allow" ] && nvram set wl_macmode=allow
	elif [ "$ACLMethod" = Reject ]; then
		[ "$WL_MACMODE" != "deny" ] && nvram set wl_macmode=deny
	else
		[ "$WL_MACMODE" != "" ] && nvram unset wl_macmode
	fi

	if [ "$ACLMethod" != Disable ]; then
		ACL_MAC=
		i=0
		while [ "$i" -lt $ACLListCount ]; do
			   eval MAC='$'MacAddr${i}
			   [ -n "$MAC" ] && MAC=`octetstr $MAC 1` && ACL_MAC="$MAC $ACL_MAC"
			   i=$(($i+1))
		done
		nvram set wl_mac="$ACL_MAC"
	fi
fi
