#!/bin/sh
source /OSM/script/os_load_base.sh

#热补丁相关临时文件
UPG_HP_TMP_PATCH_FILE="/tmp/hotpatch_tmp_file"
#获取1822补丁的版本号和升级路径
FW_1822_VER=1.5.8.1.0707 #1822固件版本号
SYS_VERSION_FILE=/OSM/conf/versions.conf
FW_1822_PATH=/startup_disk/image/boot/update_pkg/port/Hi1822_fw.bin  #1822固件路径
FW_1822_MODE=3  #1822固件升级模式

PANGEA_FwUpgradeSupportCheck()
{
diagnose --auto << EOF > $UPG_HP_TMP_PATCH_FILE
dpl hotupgradesupportcheck $FW_1822_VER $FW_1822_MODE 
quit
EOF
}
PANGEA_FwUpgradeStart()
{
diagnose --auto << EOF > $UPG_HP_TMP_PATCH_FILE
dpl hotupgradestart $FW_1822_PATH $FW_1822_MODE 
quit
EOF
}
PANGEA_FwUpgradeGetStatus()
{
diagnose --auto << EOF > $UPG_HP_TMP_PATCH_FILE
dpl gethotupgradestatus
quit
EOF
}
PANGEA_FwUpgradeVerCheck()
{
diagnose --auto << EOF > $UPG_HP_TMP_PATCH_FILE
dpl hotupgradefwvercheck $FW_1822_VER
quit
EOF
}
PANGEA_FwUpgradeMmlCmd()
{
FW_1822_VER=`cat /startup_disk/image/boot/versions.conf | grep "Hi1822_fw.bin" | tail -1 | awk -F "," '{print $2}'`
PANGEA_FwUpgradeVerCheck
vercheck=`cat $UPG_HP_TMP_PATCH_FILE | grep "failed."`
if [ -n "$vercheck" ]; then
	OS_echo "[1822fw]dpl hotupgradevercheck is not matched, upgrade it."
else
	OS_echo "[1822fw]dpl hotupgradevercheck is matched, return"
	exit 0 #当前版本号匹配，不做升级操作直接返回
fi
rm $UPG_HP_TMP_PATCH_FILE
PANGEA_FwUpgradeSupportCheck
checkResult=`cat $UPG_HP_TMP_PATCH_FILE | grep "is not supported"`
if [ -n "$checkResult" ]; then
	OS_echo "$checkResult"
	OS_echo "[1822fw]dpl hotupgradesupportcheck $FW_1822_VER $FW_1822_MODE is failed."
	exit 0 #执行升级检测失败返回0
fi
OS_echo "[1822fw]dpl hotupgradesupportcheck $FW_1822_VER $FW_1822_MODE is sucessed."
rm $UPG_HP_TMP_PATCH_FILE
PANGEA_FwUpgradeStart
OS_echo "[1822fw] dpl hotupgradestart $FW_1822_PATH $FW_1822_MODE is sucessed."
PANGEA_FwUpgradeGetStatus
getstatus=`cat $UPG_HP_TMP_PATCH_FILE | grep "failed."`
if [ -n "$getstatus" ]; then
	OS_echo "[1822fw]dpl hotupgradegetstatus is failed."
	exit 1 #执行升级状态检测失败返回1
fi
OS_echo "[1822fw]dpl hotupgradegetstatus is sucessed."
rm $UPG_HP_TMP_PATCH_FILE
PANGEA_FwUpgradeVerCheck
vercheck=`cat $UPG_HP_TMP_PATCH_FILE | grep "failed."`
if [ -n "$vercheck" ]; then
	OS_echo "[1822fw]dpl hotupgradevercheck is failed."
	exit 1 #执行升级完成版本号不匹配返回1
fi
rm $UPG_HP_TMP_PATCH_FILE
OS_echo "[1822fw]dpl hotupgradevercheck is sucessed."	
return 0
}

