#!/bin/sh

USB_WIFI_MAP="usb.wifi.table"
USB_ERROR_FILE="usb.wireless.error"
USB_BLUETOOTH_MAP="usb.bluetooth.table"
USB_BLUETOOTH_ERROR_FILE="usb.bluetooth.error"
KEY="USB_DEVICE("
VENDOR_INTERFACE_KEY="USB_VENDOR_AND_INTERFACE_INFO("
VERSION=`cat /source/backports/VERSION`
DRIVER_DIR="/source/backports/${VERSION}/drivers/net/wireless/"
RT2X00_DIR="${DRIVER_DIR}/rt2x00/rt2800usb.c"
RTL8192CU_DIR="${DRIVER_DIR}/rtl8192/os_dep/linux/usb_intf.c"
RTL8187_DIR="${DRIVER_DIR}/rtl818x/rtl8187/"
RTL8188EU_DIR="${DRIVER_DIR}/rtlwifi/rtl8188eu/os_dep/"
R8712U_DIR="${DRIVER_DIR}/rtl8712/os_intf/linux/"
RTL8812AU_DIR="${DRIVER_DIR}/rtl8812au/os_dep/linux/"
RTL8821AU_DIR="${DRIVER_DIR}/rtl8821au/os_dep/linux/"
CARL9170_DIR="${DRIVER_DIR}/ath/carl9170/"
ATH9K_DIR="${DRIVER_DIR}/ath/ath9k/"
ZD1211RW_DIR="${DRIVER_DIR}/zd1211rw/"
DRIVER_LIST="rt2x00 rtl8192cu rtl8187 rtl8188eu rtl8712 rtl8812au rtl8821au carl9170 ath9k zd1211rw"

BLUETOOTH_DIR="/source/backports/${VERSION}/drivers/bluetooth/"
BLUETOOTH_DRIVER_LIST="ath3k bcm203x bfusb bpa10x btusb"
BT_ATH3K_DIR="${BLUETOOTH_DIR}/ath3k.c"
BT_BCM203X_DIR="${BLUETOOTH_DIR}/bcm203x.c"
BT_BFUSB_DIR="${BLUETOOTH_DIR}/bfusb.c"
BT_BPA10X_DIR="${BLUETOOTH_DIR}/bpa10x.c"
BT_BTUSB_DIR="${BLUETOOTH_DIR}/btusb.c"
Find_String=""


function find_string_define {
	local string=$1
	local find=`echo ${string} | grep -c '0x[0-9a-fA-F]\{3,4\}'`
	if [ $find -lt 1 ]; then
		#In RTL8187_DIR, vendor is assigned to USB_VENDER_ID_REALTEK, in this case, the definition must be returned
		local string_define=`grep -nr ${string} ${2} | awk '/define/{print $3}'`
		find=`echo ${string_define} | grep -c '0x[0-9a-fA-F]\{3,4\}'`
		if [ $find -lt 1 ]; then
			Find_String=""
		else
			Find_String=${string_define}
		fi
	else
		Find_String=${string}
	fi
}

function create_driver_entry { #$1: driver-name
	local TABLE_SEARCH=""
	local driver=$1

	case $1 in
		rt2x00)
			TABLE_SEARCH=${RT2X00_DIR}
			;;
		rtl8192cu)
			TABLE_SEARCH=${RTL8192CU_DIR}
			;;
		rtl8187)
			TABLE_SEARCH=${RTL8187_DIR}
			;;
		rtl8188eu)
			# only linux-3.x builds rtl8188eu driver
			#if [ "${KLIB_BUILD}" = "/source/linux-3.x" ]; then
				TABLE_SEARCH=${RTL8188EU_DIR}
			#fi
			;;
		rtl8712)
			TABLE_SEARCH=${R8712U_DIR}
			;;
		rtl8812au)
			TABLE_SEARCH=${RTL8812AU_DIR}
			;;
		rtl8821au)
			TABLE_SEARCH=${RTL8821AU_DIR}
			;;
		carl9170)
			TABLE_SEARCH=${CARL9170_DIR}
			;;
		ath9k)
			TABLE_SEARCH=${ATH9K_DIR}
			;;
		zd1211rw)
			TABLE_SEARCH=${ZD1211RW_DIR}
			;;
		*)
			;;
	esac
	generate_entry_table ${TABLE_SEARCH} ${USB_WIFI_MAP} ${USB_ERROR_FILE}
}

