#--------------------------------------------------------------------
#
#      Build script for MT76xx series Wi-Fi driver on MTK DTV platform 
#
#--------------------------------------------------------------------

#additional script to source-in

#default values of variables
WIFI_CHIP=mt7601
BUILD_OPT_NUM=-1
#WLAN_DRV_KO_LIST="UTIL/rtutil5572sta MODULE/rt5572sta NETIF/rtnet5572sta"
WLAN_DRV_KO_LIST="os/linux/mt7601Usta"

#note! the entries between every xxx_list[] array must match
#build_opt_list=(mt5396_dbg mt5396_rel mt5369_dbg mt5369_rel mt5398_dbg mt5398_rel mt5860_dbg mt5860_rel mt5880_dbg mt5880_rel)
chip_list=(mt5396 mt5396 mt5369 mt5369 mt5398 mt5398 mt5860 mt5860 mt5880 mt5880)
obj_type_list=(dbg rel dbg rel dbg rel dbg rel dbg rel)
kern_ver_list=(2.6.35 2.6.35 2.6.35 2.6.35 3.0 3.0 3.0 3.0 3.0 3.0)
toolchain_ver_list=(4.5.1 4.5.1 4.5.1 4.5.1 4.5.1 4.5.1 4.5.1 4.5.1 4.5.1 4.5.1)
vfp_ca9_list=(ca9 ca9 ca9 ca9 ca9 ca9 ca9 ca9 ca9 ca9)
smp_list=(smp smp nonsmp nonsmp smp smp smp smp nonsmp nonsmp)

