#!/bin/bash
#========================================
# Common functions
#========================================
intf_resetAll()
{
	sed -i '/CONFIG_USB_HCI = /c\CONFIG_USB_HCI = n' Makefile
	sed -i '/CONFIG_PCI_HCI = /c\CONFIG_PCI_HCI = n' Makefile
	sed -i '/CONFIG_SDIO_HCI = /c\CONFIG_SDIO_HCI = n' Makefile
	sed -i '/CONFIG_GSPI_HCI = /c\CONFIG_GSPI_HCI = n' Makefile
}
intf_usb()
{
	intf_resetAll
	sed -i '/CONFIG_USB_HCI = /c\CONFIG_USB_HCI = y' Makefile
}
intf_pcie()
{
	intf_resetAll
	sed -i '/CONFIG_PCI_HCI = /c\CONFIG_PCI_HCI = y' Makefile
}
intf_sdio()
{
	intf_resetAll
	sed -i '/CONFIG_SDIO_HCI = /c\CONFIG_SDIO_HCI = y' Makefile
}
intf_gspi()
{
	intf_resetAll
	sed -i '/CONFIG_GSPI_HCI = /c\CONFIG_GSPI_HCI = y' Makefile
}

tx_power_config()
{
	if [ $1 = 0 ]; then
		sed -i '/CONFIG_TXPWR_BY_RATE_EN = / c\CONFIG_TXPWR_BY_RATE_EN = n' $MAKEFILE
	elif [ $1 = 1 ]; then
		sed -i '/CONFIG_TXPWR_BY_RATE_EN = / c\CONFIG_TXPWR_BY_RATE_EN = y' $MAKEFILE
	else
		sed -i '/CONFIG_TXPWR_BY_RATE_EN = / c\CONFIG_TXPWR_BY_RATE_EN = auto' $MAKEFILE
	fi

	if [ $2 = 0 ]; then
		sed -i '/CONFIG_TXPWR_LIMIT_EN = / c\CONFIG_TXPWR_LIMIT_EN = n' $MAKEFILE
	elif [ $2 = 1 ]; then
		sed -i '/CONFIG_TXPWR_LIMIT_EN = / c\CONFIG_TXPWR_LIMIT_EN = y' $MAKEFILE
	else
		sed -i '/CONFIG_TXPWR_LIMIT_EN = / c\CONFIG_TXPWR_LIMIT_EN = auto' $MAKEFILE
	fi
}

nic_resetAll()
{
	sed -i '/CONFIG_RTL8852A = /c\CONFIG_RTL8852A = n' Makefile
	sed -i '/CONFIG_RTL8852B = /c\CONFIG_RTL8852B = n' Makefile
	sed -i '/CONFIG_MP_INCLUDED = /c\CONFIG_MP_INCLUDED = y' Makefile
	sed -i '/CONFIG_POWER_SAVING = /c\CONFIG_POWER_SAVING = n' Makefile
	sed -i '/CONFIG_BTC = / c\CONFIG_BTC = n' Makefile
	sed -i '/USE_TRUE_PHY = / c\USE_TRUE_PHY = y' Makefile
	tx_power_config 2 2
}

nic_8852a(){
	nic_resetAll
	sed -i '/CONFIG_RTL8852A = /c\CONFIG_RTL8852A = y' Makefile
	#sed -i '/CONFIG_BTC = / c\CONFIG_BTC = n' Makefile
	sed -i '/CONFIG_TXPWR_BY_RATE = /c\CONFIG_TXPWR_BY_RATE = n' Makefile
	sed -i '/CONFIG_TXPWR_LIMIT = /c\CONFIG_TXPWR_LIMIT = n' Makefile
	sed -i '/CONFIG_MP_INCLUDED = /c\CONFIG_MP_INCLUDED = n' Makefile
	sed -i '/CONFIG_POWER_SAVING = /c\CONFIG_POWER_SAVING = n' Makefile
	sed -i '/USE_TRUE_PHY = / c\USE_TRUE_PHY = y' Makefile
}