CHECK_PATCH_CONDITION()
{
	return
}

PATCH_START()
{
    return
}

PATCH_END()
{
    return
    OS_echo "[1822fw]Patch End clean up /OSM/replace_1822_card_firmware.tar.gz and /OSM/Hi1822_fw.bin"
    if [ -f /OSM/replace_1822_card_firmware.tar.gz ] ; then 
        OS_echo "[1822fw][product] ------patch replace_1822_card_firmware begain .------"
        cd /OSM/
        tar zxvf replace_1822_card_firmware.tar.gz

        #获取当前配置表的固件版本号
        cd /OSM/conf/
        orifwver=`cat ./versions.conf | grep "Hi1822_fw.bin" | tail -1 | awk -F "," '{print $2}'`
        OS_echo "[1822fw]the original fw version is $orifwver, SPH005 version is $FW_1822_VER"

        if [ $orifwver != $FW_1822_VER ]; then
            cp ./versions.conf ./versions_bak.conf

            #修改原versions.conf文件中的Hi1822fw版本号
            rm -rf ./versions.conf
            fwver=`cat ./versions_bak.conf | grep "Hi1822_fw.bin" | tail -1 | awk -F "," '{print $2}'`
            fwnum=`grep -n -A 1 "HI1822_FW" "versions_bak.conf" | tail -1 | awk -F "-" '{print $1}'`
            sed "/Hi1822_fw/ s/$fwver/$FW_1822_VER/" versions_bak.conf > versions_trans.conf
            sed "$fwnum s/$fwver/$FW_1822_VER/" versions_trans.conf > versions.conf
            rm -rf versions_trans.conf
            rm -rf ./versions_bak.conf
        fi
        cd -
        stafwver=`cat /startup_disk/image/boot/versions.conf | grep "Hi1822_fw.bin" | tail -1 | awk -F "," '{print $2}'`
        stafwver_1=`cat /startup_disk/image/boot_osp/versions.conf | grep "Hi1822_fw.bin" | tail -1 | awk -F "," '{print $2}'`
        OS_echo "[1822fw]boot 1822fw version $stafwver, boot_osp 1822fw version $stafwver_1"
        BOOT_LINK_a=`ls -l /startup_disk/image | grep -a "boot ->" | awk '{print $11}'` #检查boot指向
        OS_echo "[1822fw]boot dir now is $BOOT_LINK_a."
        boot_osp_dbver=`cat /startup_disk/image/boot_osp/versions.conf | grep "SpcVersion=" | awk -F "=" '{print $2}'`	
        OS_echo "[1822fw]boot_osp_dbver is $boot_osp_dbver."

        if [ $stafwver != $FW_1822_VER ]; then
            OS_echo "[1822fw][product] ------cp Hi1822_fw.bin to boot.------"
            cd /startup_disk/image/boot/

            #change disk to read-write
            sudo mount -o remount,rw,async /startup_disk/image/

            #备份1822卡固件文件
            cp ./update_pkg/port/Hi1822_fw.bin ./update_pkg/port/Hi1822_fw_bak.bin

            #备份sha256文件和version文件
            cp ./versions.conf ./versions_bak.conf
            cp ./sha256sum_sync ./sha256sum_sync_bak
            sync

            #替换1822卡固件文件和version文件
            cp /OSM/conf/versions.conf ./
            cp /OSM/Hi1822_fw.bin ./update_pkg/port/Hi1822_fw.bin

            BOARD_TYPE="`grep "^Product Name is" /proc/osp/bsp | awk '{print $4}'`"
            PANGEA_LINK=$BOARD_TYPE
            if [ -f ./product_versions.${PANGEA_LINK}.conf ]; then
                profwver=`cat /startup_disk/image/boot/product_versions.${PANGEA_LINK}.conf | grep "Hi1822_fw.bin" | tail -1 | awk -F "," '{print $2}'`
                if [ $profwver != $FW_1822_VER ]; then
                    cp ./product_versions.${PANGEA_LINK}.conf ./product_versions.${PANGEA_LINK}_bak.conf
                    rm -rf ./product_versions.${PANGEA_LINK}.conf
                    prover=`cat ./product_versions.${PANGEA_LINK}_bak.conf | grep "Hi1822_fw.bin" | tail -1 | awk -F "," '{print $2}'`
                    pronum=`grep -n -A 1 "HI1822_FW" "product_versions.${PANGEA_LINK}_bak.conf" | tail -1 | awk -F "-" '{print $1}'`
                    sed "/Hi1822_fw/ s/$prover/$FW_1822_VER/" product_versions.${PANGEA_LINK}_bak.conf > product_versions.${PANGEA_LINK}_trans.conf
                    sed "$pronum s/$prover/$FW_1822_VER/" product_versions.${PANGEA_LINK}_trans.conf > product_versions.${PANGEA_LINK}.conf
                    rm -rf product_versions.${PANGEA_LINK}_trans.conf
                else
                    OS_echo "[1822fw]the fw in product_versions.${PANGEA_LINK}.conf is $profwver"
                fi
            else
                OS_echo "[1822fw]the pangea_link is ${PANGEA_LINK}"
                OS_echo "[1822fw]product_versions.${PANGEA_LINK}.conf is not exist"
            fi
            sync
            #都要计算 特有问题 区分下路径
            local_sum=`sha256sum ./product_versions.${PANGEA_LINK}.conf`
            sed -i "/product_versions.${PANGEA_LINK}.conf/c$local_sum" ./sha256sum_sync

            local_sum=`sha256sum ./update_pkg/port/Hi1822_fw.bin`
            OS_echo "[1822fw]$local_sum"
            sed -i "/Hi1822_fw.bin/c$local_sum" ./sha256sum_sync
            sync
            OS_echo "[1822fw][product] ------patch replace_1822_card_firmware end .------"
        fi

        #执行1822固件升级命令
        PANGEA_FwUpgradeMmlCmd

        if [ $BOOT_LINK_a == "boot_osp_bak" ]; then
            boot_osp_bak_dbver=`cat /startup_disk/image/boot_osp_bak/versions.conf | grep "SpcVersion=" | awk -F "=" '{print $2}'`
            OS_echo "[1822fw]boot_osp_bak_dbver is $boot_osp_bak_dbver."
            if [ $boot_osp_dbver == $boot_osp_bak_dbver ]; then
                cp /OSM/conf/versions.conf /startup_disk/image/boot_osp/
                cp /OSM/Hi1822_fw.bin /startup_disk/image/boot_osp/update_pkg/port/Hi1822_fw.bin
            fi
        fi
    else
        OS_echo "[1822fw]can not find replace_1822_card_firmware.tar.gz"            
    fi

    return
}