#function definitions
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function show_list(){
    echo "Build option list------>"
    for ((i=0; i<${#obj_type_list[@]}; i++)); do
        echo "$i: ${chip_list[$i]}_${obj_type_list[$i]}"
    done
}

BUILD_MAX_IDX=${#chip_list[@]}-1

function show_prompt(){
    read -p "Please enter your build option...[0~$((${#chip_list[@]}-1))], type '?' to show option list:" BUILD_OPT_NUM
    if [ ${BUILD_OPT_NUM} == ? ]; then
        show_list;
        BUILD_OPT_NUM=-1
    elif [ ${BUILD_OPT_NUM} -gt ${#obj_type_list[@]} ]; then
        echo "Index out of range, please enter again"
        BUILD_OPT_NUM=-1
    fi
}

function sanity_check(){
    if [ ${#chip_list[@]} -ne ${#obj_type_list[@]} ]; then
       echo "The entry number in chip_list[${#chip_list[@]}] does not match the number in obj_type_list[${#obj_type_list[@]}]! Please check first!"
       exit -1;
    fi
    if [ ${#kern_ver_list[@]} -ne ${#obj_type_list[@]} ]; then
       echo "The entry number in kern_ver_list[${#kern_ver_list[@]}] does not match the number in obj_type_list[${#obj_type_list[@]}]! Please check first!"
       exit -1;
    fi
    if [ ${#toolchain_ver_list[@]} -ne ${#obj_type_list[@]} ]; then
       echo "The entry number in toolchain_ver_list[${#toolchain_ver_list[@]}] does not match the number in obj_type_list[${#obj_type_list[@]}]! Please check first!"
       exit -1;
    fi
    if [ ${#smp_list[@]} -ne ${#obj_type_list[@]} ]; then
       echo "The entry number in smp_list[${#smp_list[@]}] does not match the number in obj_type_list[${#obj_type_list[@]}]! Please check first!"
       exit -1;
    fi
    if [ ${#vfp_ca9_list[@]} -ne ${#obj_type_list[@]} ]; then
       echo "The entry number in vfp_ca9_list[${#vfp_ca9_list[@]}] does not match the number in obj_type_list[${#obj_type_list[@]}]! Please check first!"
       exit -1;
    fi
}

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# *** Script execution begins here ***
sanity_check;

echo "Welcome to build script of MT76xx driver..."

while [ "${BUILD_OPT_NUM}" -lt 0 ]
do
    show_prompt;
done
  
echo -e "Build option=$BUILD_OPT_NUM, starting to build..."

# * build parameters *
CHIP=${chip_list[$BUILD_OPT_NUM]}
MODEL=atsc
DBG=${obj_type_list[$BUILD_OPT_NUM]}
TARGET_KERNEL_VERSION=${kern_ver_list[$BUILD_OPT_NUM]}
TOOL_CHAIN_VERSION=${toolchain_ver_list[$BUILD_OPT_NUM]}
if [ "${smp_list[${BUILD_OPT_NUM}]}" == "smp" ]; then
	SMP=_smp
fi
TARGET_CHIP=${CHIP}

if [ "${CHIP}" == "mt5860" ];then
	TARGET_CHIP=mt5880
fi
if [ "${DBG}" == "dbg" ]; then
    DEFCONFIG=${TARGET_CHIP}${SMP}_mod_dbg_defconfig
    OUTPUT_DBG_REL=debug
else
    DEFCONFIG=${TARGET_CHIP}${SMP}_mod_defconfig
    OUTPUT_DBG_REL=rel
fi

# * path settings *
#need clever way?
CUR_DIR=`pwd`

WLAN_SOURCE_DIR=`echo $CUR_DIR | awk '{ print substr( $0, 0, length($0) - 12 ) }'`
#VM_LINUX_ROOT := $(word 1, $(subst /vm_linux/,/vm_linux /, vmlinxsarick))
#THIRDPARTY_ROOT ?= $(word 1, $(subst /third_party/,/third_party /, $(shell pwd)))

VM_LINUX_ROOT=${WLAN_SOURCE_DIR}/../../../../..
THIRDPARTY_ROOT=${VM_LINUX_ROOT}/third_party
SYSBUILD_DIR=${CHIP}_${MODEL}_linux
MODULE_OBJ_DIR=${VM_LINUX_ROOT}/output/mtk_linux/${SYSBUILD_DIR}/${OUTPUT_DBG_REL}/obj/kernel/linux_core/kernel/linux-${TARGET_KERNEL_VERSION}/${DEFCONFIG}_modules
OBJ_ROOT=${VM_LINUX_ROOT}/output/mtk_linux/${CHIP}_${MODEL}_linux/${OUTPUT_DBG_REL}/obj/kernel
KERNEL_OBJ_ROOT=${OBJ_ROOT}/linux_core/kernel
KO_OUTPUT_DIR=${VM_LINUX_ROOT}/output/mtk_linux/${SYSBUILD_DIR}/${OUTPUT_DBG_REL}/obj/kernel/linux_core/kernel/linux-${TARGET_KERNEL_VERSION}/drivers/net/wireless/${WIFI_CHIP}
TARGET_KERNEL_DIR=${VM_LINUX_ROOT}/linux_core/kernel/linux-${TARGET_KERNEL_VERSION}
WIFI_BUILD_DIR=${VM_LINUX_ROOT}/linux_core/kernel/linux-${TARGET_KERNEL_VERSION}/drivers/net/wireless/${WIFI_CHIP}

export MODULE_OBJ_DIR
export OBJ_ROOT
export KERNEL_OBJ_ROOT
export DEFCONFIG
export TOOL_CHAIN_VERSION

#wifi chip specific
RT28xx_DIR=${WIFI_BUILD_DIR}
LINUX_SRC=${TARGET_KERNEL_DIR}
MTK_BUILD=0
export RT28xx_DIR
export LINUX_SRC
export MTK_BUILD

#checking host kernel path
if [ ! -e ${MODULE_OBJ_DIR} ]; then
    echo "XXXXX ERROR!! XXXXX"
    echo "Kernel OBJ root dir does not exist!!!"
    echo "Please check path (${MODULE_OBJ_DIR})"
    echo "Please set to the correct path. This may affect the driver obj and cause unknown incompatible issue."
    exit -1
elif [ ! -e ${MODULE_OBJ_DIR}/.config ]; then
    echo "XXXXX ERROR!! XXXXX"
    echo "Kernel .config file does not exist!!! (${MODULE_OBJ_DIR}/.config)"
    echo "Please set the correct path. This may affect the driver obj and cause unknown incompatible issue."
    exit -1
fi
#checking toolchain
if [ "${vfp_ca9_list[$BUILD_OPT_NUM]}" == "ca9" ]; then
    CA9=true
    TOOL_CHAIN_SUFFIX=_vfp_ca9
	TOOL_CHAIN_35_DIR_NAME=vfp_4.5.1_2.6.27_cortex-a9-rhel4
	TOOL_CHAIN_PREFIX=armv7a-mediatek451_001_vfp-linux-gnueabi-
	WLAN_KO_DST_PATH_SUFFIX=_CA9
elif [ "${VFP_CA9}" == "vfp" ]; then
    VFP=true
    TOOL_CHAIN_SUFFIX=_vfp
	TOOL_CHAIN_35_DIR_NAME=vfp_4.5.1_2.6.27_arm11-rhel4
    TOOL_CHAIN_PREFIX=armv6z-mediatek451_001_vfp-linux-gnueabi-
else
    TOOL_CHAIN_SUFFIX=
    TOOL_CHAIN_35_DIR_NAME=soft_4.5.1_2.6.27_arm11-rhel4
    TOOL_CHAIN_PREFIX=armv6z-mediatek451_001_soft-linux-gnueabi-
fi

if [ "${CHIP}" == "mt5396" ];then
	WLAN_KO_DST_PATH_SUFFIX=${WLAN_KO_DST_PATH_SUFFIX}_SMP
elif [ "${CHIP}" == "mt5398" ];then
	WLAN_KO_DST_PATH_SUFFIX=${WLAN_KO_DST_PATH_SUFFIX}_SMP
elif [ "${CHIP}" == "mt5860" ];then
	WLAN_KO_DST_PATH_SUFFIX=${WLAN_KO_DST_PATH_SUFFIX}_SMP
else
	WLAN_KO_DST_PATH_SUFFIX=${WLAN_KO_DST_PATH_SUFFIX}_NONSMP
fi

if [ "${DBG}" == "dbg" ];then
	WLAN_KO_DST_PATH_SUFFIX=${WLAN_KO_DST_PATH_SUFFIX}_DBG
else
	WLAN_KO_DST_PATH_SUFFIX=${WLAN_KO_DST_PATH_SUFFIX}_REL
fi

if [ "${TOOL_CHAIN_VERSION}" == "4.4.2" ]; then
	TARGET_CROSS_COMPILE=/usr/local/mtk-toolchain/gnuarm-${TOOL_CHAIN_VERSION}${TOOL_CHAIN_SUFFIX}/cross-tools/arm11/bin/armv6z-mediatek442_001${TOOL_CHAIN_SUFFIX}-linux-gnueabi-
elif [ "${TOOL_CHAIN_VERSION}" == "4.5.1" ]; then
	TARGET_CROSS_COMPILE=/mtkoss/gnuarm/${TOOL_CHAIN_35_DIR_NAME}/i686/bin/${TOOL_CHAIN_PREFIX}	
else
	echo "TOOL_CHAIN_VERSION:${TOOL_CHAIN_VERSION} Error!!!"
	exit 1
fi

WLAN_KO_DST_PATH_DIR=${VM_LINUX_ROOT}/third_party/library/gnuarm-${TOOL_CHAIN_VERSION}${TOOL_CHAIN_SUFFIX}/wlan/linux-${TARGET_KERNEL_VERSION}${WLAN_KO_DST_PATH_SUFFIX}/mtk/${WIFI_CHIP}

CROSS_COMPILE=${TARGET_CROSS_COMPILE}
CC=${CROSS_COMPILE}gcc

export CROSS_COMPILE
export CC
export TARGET_CROSS_COMPILE
export TARGET_KERNEL_VERSION
export TARGET_KERNEL_DIR

#set kernel cross compile
if [ ! -e ${KERNEL_OBJ_ROOT} ]; then
	mkdir -p ${KERNEL_OBJ_ROOT}
fi
if [ -e ${KERNEL_OBJ_ROOT}/.mtk_cross_compile ]; then
	rm -vf ${KERNEL_OBJ_ROOT}/.mtk_cross_compile
fi
echo "${TARGET_CROSS_COMPILE}" > ${KERNEL_OBJ_ROOT}/.mtk_cross_compile
echo "arm" > ${KERNEL_OBJ_ROOT}/.mtk_target_cpu

#modify config.mk to set "HAS_HE_BD_SUPPORT=n"
if [ "`grep HAS_HE_BD_SUPPORT=y ${WLAN_SOURCE_DIR}/config.mk`" != "" ]; then
    echo "Modifying config.mk as [HAS_HE_BD_SUPPORT=n]..."
    sed -i 's/^.*'"HAS_HE_BD_SUPPORT=y"'.*$/'"HAS_HE_BD_SUPPORT"'\=n/g' ${WLAN_SOURCE_DIR}/config.mk
fi

if [ "`grep HAS_HE_BD_SUPPORT=y ${WLAN_SOURCE_DIR}/os/linux/config.mk`" != "" ]; then
    echo "Modifying config.mk as [HAS_HE_BD_SUPPORT=n]..."
    sed -i 's/^.*'"HAS_HE_BD_SUPPORT=y"'.*$/'"HAS_HE_BD_SUPPORT"'\=n/g' ${WLAN_SOURCE_DIR}/os/linux/config.mk
fi


echo "Building ${WIFI_CHIP} driver..."
echo "---------------------"
echo "CHIP=${CHIP}"
echo "MODEL=${MODEL}"
echo "DBG=${DBG}"
echo "WLAN_SOURCE_DIR=${WLAN_SOURCE_DIR}"
echo "TARGET_KERNEL_VERSION=${TARGET_KERNEL_VERSION}"
echo "TOOL_CHAIN_VERSION=${TOOL_CHAIN_VERSION}"
echo "SMP=${SMP}"
echo "Kernel defconfig=${DEFCONFIG}"
echo "Sysbuild=${SYSBUILD_DIR}"
echo "KERNEL_OBJ_ROOT=${MODULE_OBJ_DIR}"
echo "WLAN_KO_DST_PATH_DIR=${WLAN_KO_DST_PATH_DIR}"
echo "---------------------"

if [ $TARGET_KERNEL_VERSION == 2.6.35 ]; then
	cp -f ${VM_LINUX_ROOT}/linux_core/kernel/linux-${TARGET_KERNEL_VERSION}/drivers/Kconfig ${VM_LINUX_ROOT}/linux_core/kernel/linux-${TARGET_KERNEL_VERSION}/drivers/Kconfig.bak	
	sed -i '/mtk/'d ${VM_LINUX_ROOT}/linux_core/kernel/linux-${TARGET_KERNEL_VERSION}/drivers/Kconfig
fi

if [ ! -e ${WLAN_KO_DST_PATH_DIR} ]; then
        mkdir -p ${WLAN_KO_DST_PATH_DIR}
fi

echo "----------------Generate .config-----------------"
if [ ! -e ${MODULE_OBJ_DIR} ]; then
        mkdir -p ${MODULE_OBJ_DIR}
fi

if [ ! -e ${WLAN_KO_DST_PATH_DIR} ]; then
    mkdir -p ${WLAN_KO_DST_PATH_DIR}
fi
#make -f  gen_kconfig.mak


#Clean up obj before compling
echo "Cleaning obj ..."
rm -rf `find ${WLAN_SOURCE_DIR} -name "*.o"`
rm -rf `find ${WLAN_SOURCE_DIR} -name "*.ko"`
rm -rf `find ${WLAN_SOURCE_DIR} -name ".*.cmd"`
rm -rf `find ${WLAN_SOURCE_DIR} -name "*.mod.c"`
rm -rf `find ${WLAN_SOURCE_DIR} -name "modules.order"`
rm -rf `find ${WLAN_SOURCE_DIR} -name "*.o"`
rm -rf `find ${WLAN_SOURCE_DIR} -name "*.ko"`
rm -rf `find ${WLAN_SOURCE_DIR} -name ".*.cmd"`
rm -rf `find ${WLAN_SOURCE_DIR} -name "*.mod.c"`
rm -rf `find ${WLAN_SOURCE_DIR} -name "modules.order"`

if [ -e ${WIFI_BUILD_DIR} ]; then
    echo "Removing original folder:${WIFI_BUILD_DIR}..."
    rm -rf ${WIFI_BUILD_DIR}
fi    

echo "Copying driver source to: ${WIFI_BUILD_DIR} "
cp -rf ${WLAN_SOURCE_DIR} ${WIFI_BUILD_DIR}
cp -f ${WIFI_BUILD_DIR}/scripts/dtv/Makefile ${WIFI_BUILD_DIR}/
cp -f ${WIFI_BUILD_DIR}/scripts/dtv/Makefile.6 ${WIFI_BUILD_DIR}/os/linux/

echo "Start building driver ${WIFI_CHIP}, entering [${WIFI_BUILD_DIR}]..."
cd ${WIFI_BUILD_DIR};
make V=1;

echo "---> Finished building ${WIFI_CHIP}..."


if [ $TARGET_KERNEL_VERSION == 2.6.35 ]; then
	cp -f ${VM_LINUX_ROOT}/linux_core/kernel/linux-${TARGET_KERNEL_VERSION}/drivers/Kconfig.bak ${VM_LINUX_ROOT}/linux_core/kernel/linux-${TARGET_KERNEL_VERSION}/drivers/Kconfig
fi

#
#   Copy the ko to pre-build library path
#

echo "Updating ko files..."
for thisko in ${WLAN_DRV_KO_LIST};
do
    if [ -f ${KO_OUTPUT_DIR}/${thisko}.ko ];then
        cp -f ${KO_OUTPUT_DIR}/${thisko}.ko ${WLAN_KO_DST_PATH_DIR}
    else
        echo XXXXX Error:  ${KO_OUTPUT_DIR}/${thisko}.ko was not found! XXXXX
        exit 1        
    fi
done
${TARGET_CROSS_COMPILE}strip --strip-unneeded ${WLAN_KO_DST_PATH_DIR}/*.ko
echo "${WLAN_KO_DST_PATH}/${WLAN_KO_DST_PATH_DIR_NAME}/ ko files are updated ..."

echo "${WLAN_KO_DST_PATH_DIR} ko files are updated ..."
echo "---------------------"
echo "Build Success!"
echo "---------------------"
exit 0











