#! /bin/sh
CFG_PATH="/lib/firmware"
FW_PRINTCONST=/sbin/fw_printconst

case "$1" in
start)
	ccode=`fw_printconst ccode | cut -f 2 -d '='`
	regrev=`fw_printconst regrev | cut -f 2 -d '='`
	country_list=`fw_printconst country_list | cut -f 2 -d '='`
	if [ -z "$ccode" -o -z "$regrev" ]; then
		echo "Country code and/or regulatory domain not written in the const partition! May lead to incorrect radio behavior."
	else
		cp $CFG_PATH/config.txt /tmp/config.txt
		cat /tmp/config.txt | grep -v "ccode" | grep -v "regrev" > $CFG_PATH/config.txt
		echo "ccode=$ccode" >> $CFG_PATH/config.txt
		echo "regrev=$regrev" >> $CFG_PATH/config.txt
		rm /tmp/config.txt
		echo "WiFi country and regulatory domain set to $ccode and $regrev"
	fi

	if [ -n "$country_list" ]; then
		cp $CFG_PATH/config.txt /tmp/config.txt
		cat /tmp/config.txt | grep -v "country_list" > $CFG_PATH/config.txt
		echo "country_list=$country_list" >> $CFG_PATH/config.txt
		rm /tmp/config.txt
	fi

	wifiregdom=$($FW_PRINTCONST -n wifiRegulatoryDomain)
	if [ $? == 1 ]; then
		wifiregdom=""
	fi

    # Example models: jbl_4305p, jbl_l75ms.
	model=$($FW_PRINTCONST -n model)

    # KR and IN are using CE for now.
	case "$wifiregdom" in
	"US")
		nvram_reg="fcc"
		# special handling for CA: the wifiregdom is US, but the nvram_reg is IC
		if [ "x$ccode" == "xCA" ]; then nvram_reg="ic"; fi
		;;
    "JP")
        nvram_reg="mic"
        ;;
    "TW")
        nvram_reg="ncc"
        ;;
	"CN")
		nvram_reg="srrc"
		;;
	"GB" | *)
		nvram_reg="ce"
		;;
	esac

	echo "[wifi-regulatory-domain] model: $model"
	echo "[wifi-regulatory-domain] regulatory domain: $wifiregdom"
	echo "[wifi-regulatory-domain] nvram regulatory domain: $nvram_reg"
	echo "[wifi-regulatory-domain] old search file: $CFG_PATH/nvram_ap6398s_"$nvram_reg"_"$model".txt"
	echo "[wifi-regulatory-domain] new search file: $CFG_PATH/nvram_ap6398sv_"$nvram_reg"_"$model".txt"

	if [ -f "$CFG_PATH/nvram_ap6398s_"$nvram_reg"_"$model".txt" ]; then
		echo "[wifi-regulatory-domain] found 'old' file match."
		model_old="_$model"
	fi
	if [ -f "$CFG_PATH/nvram_ap6398sv_"$nvram_reg"_"$model".txt" ]; then
		echo "[wifi-regulatory-domain] found 'new' file match."
		model_new="_$model"
	fi
    
	ln -sf "$CFG_PATH/nvram_ap6398s_$nvram_reg$model_old.txt" "/lib/firmware/nvram_bcm4359c0_ag.txt"
	ln -sf "$CFG_PATH/nvram_ap6398sv_$nvram_reg$model_new.txt" "/lib/firmware/nvram_ap6398sv.txt"

	# configure the Bluetooth firmwares
	if [ -f "/etc/firmware/BCM4362A2_fw_"$model".hcd" ]; then
		echo "[wifi-regulatory-domain] found 'BCM4362A2' file match."
		bt_model_old="_$model"
	fi
	if [ -f "/etc/firmware/BCM4359C0_fw_"$model".hcd" ]; then
		echo "[wifi-regulatory-domain] found 'BCM4359C0' file match."
		bt_model_new="_$model"
	fi

	ln -sf "/etc/firmware/BCM4362A2_fw$bt_model_old.hcd" "/etc/firmware/BCM4362A2.hcd"
	ln -sf "/etc/firmware/BCM4359C0_fw$bt_model_new.hcd" "/etc/firmware/BCM4359C0.hcd"
	;;

stop)
	echo "Nothing to do with WiFi country and regulatory domain"
	;;

restart)
	$0 stop
	$0 start
	;;

*)

	echo "Usage: /etc/init.d/init-wifi-country-region {start|stop|restart}"
	exit 1
esac