PATCH_ROLLBACK()
{
    BOARD_TYPE="`grep "^Product Name is" /proc/osp/bsp | awk '{print $4}'`"
    PANGEA_LINK=$BOARD_TYPE
    #回退版本配套表
    cd /startup_disk/image/boot/

    #change disk to read-write
    sudo mount -o remount,rw,async /startup_disk/image/

    #mv ./versions_bak.conf ./versions.conf
    if [ -f versions_bak.conf ]; then
        fwbakver=`cat ./versions_bak.conf | grep "Hi1822_fw.bin" | tail -1 | awk -F "," '{print $2}'`
        prover_rol=`cat ./versions.conf | grep "Hi1822_fw.bin" | tail -1 | awk -F "," '{print $2}'`
        pronum_rol=`grep -n -A 1 "HI1822_FW" "versions.conf" | tail -1 | awk -F "-" '{print $1}'`
        sed -i "/Hi1822_fw/ s/$prover_rol/$fwbakver/" versions.conf
        sed -i "$pronum_rol s/$prover_rol/$fwbakver/" versions.conf
        prover_rol=`cat /OSM/conf/versions.conf | grep "Hi1822_fw.bin" | tail -1 | awk -F "," '{print $2}'`
        pronum_rol=`grep -n -A 1 "HI1822_FW" "/OSM/conf/versions.conf" | tail -1 | awk -F "-" '{print $1}'`
        sed -i "/Hi1822_fw/ s/$prover_rol/$fwbakver/" /OSM/conf/versions.conf
        sed -i "$pronum_rol s/$prover_rol/$fwbakver/" /OSM/conf/versions.conf
        if [ -f /OSM/conf/versions_bak.conf ]; then
            rm -rf /OSM/conf/versions_bak.conf
        fi
        rm -rf versions_bak.conf
    else
        OS_echo "[1822fw]There is no versions_bak.conf in /startup_disk/image/boot/"
    fi
    sync

    
    sync
    
    #回退固件
    if [ -f ./update_pkg/port/Hi1822_fw_bak.bin ]; then
        mv ./update_pkg/port/Hi1822_fw_bak.bin ./update_pkg/port/Hi1822_fw.bin
    else
        OS_echo "[1822fw]There is no Hi1822_fw_bak.bin in /startup_disk/image/boot/"
    fi
    #mv ./product_versions.${PANGEA_LINK}_bak.conf ./product_versions.${PANGEA_LINK}.conf
    if [ -f product_versions.${PANGEA_LINK}_bak.conf ]; then
        fwbakprover=`cat ./product_versions.${PANGEA_LINK}_bak.conf | grep "Hi1822_fw.bin" | tail -1 | awk -F "," '{print $2}'`
        prover_rpro=`cat ./product_versions.${PANGEA_LINK}.conf | grep "Hi1822_fw.bin" | tail -1 | awk -F "," '{print $2}'`
        pronum_rpro=`grep -n -A 1 "HI1822_FW" "product_versions.${PANGEA_LINK}.conf" | tail -1 | awk -F "-" '{print $1}'`
        sed -i "/Hi1822_fw/ s/$prover_rpro/$fwbakprover/" product_versions.${PANGEA_LINK}.conf
        sed -i "$pronum_rpro s/$prover_rpro/$fwbakprover/" product_versions.${PANGEA_LINK}_mids.conf
        rm -rf product_versions.${PANGEA_LINK}_bak.conf
    else
        OS_echo "[1822fw]There is no product_versions.${PANGEA_LINK}_bak.conf in /startup_disk/image/boot/"
    fi

    #都要计算 特有问题 区分下路径
    if [ -f sha256sum_sync ]; then
        sed -i "/product_versions.${PANGEA_LINK}.conf/d" ./sha256sum_sync
        sed -i "/versions.conf/d" ./sha256sum_sync
        sed -i "/Hi1822_fw.bin/d" ./sha256sum_sync
        sync
        rm -rf ./sha256sum_sync_bak
    fi

    rm -rf /OSM/Hi1822_fw.bin
    rm -rf /OSM/replace_1822_card_firmware.tar.gz

    hifc_sdk=`lsmod | awk '{print $1}' | grep hifc_sdk | wc -l`
    unfcommon=`lsmod | awk '{print $1}' | grep unfcommon | wc -l`

    if [[ $unfcommon -lt 1 && $hifc_sdk -lt 1 ]] ;then
        OS_echo "[1822_hot_patch] system is during power on, return."
        return
    fi
    
    #执行1822固件升级命令
    PANGEA_FwUpgradeMmlCmd
    
    return
}

PATCH_ROLLEND()
{
return
}

L_CMD=$1
case $L_CMD in
    "start") PATCH_START
               ;;
    "end") PATCH_END
               ;;
    "rollstart") PATCH_ROLLBACK
	CHECK_PATCH_CONDITION
               ;;
    "rollend") PATCH_ROLLEND
               ;;
    *)
         OS_echo "unsupported option."
esac
exit 0
