mtk_wifi_e2p_die() {
	echo "mtk wifi eeprom:" "$*"
}

mtk_wifi_e2p_extract() {
	local part=$1
	local offset=$2
	local count=$3
	local mtd

	. /lib/functions.sh

	mtd=$(find_mtd_part $part)
	mtd=`echo $mtd | sed -e 's/mtdblock/mtd/'`
	mmc=`blkid -t PARTLABEL=factory -o device`

	if [ -n "$mtd" ]
	then
		input_dev=$mtd
	elif [ -n "$mmc" ]
	then
		input_dev=$mmc
	else
		mtk_wifi_e2p_die "no flash device found for partition $part"
		return
	fi

	dd if=$input_dev of=/lib/firmware/$FIRMWARE bs=$count skip=$offset count=1 2>/dev/null
}

check_e2p() {
	[ -f "/etc/wireless/l1profile.dat" ] || return
	# first line "Default" is illegal in shell
	cat /etc/wireless/l1profile.dat | grep "EEPROM" > /tmp/l1profile.sh

	. /tmp/l1profile.sh

	[ "$FIRMWARE" != "" ] || FIRMWARE=$INDEX0_EEPROM_name
	FW="/lib/firmware/$FIRMWARE"
	[ -e "$FW" ] && return

	# This is the ideal way, but driver does not follow...
	# case "$FIRMWARE" in
	# $INDEX0_EEPROM_name)
	# 	mtk_wifi_e2p_extract "Factory" `printf "%d" $INDEX0_EEPROM_offset` `printf "%d" $INDEX0_EEPROM_size`
	# 	;;
	# $INDEX1_EEPROM_name)
	# 	mtk_wifi_e2p_extract "Factory" `printf "%d" $INDEX1_EEPROM_offset` `printf "%d" $INDEX1_EEPROM_size`
	# 	;;
	# $INDEX2_EEPROM_name)
	# 	mtk_wifi_e2p_extract "Factory" `printf "%d" $INDEX2_EEPROM_offset` `printf "%d" $INDEX2_EEPROM_size`
	# 	;;
	# esac

	# Then here's the compromised way

	case "$FIRMWARE" in
	$INDEX2_EEPROM_name)
		l1_e2p_offset=`printf "%d" $INDEX2_EEPROM_offset`
		l1_e2p_size=`printf "%d" $INDEX2_EEPROM_size`
		final_size=`expr $l1_e2p_offset + $l1_e2p_size`
		mtk_wifi_e2p_extract "Factory" 0 $final_size
		;;
	$INDEX1_EEPROM_name)
		l1_e2p_offset=`printf "%d" $INDEX1_EEPROM_offset`
		l1_e2p_size=`printf "%d" $INDEX1_EEPROM_size`
		final_size=`expr $l1_e2p_offset + $l1_e2p_size`
		mtk_wifi_e2p_extract "Factory" 0 $final_size
		;;
	$INDEX0_EEPROM_name)
		l1_e2p_offset=`printf "%d" $INDEX0_EEPROM_offset`
		l1_e2p_size=`printf "%d" $INDEX0_EEPROM_size`
		final_size=`expr $l1_e2p_offset + $l1_e2p_size`
		mtk_wifi_e2p_extract "Factory" 0 $final_size
		;;
	esac

	rm -f /tmp/l1profile.sh
}


boot_hook_add preinit_main check_e2p