ch_obj_8852ae()
{
	sed -i '/obj-$(CONFIG_RTL/c\obj-$(CONFIG_RTL8852AE) := $(MODULE_NAME).o' Makefile
	sed -i '/export CONFIG_RTL/c\export CONFIG_RTL8852AE = m' Makefile

	cp -f Kconfig_rtl8852a_pci_linux Kconfig
}

ch_obj_8852au()
{
	sed -i '/obj-$(CONFIG_RTL/c\obj-$(CONFIG_RTL8852AU) := $(MODULE_NAME).o' Makefile
	sed -i '/export CONFIG_RTL/c\export CONFIG_RTL8852AU = m' Makefile

	cp -f Kconfig_rtl8852a_usb_linux Kconfig
}

ch_obj_8852as()
{
	sed -i '/obj-$(CONFIG_RTL/c\obj-$(CONFIG_RTL8852AS) := $(MODULE_NAME).o' Makefile
	sed -i '/export CONFIG_RTL/c\export CONFIG_RTL8852AS = m' Makefile

	cp -f Kconfig_rtl8852a_sdio_linux Kconfig
}

nic_8852b(){
	nic_resetAll
	sed -i '/CONFIG_RTL8852B = /c\CONFIG_RTL8852B = y' Makefile
	#sed -i '/CONFIG_BTC = / c\CONFIG_BTC = n' Makefile
	sed -i '/CONFIG_TXPWR_BY_RATE = /c\CONFIG_TXPWR_BY_RATE = n' Makefile
	sed -i '/CONFIG_TXPWR_LIMIT = /c\CONFIG_TXPWR_LIMIT = n' Makefile
	sed -i '/CONFIG_MP_INCLUDED = /c\CONFIG_MP_INCLUDED = n' Makefile
	sed -i '/CONFIG_POWER_SAVING = /c\CONFIG_POWER_SAVING = n' Makefile
	sed -i '/USE_TRUE_PHY = /c\USE_TRUE_PHY = n' Makefile
}

ch_obj_8852be()
{
	sed -i '/obj-$(CONFIG_RTL/c\obj-$(CONFIG_RTL8852BE) := $(MODULE_NAME).o' Makefile
	sed -i '/export CONFIG_RTL/c\export CONFIG_RTL8852BE = m' Makefile

	cp -f Kconfig_rtl8852b_pci_linux Kconfig
}

ch_obj_8852bu()
{
	sed -i '/obj-$(CONFIG_RTL/c\obj-$(CONFIG_RTL8852BU) := $(MODULE_NAME).o' Makefile
	sed -i '/export CONFIG_RTL/c\export CONFIG_RTL8852BU = m' Makefile

	cp -f Kconfig_rtl8852b_usb_linux Kconfig
}

ch_obj_8852bs()
{
	sed -i '/obj-$(CONFIG_RTL/c\obj-$(CONFIG_RTL8852BS) := $(MODULE_NAME).o' Makefile
	sed -i '/export CONFIG_RTL/c\export CONFIG_RTL8852BS = m' Makefile

	cp -f Kconfig_rtl8852b_sdio_linux Kconfig
}

get_svn_revision()
{
	if [ -z "$svn_rev" ] && [ -f .svn/entries ]; then
		svn_rev=`sed -n '11 s/^\([0-9][0-9]*\)$/\1/p' .svn/entries`
	fi
	if [ -z "$svn_rev" ]; then
		svn_rev="xxxx"
	fi
}

#========================================
# Sub functions
#========================================