function create_bt_driver_entry { #$1: driver-name
	local TABLE_SEARCH=""
	local driver=$1

	case $1 in
		ath3k)
			TABLE_SEARCH=${BT_ATH3K_DIR}
			;;
		bcm203x)
			TABLE_SEARCH=${BT_BCM203X_DIR}
			;;
		bfusb)
			TABLE_SEARCH=${BT_BFUSB_DIR}
			;;
		bpa10x)
			TABLE_SEARCH=${BT_BPA10X_DIR}
			;;
		btusb)
			TABLE_SEARCH=${BT_BTUSB_DIR}
			;;
		*)
			;;
	esac
	generate_entry_table ${TABLE_SEARCH} ${USB_BLUETOOTH_MAP} ${USB_BLUETOOTH_ERROR_FILE}
	generate_vendor_interface_table ${TABLE_SEARCH} ${USB_BLUETOOTH_MAP} ${USB_BLUETOOTH_ERROR_FILE}
}

function generate_vendor_interface_table {
	local record=""
	# ex. line is like { USB_VENDOR_AND_INTERFACE_INFO(0x05ac, 0xff, 0x01, 0x01) },

	for line in `grep -r ${VENDOR_INTERFACE_KEY} $1 | cut -d'(' -f2 | cut -d')' -f1 | sed 's/ //g'`; do
		local vendor=`echo ${line} | cut -d',' -f1`
		find_string_define ${vendor} $1
		if [ ${#Find_String} -eq 0 ]; then
			record=$3
		else
			vendor=${Find_String}
			record=$2
		fi

		echo "VENDOR(${vendor},${driver})" >> ${record}
	done
}

function generate_entry_table { #$1 table-name, $2 map-file-name, $3 error-file-name
	local record=""
	# ex. line is like { USB_DEVICE(0x07d1, 0x3c13) },

	for line in `grep -r ${KEY} $1 | cut -d'(' -f2 | cut -d')' -f1 | sed 's/ //g'`; do
		local vendor=`echo ${line} | cut -d',' -f1`
		local product=`echo ${line} | cut -d',' -f2`
		find_string_define ${vendor} $1
		if [ ${#Find_String} -eq 0 ]; then
			record=$3
		else
			vendor=${Find_String}
			find_string_define ${product}
			if [ ${#Find_String} -eq 0 ]; then
				record=$3
			else
				product=${Find_String}
				record=$2
			fi
		fi

		echo "(${vendor}:${product},${driver})" >> ${record}
	done
}

function create_usb_wireless_table { #$1: driver list
	local driver_list=$1
	if [ -f ${USB_WIFI_MAP} ]; then
		rm ${USB_WIFI_MAP}
	fi

	if [ -f ${USB_ERROR_FILE} ]; then
		rm ${USB_ERROR_FILE}
	fi

	if [ ${#driver_list} -eq 0 ]; then
		driver_list=${DRIVER_LIST}
	fi

	if [ "${driver_list}" = "all" ]; then
		driver_list=${DRIVER_LIST}
	fi

	for driver in ${driver_list}; do 
		create_driver_entry "${driver}"
	done
}

function create_usb_bluetooth_table { #$1: driver list
	local driver_list=$1
	if [ -f ${USB_BLUETOOTH_MAP} ]; then
		rm ${USB_BLUETOOTH_MAP}
	fi

	if [ -f ${USB_BLUETOOTH_ERROR_FILE} ]; then
		rm ${USB_BLUETOOTH_ERROR_FILE}
	fi

	if [ ${#driver_list} -eq 0 ]; then
		driver_list=${BLUETOOTH_DRIVER_LIST}
	fi

	if [ "${driver_list}" = "all" ]; then
		driver_list=${BLUETOOTH_DRIVER_LIST}
	fi

	for driver in ${driver_list}; do
		create_bt_driver_entry "${driver}"
	done
}

case $1 in
	create-table)
		shift;
		create_usb_wireless_table "$@"
		;;
	create-bt-table)
		shift;
		create_usb_bluetooth_table "$@"
		;;
	*)
		;;
esac

