# --------------------------------------------------------------------
#   script file for building with Ralink combo driver
#
# --------------------------------------------------------------------

source ./wlan_setenv.sh

#############################
# Common variable sets      #
#############################
WLAN_DRV_SOURCE_NAME=ralink
WLAN_DRV_DST_LINK_NAME=ralink
WLAN_DRV_KCONFIG_NAME=CONFIG_RT537X
SCRIPT_PATH=`pwd`
WLAN_KO_DST_PATH_DIR_NAME=ralink
#WLAN_DRV_KO_LIST="UTIL/rtutil5572sta MODULE/rt5572sta NETIF/rtnet5572sta"
WLAN_DRV_KO_LIST="rt5572sta"
WLAN_SYSROOT_PATH=lib/modules/$WLAN_KERNELRELEASE/BDP

#############################
# Specific variable sets    #
#############################
WLAN_DRV_ADDITIONAL_CONFIG=ralink
WLAN_ADDITIONAL_CMDS=``

if [ "${PRJ_TYPE}" == "BDP" ]; then
    echo "-----------------------------------------------"
    echo "       BDP platforms"
    echo "-----------------------------------------------"
    
    #prompt to ask if config.sh is desired
    NEED_CONFIG=1
    read -p "Do you want to run config.sh? (Y/n) " PROMPT_CONFIG
    
    if [ "$PROMPT_CONFIG" == "n" ] || [ "$PROMPT_CONFIG" == "N" ]; then
        NEED_CONFIG=0
    fi

    if [ $NEED_CONFIG == 1 ]; then
        echo "config.sh is started..."
        cd ${WLAN_PRJ_ROOT}/BDP_Generic
        echo === sh config.sh $@ ${WLAN_DRV_ADDITIONAL_CONFIG}
        sh config.sh -f $@ ${WLAN_DRV_ADDITIONAL_CONFIG}
        
        if [ $? -ne 0 ]; then
            echo "===    `date` config fail!"
            exit 1
        else
            echo "===    `date` config success!"
        fi
    else
        echo "config.sh is skipped..."
    fi
    

    # Add config.mk symbolic
    #ln -sd ${WLAN_SRC_PATH}/${WLAN_DRV_SOURCE_NAME}/config.mk  ${WLAN_SRC_PATH}/${WLAN_DRV_SOURCE_NAME}/MODULE/os/linux/config.mk 
    #ln -sd ${WLAN_SRC_PATH}/${WLAN_DRV_SOURCE_NAME}/config.mk  ${WLAN_SRC_PATH}/${WLAN_DRV_SOURCE_NAME}/NETIF/os/linux/config.mk 
    #ln -sd ${WLAN_SRC_PATH}/${WLAN_DRV_SOURCE_NAME}/config.mk  ${WLAN_SRC_PATH}/${WLAN_DRV_SOURCE_NAME}/UTIL/os/linux/config.mk 
    
    # source again.
    cd $SCRIPT_PATH
    source ./wlan_setenv.sh

    # Adding symbolic hard link to linux kernel
    if [ -e ${WLAN_SRC_LINUX_PATH}/${WLAN_DRV_DST_LINK_NAME} ]; then
        echo "=== ${WLAN_DRV_DST_LINK_NAME} folder already exists, do not link ==="
    else
        echo "=== Adding symbolic hard link ... ==="
        ln -sd ${WLAN_SRC_PATH}/${WLAN_DRV_SOURCE_NAME} ${WLAN_SRC_LINUX_PATH}/${WLAN_DRV_DST_LINK_NAME}
    fi

    #Modify Kconfig & Makefile under drivers/net/wireless
    #Backup first
    if [ -e ${WLAN_SRC_LINUX_PATH}/Kconfig.bak ]; then
        cp -f ${WLAN_SRC_LINUX_PATH}/Kconfig.bak ${WLAN_SRC_LINUX_PATH}/Kconfig
    else
        cp -f ${WLAN_SRC_LINUX_PATH}/Kconfig ${WLAN_SRC_LINUX_PATH}/Kconfig.bak
    fi
    if [ -e ${WLAN_SRC_LINUX_PATH}/Makefile.bak ]; then
        cp -f ${WLAN_SRC_LINUX_PATH}/Makefile.bak ${WLAN_SRC_LINUX_PATH}/Makefile
    else
        cp -f ${WLAN_SRC_LINUX_PATH}/Makefile ${WLAN_SRC_LINUX_PATH}/Makefile.bak
    fi
    echo "Adjusting wireless/Kconfig..."
    if [ "`grep ${WLAN_DRV_DST_LINK_NAME}/Kconfig ${WLAN_SRC_LINUX_PATH}/Kconfig`" == "" ]; then
        sed -i '\/rt2x00/a source \"drivers\/net\/wireless\/'"$WLAN_DRV_DST_LINK_NAME"'\/Kconfig\"' ${WLAN_SRC_LINUX_PATH}/Kconfig
    fi
    echo "Adjusting wireless/Makefile..."
    if [ "`grep ${WLAN_DRV_DST_LINK_NAME} ${WLAN_SRC_LINUX_PATH}/Makefile`" == "" ]; then
        sed -i '$a obj-\$('"$WLAN_DRV_KCONFIG_NAME"')	+= '"$WLAN_DRV_DST_LINK_NAME"'/' ${WLAN_SRC_LINUX_PATH}/Makefile
    fi

    #Modify .config
    echo "Adjusting .config ..."
    if [ "`grep $WLAN_DRV_KCONFIG_NAME ${MODULE_OBJ_DIR}-${WLAN_KERNELRELEASE}/.config`" != "" ]; then
        #if CONFIG_XXX already in .config, we make sure it has been changed =m
        sed -i 's/^.*'"$WLAN_DRV_KCONFIG_NAME"'.*$/'"$WLAN_DRV_KCONFIG_NAME"'\=m/g' ${MODULE_OBJ_DIR}-${WLAN_KERNELRELEASE}/.config
    else
        #if CONFIG_XXX does not in .config, we add it as =m
        if [ $WLAN_KERNELRELEASE == 2.6.27 ]; then
            sed -i '/CONFIG_WIRELESS_EXT=y/a '"$WLAN_DRV_KCONFIG_NAME"'\=m' ${MODULE_OBJ_DIR}-${WLAN_KERNELRELEASE}/.config
        elif [ $WLAN_KERNELRELEASE == 2.6.35 ]; then
            sed -i '/CONFIG_WLAN/a '"$WLAN_DRV_KCONFIG_NAME"'\=m' ${MODULE_OBJ_DIR}-${WLAN_KERNELRELEASE}/.config
        fi
    fi

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

    #Start building
    echo "============================================================="
    echo "=== Compiling $WLAN_DRV_SOURCE_NAME on $PRJ_TYPE platform ..."
    echo "============================================================="
    cd ${WLAN_PRJ_ROOT}/BDP_Generic
    sh build.sh
    if [ $? -ne 0 ]; then
    echo ===    `date` XXXXXXXXX Building BDP fail XXXXXXXXXX!
    exit 1
    else
    echo ===    `date` OOOOOOOOO Building BDP success OOOOOOOOO!
    fi

    cd $WLAN_KO_DST_PATH
    mkdir -p $WLAN_SYSROOT_PATH

    #Copy ko to destination library path
    echo "Updating ko files..."
    for thisko in ${WLAN_DRV_KO_LIST};
    do
        if [ -f ${WLAN_OBJ_PATH}/${WLAN_DRV_DST_LINK_NAME}/${thisko}.ko ];then
            cp -f ${WLAN_OBJ_PATH}/${WLAN_DRV_DST_LINK_NAME}/${thisko}.ko ${WLAN_SYSROOT_PATH}/
        else
            echo XXXXX Error:  ${thisko}.ko was not found! XXXXX
            exit 1        
        fi
    done
    echo "${WLAN_KO_DST_PATH}/${WLAN_KO_DST_PATH_DIR_NAME}/ ko files are updated ..."

    #tar ko
    tar jcf $WLAN_TAR_NAME-install.tar.bz2 $WLAN_SYSROOT_PATH/*
    #rm -fr $WLAN_SYSROOT_PATH


    #Do a last build to make sure the latest ko was built with the image.
    cd ${WLAN_PRJ_ROOT}/BDP_Generic
    sh build.sh DIR=xxx





elif [ "${PRJ_TYPE}" == "DTV" ]; then
    echo "-----------------------------------------------"
    echo "       DTV platforms"
    echo "-----------------------------------------------"




fi