get_coex_version()
{
	local ICName			# Target IC name
	local CoexDate
	local CoexDateShare="00000000"	# 8 digital characters for Date YYYYMMDD
	local CoexVerShare="00"		# 2 hex characters for Version
	local CoexDateIDV="00000000"	# 8 digital characters for Date YYYYMMDD
	local CoexVerIDV="00"		# 2 hex characters for Version
	# Share Antenna
	local FileShare			# File Name
	local StrDateShare		# String for grep Coex Date
	local StrVerShare		# String for grep Coex Version
	# Individual Antenna
	local FileIDV			# File Name
	local StrDateIDV		# String for grep Coex Date
	local StrVerIDV			# String for grep Coex Version

	if [ $1 ]; then
		ICName=$1
	else
		if [ $CHIP ]; then
			ICName=$CHIP
		else
			if [ $card ]; then
				ICName=$card
			else
				echo "Unknown target for generate COEX version!!"
				return
			fi
		fi
	fi

	case "$ICName" in
	[Rr][Tt][Ll]8822[Bb]*)
		# Share Antenna
		FileShare="./hal/btc/halbtc8822b1ant.c"
		StrDateShare="u32.*glcoex_ver_date_8822b_1ant"
		StrVerShare="u32.*glcoex_ver_8822b_1ant"
		# Individual Antenna
		FileIDV="./hal/btc/halbtc8822b2ant.c"
		StrDateIDV="u32.*glcoex_ver_date_8822b_2ant"
		StrVerIDV="u32.*glcoex_ver_8822b_2ant"
		;;
	[Rr][Tt][Ll]8821[Cc]*)
		# Share Antenna
		FileShare="./hal/btc/halbtc8821c1ant.c"
		StrDateShare="u32.*glcoex_ver_date_8821c_1ant"
		StrVerShare="u32.*glcoex_ver_8821c_1ant"
		# Individual Antenna
		FileIDV="./hal/btc/halbtc8821c2ant.c"
		StrDateIDV="u32.*glcoex_ver_date_8821c_2ant"
		StrVerIDV="u32.*glcoex_ver_8821c_2ant"
		;;
	*)
		echo "Can't find coex information for $ICName !!"
		return
		;;
	esac

	# Parse Share Antenna Information
	if [ -f "$FileShare" ]; then
		CoexDateShare=`grep $StrDateShare $FileShare |  sed 's/ //g' | awk -F '=' '{printf $2}' | awk -F ';' '{printf $1}'`
		CoexVerShare=`grep $StrVerShare $FileShare | sed 's/ //g' | sed 's/0x//g' | awk -F '=' '{printf $2}' | awk -F ';' '{printf $1}'`
	fi
	# Parse Individual Antenna Information
	if [ -f "$FileIDV" ]; then
		CoexDateIDV=`grep $StrDateIDV $FileIDV |  sed 's/ //g' | awk -F '=' '{printf $2}' | awk -F ';' '{printf $1}'`
		CoexVerIDV=`grep $StrVerIDV $FileIDV | sed 's/ //g' | sed 's/0x//g' | awk -F '=' '{printf $2}' | awk -F ';' '{printf $1}'`
	fi

	if [ $CoexDateShare -lt $CoexDateIDV ]; then
		CoexDate=$CoexDateIDV
	else
		CoexDate=$CoexDateShare
	fi
	coex_version="COEX"$CoexDate-$CoexVerShare$CoexVerIDV
}

get_coex_version_common()
{
	local ICName			# Target IC name
	local CoexDate
	local CoexDateCmn="00000000"	# 8 digital characters for Date YYYYMMDD
	local CoexVerCmn="00"		# 2 hex characters for Version
	local FileCmn			# File Name
	local StrDateCmn		# String for grep Coex Date
	local StrVerCmn		# String for grep Coex Version
	local CoexDatePara="00000000"
	local CoexVerPara="00"
	local FilePara
	local StrDatePara
	local StrVerPara

	if [ $1 ]; then
		ICName=$1
	else
		if [ $CHIP ]; then
			ICName=$CHIP
		else
			if [ $card ]; then
				ICName=$card
			else
				echo "Unknown target for generate COEX version!!"
				return
			fi
		fi
	fi

	FileCmn="./hal/btc/halbtccommon.c"
	StrDateCmn="u32\scoex_ver_date"
	StrVerCmn="u32\scoex_ver\s="
	CoexDateCmn=`grep $StrDateCmn $FileCmn |  sed 's/ //g' | awk -F '=' '{printf $2}' | awk -F ';' '{printf $1}'`
	CoexVerCmn=`grep $StrVerCmn $FileCmn | sed 's/ //g' | awk -F '=' '{printf $2}' | awk -F ';' '{printf "%02x",$1}'`

	case "$ICName" in
	[Rr][Tt][Ll]8822[Cc]*)
		FilePara="./hal/btc/halbtc8822c.c"
		;;
	*)
		echo "Can't find coex information for $ICName !!"
		return
		;;
	esac

	StrDatePara="para_ver_date"
	StrVerPara="para_ver\s"
	CoexDatePara=`grep $StrDatePara $FilePara | awk -F ',' '{printf "%d",$1}'`
	CoexVerPara=`grep $StrVerPara $FilePara | awk -F ',' '{printf "%02x",$1}'`

	if [ $CoexDateCmn -lt $CoexDatePara ]; then
		CoexDate=$CoexDatePara
	else
		CoexDate=$CoexDateCmn
	fi
	coex_version="COEX"$CoexDate-$CoexVerCmn$CoexVerPara
}

