#!/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("
DRIVER_DIR="/source/linux-4.4.x/drivers/net/wireless/"
BACKPORTS_DRIVER_DIR="/source/backports/v4.4.x/"
ATH9K_DIR="${DRIVER_DIR}/ath/ath9k/"
CARL9170_DIR="${DRIVER_DIR}/ath/carl9170/"
RT2X00_DIR="${DRIVER_DIR}/rt2x00/rt2800usb.c"
RTL8187_DIR="${DRIVER_DIR}/realtek/rtl818x/rtl8187/"
RTL8192CU_DIR="${DRIVER_DIR}/realtek/rtlwifi/rtl8192cu/"
RTL8712_DIR="/source/linux-4.4.x/drivers/staging/rtl8712/"
ZD1211RW_DIR="${DRIVER_DIR}/zd1211rw/"
RTL8812AU_DIR="${BACKPORTS_DRIVER_DIR}/rtl8812au"
DRIVER_LIST="ath9k carl9170 mt7601u rt2x00 rtl8187 rtl8192cu rtl8712 zd1211rw rtl8812au"

BLUETOOTH_DIR="/source/linux-4.4.x/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
		ath9k)
			TABLE_SEARCH=${ATH9K_DIR}
			;;
		carl9170)
			TABLE_SEARCH=${CARL9170_DIR}
			;;
		rt2x00)
			TABLE_SEARCH=${RT2X00_DIR}
			;;
		rtl8187)
			TABLE_SEARCH=${RTL8187_DIR}
			;;
		rtl8192cu)
			TABLE_SEARCH=${RTL8192CU_DIR}
			;;
		rtl8712)
			TABLE_SEARCH=${RTL8712_DIR}
			;;
		zd1211rw)
			TABLE_SEARCH=${ZD1211RW_DIR}
			;;
		rtl8812au)
			TABLE_SEARCH=${RTL8812AU_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

