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

WIFI_CHIP_NAME=mt7601
BUILD_TYPE=rel
#BUILD_TYPE=debug
LINUX_KERNEL_VERSION=3.0
WIFI_KO_NAME=${WIFI_CHIP_NAME}Usta.ko
CUR_DIR=`pwd`

WLAN_SOURCE_DIR=`echo $CUR_DIR | awk '{ print substr( $0, 0, length($0) - 12 ) }'`
VM_LINUX_ROOT=${WLAN_SOURCE_DIR}/../../../../..
ANDROID_PATH=${VM_LINUX_ROOT}/android/ics-4.x
WIFI_BUILD_DIR=${VM_LINUX_ROOT}/linux_core/kernel/linux-${LINUX_KERNEL_VERSION}/drivers/net/wireless/${WIFI_CHIP_NAME}
KERNEL_WIRELESS_PATH=${VM_LINUX_ROOT}/linux_core/kernel/linux-${LINUX_KERNEL_VERSION}/drivers/net/wireless
KERNEL_PATH=${VM_LINUX_ROOT}/linux_core/kernel/linux-${LINUX_KERNEL_VERSION}
SYS_BUILD_PATH=${VM_LINUX_ROOT}/sys_build/mtk_android/mt5860_cn_android
WLAN_BUILD_DESTINATION=${ANDROID_PATH}/device/mtk/mt5396/prebuilt/wifi
WLAN_MODULE_OUTPUT=${VM_LINUX_ROOT}/output/mtk_android/mt5860_cn_android/rel/obj/kernel/linux_core/kernel/linux-${LINUX_KERNEL_VERSION}/mt5880_android_smp_mod_defconfig_modules/drivers/net/wireless/${WIFI_CHIP_NAME}
TOOLCHAIN=/mtkoss/gnuarm/vfp_4.5.1_2.6.27_cortex-a9-rhel4/i686/bin/armv7a-mediatek451_001_vfp-linux-gnueabi-
#make -L -C $RA_KERNEL COMPANY=mtk MODEL=Python_m4v1_cn ODB=true O=$RA_KERNEL/mt5880_android_smp_mod_defconfig_modules modules

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/Kconfig.mt76xx ${WIFI_BUILD_DIR}/Kconfig

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

cd ${KERNEL_WIRELESS_PATH}
#backup original Makefile
cp -f ./Makefile ./Makefile.bak
echo "obj-m += ${WIFI_CHIP_NAME}/" >> ./Makefile
cp -f ${WIFI_BUILD_DIR}/scripts/dtv/Makefile._ics ${WIFI_BUILD_DIR}/Makefile
cp -f ${WIFI_BUILD_DIR}/scripts/dtv/Makefile.6._ics ${WIFI_BUILD_DIR}/os/linux/Makefile.6

echo "clean up objs in ${KERNEL_WIRELESS_PATH}"
rm -rf ${WLAN_MODULE_OUTPUT}

cd ${SYS_BUILD_PATH}
source init.sh
make kernel

if [ -f ${WLAN_MODULE_OUTPUT}/${WIFI_KO_NAME} ];then
    cp -f ${WLAN_MODULE_OUTPUT}/${WIFI_KO_NAME} ${WLAN_BUILD_DESTINATION}
    ${TOOLCHAIN}strip --strip-unneeded ${WLAN_BUILD_DESTINATION}/${WIFI_KO_NAME}
else
    echo XXXXX Error:  ${WLAN_MODULE_OUTPUT}/${WIFI_KO_NAME} was not found! XXXXX
    rm ${KERNEL_WIRELESS_PATH}/Makefile
    mv ${KERNEL_WIRELESS_PATH}/Makefile.bak ${KERNEL_WIRELESS_PATH}/Makefile
    exit 1        
fi

rm ${KERNEL_WIRELESS_PATH}/Makefile
mv ${KERNEL_WIRELESS_PATH}/Makefile.bak ${KERNEL_WIRELESS_PATH}/Makefile

echo "Build Complete!!"