add_version_file()
{
	defversion="#define DRIVERVERSION\t\"$postfix\""
	echo -e $defversion > include/rtw_version.h

	if [ "$coex_version" ]; then
		str_version="#define BTCOEXVERSION\t\"$coex_version\""
		echo -e $str_version >> include/rtw_version.h
	fi
}

LogFile=make_log
function LOG()
{
	echo -e $1 | tee -a -i $LogFile;
}

KO_FOLDER=rtk_wifi_ko
config_make_ko_folder()
{
	rm -rf $KO_FOLDER
	mkdir -p $KO_FOLDER
	chmod -R 777 $KO_FOLDER
}
config_cp_ko()
{
	cp *.ko $KO_FOLDER/.
}
conf_driver()
{
	sed -i '/CONFIG_AUTOCFG_CP = /c\CONFIG_AUTOCFG_CP = y' Makefile
	if [ -f include/rtw_version.h ]; then
		echo "rtw_version.h has existed!"
	else
		add_version_file
	fi
}
config_make()
{
	if [ $GEN_GIT_INFO = 1 ]; then
		sed -i '/CONFIG_GEN_GIT_INFO / c\#define CONFIG_GEN_GIT_INFO 1' ./phl/phl_config.h
		sed -i '/gen_git_info.sh / c\	./phl/gen_git_info.sh $(TopDIR)' ./Makefile
	else
		sed -i '/CONFIG_GEN_GIT_INFO / c\#define CONFIG_GEN_GIT_INFO 0' ./phl/phl_config.h
		sed -i '/gen_git_info.sh / c\\	#./phl/gen_git_info.sh $(TopDIR)' ./Makefile
	fi
	make clean;
	# 1:stdout,2:stderr
	# Redirecting the stderr to file
	#make -j2 2> $LogFile;
	# Redirecting the stderr and stdout to file
	#make -j2 > $LogFile 2>&1;
	#make -j2 &>> $LogFile

	if [ $SMATCH_CHECK = 1 ]; then
		#echo "compiling.... & smatch checking...."
		#make CHECK="smatch $SMATCH_OPT" CC="cgcc" -j2 2>&1 | grep "warn:\|error:" | tee -a -i $LogFile;
		#make CHECK="smatch $SMATCH_OPT" CC="cgcc" -j2 2>&1 | grep "warn:\|error:"
		make CHECK="smatch $SMATCH_OPT" CC="cgcc" -j2 2>&1 | tee -a -i $LogFile;
		cat $LogFile | grep "warn:\|error:" | cut -c$((${#PWD}+2))- > smatch_log
	else
		make -j2 2>&1 | tee -a -i $LogFile;
	fi

	config_cp_ko;
}


make_8852ae()
{
	#get_coex_version_common rtl8852a
	conf_driver;
	nic_8852a;
	intf_pcie;
	ch_obj_8852ae;
	config_make;
	LOG ".......... compile 8852ae done ..........";
	LOG "==================================================";
}

make_8852au()
{
	#get_coex_version_common rtl8852a
	conf_driver;
	nic_8852a;
	intf_usb;
	ch_obj_8852au;
	config_make;
	LOG ".......... compile 8852au done ..........";
	LOG "==================================================";
}

make_8852as()
{
	#get_coex_version_common rtl8852a
	conf_driver;
	nic_8852a;
	intf_sdio;
	ch_obj_8852as;
	config_make;
	LOG ".......... compile 8852as done ..........";
	LOG "==================================================";
}

make_8852be()
{
	#get_coex_version_common rtl8852a
	conf_driver;
	nic_8852b;
	intf_pcie;
	ch_obj_8852be;
	config_make;
	LOG ".......... compile 8852be done ..........";
	LOG "==================================================";
}

make_8852bu()
{
	#get_coex_version_common rtl8852a
	conf_driver;
	nic_8852b;
	intf_usb;
	ch_obj_8852bu;
	config_make;
	LOG ".......... compile 8852bu done ..........";
	LOG "==================================================";
}

make_8852bs()
{
	#get_coex_version_common rtl8852a
	conf_driver;
	nic_8852b;
	intf_sdio;
	ch_obj_8852bs;
	config_make;
	LOG ".......... compile 8852bs done ..........";
	LOG "==================================================";
}

make_all()
{
	make_8852ae;
	make_8852au;
	make_8852as;
#	make_8852be;
#	make_8852bu;
#	make_8852bs;
}

make_sdio()
{
	make_8852as;
	make_8852bs;
}

make_pcie()
{
	make_8852ae;
	make_8852be;
}
make_usb()
{
	make_8852au;
	make_8852bu;
}
#========================================
# Main Script Start From Here
#========================================
# set variables
MAKEFILE="Makefile"

if [ ! -f include/rtw_version.h ]; then
	# Make Version string
	if [ -z "$2" ]; then
		get_svn_revision
		version="v0.0.0_"$svn_rev
		datestr=$(date +%Y%m%d)
		#please remove the "_beta" at formal release time
		postfix=$version.$datestr"_beta"
	else
		postfix=$2
	fi
fi

# Chip options
declare -a chipsOption
chipsOption=()
chipsOption+=(RTL8852ae RTL8852au RTL8852as)
chipsOption+=(RTL8852be RTL8852bu RTL8852bs)
chipsOption+=(USB SDIO PCIE)
chipsOption+=(ALL)

if [ $# -eq 1 ]; then
	card=$1
	echo "You have selected $card"
else
	# Select NIC type
	echo "Please select card type(1-${#chipsOption[*]}):"
	select card in "${chipsOption[@]}";
	do
		echo "You have selected $card"
		break
	done
fi

# syntax checker - Smatch / checkpatch.pl
declare -a syntaxChecker
syntaxChecker=()
syntaxChecker+=(None Smatch)


SMATCH_CHECK=0
# Make Other selection
if [ "$card" != "ALL" ];then
if [ "$card" != "USB" ];then
if [ "$card" != "SDIO" ];then
if [ "$card" != "PCIE" ];then
if [ $# -eq 1 ]; then
	checker="None"
	echo "You have selected syntax checker $checker"
	GEN_GIT_INFO=0
	echo "exclude phl_git_info.h"
else
        # Select checker
	echo "=================================="
        echo "Please select syntax checker:(1-${#syntaxChecker[*]}):"
        select checker in "${syntaxChecker[@]}";
        do
        	echo "You have selected $checker"

          if [ "$checker" = "Smatch" ]; then
          	SMATCH_CHECK=1
          fi
          break
        done
fi
fi
fi
fi
fi

if [ -z "$GEN_GIT_INFO" ];then
	echo "=================================="
	echo "Do you want to include phl_git_info.h?"
	select _option in "yes" "no";
	do
		if [ "$_option" = "yes" ]; then
			GEN_GIT_INFO=1
		else
			GEN_GIT_INFO=0
		fi
		break
	done
fi


rm $LogFile
config_make_ko_folder;
# Make
case "$card" in
	[Rr][Tt][Ll]8852[Aa][Ee])
	make_8852ae;;
	[Rr][Tt][Ll]8852[Aa][Uu])
	make_8852au;;
	[Rr][Tt][Ll]8852[Aa][Ss])
	make_8852as;;
	[Rr][Tt][Ll]8852[Bb][Ee])
	make_8852be;;
	[Rr][Tt][Ll]8852[Bb][Uu])
	make_8852bu;;
	[Rr][Tt][Ll]8852[Bb][Ss])
	make_8852bs;;
	[Ss][Dd][Ii][Oo])
	make_sdio;;
	[Pp][Cc][Ii][Ee])
	make_pcie;;
	[Uu][Ss][Bb])
	make_usb;;
	[Aa][Ll][Ll])
	make_all;;
	*)
	echo "Unknown NIC type"
	;;
esac
