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

#修改点：
# 1 打补丁时检查SPH005是否return，若未return，修改SPH005整行替换/OSM/conf下的versions.conf文件。


PATCH_START()
{
    return
}

PATCH_END()
{
	#上电恢复补丁时不处理 
local pid=$(ps -ww -eo pid,cmd | grep "/OSM/bin/omm_upd_server.bin" | grep -v grep | awk '{print $1}')
if [ "$pid" = "" ]; then
    OS_echo "[1821fw] It is repoweron, no need process."
    return 0
else
	OS_echo	"[1821fw] begin to update 1821fw version.conf"
	sudo mount -o remount,rw,async /startup_disk/image/
	fwversion_1821ioc=`strings /startup_disk/image/boot_osp/update_pkg/port/ioc_fw.bin |grep "1.11T\|1.20T"|uniq`
	fwversion_1821_ioc_now=`cat /startup_disk/image/boot_osp/versions.conf |grep "ioc_fw.bin"|uniq|awk -F ',' '{print $2}'`
	if [ $fwversion_1821ioc !=  $fwversion_1821_ioc_now ];then
						  now_version_fwnum1=`grep -n -A 1 "\[SmartIO\]" "/startup_disk/image/boot_osp/versions.conf" | tail -1 | awk -F "-" '{print $1}'`
                          now_version_fwnum2=`grep -n -A 1 "\[IOC_HD\]" "/startup_disk/image/boot_osp/versions.conf" | tail -1 | awk -F "-" '{print $1}'`
                          now_version_fwnum3=`grep -n -A 1 "\[IOC_HD_16\]" "/startup_disk/image/boot_osp/versions.conf" | tail -1 | awk -F "-" '{print $1}'`
                          now_version_fwnum4=`grep -n -A 1 "\[IOC_10GE\]" "/startup_disk/image/boot_osp/versions.conf" | tail -1 | awk -F "-" '{print $1}'`
                          now_version_fwnum5=`grep -n -A 1 "\[IOC_8G\]" "/startup_disk/image/boot_osp/versions.conf" | tail -1 | awk -F "-" '{print $1}'`
                          now_version_fwnum6=`grep -n -A 1 "\[IOC_16G\]" "/startup_disk/image/boot_osp/versions.conf" | tail -1 | awk -F "-" '{print $1}'`
                          sed -i "/ioc_fw/ s/$fwversion_1821_ioc_now/$fwversion_1821ioc/" /startup_disk/image/boot_osp/versions.conf
                          sed -i "$now_version_fwnum1 s/$fwversion_1821_ioc_now/$fwversion_1821ioc/" /startup_disk/image/boot_osp/versions.conf
                          sed -i "$now_version_fwnum2 s/$fwversion_1821_ioc_now/$fwversion_1821ioc/" /startup_disk/image/boot_osp/versions.conf
                          sed -i "$now_version_fwnum3 s/$fwversion_1821_ioc_now/$fwversion_1821ioc/" /startup_disk/image/boot_osp/versions.conf
                          sed -i "$now_version_fwnum4 s/$fwversion_1821_ioc_now/$fwversion_1821ioc/" /startup_disk/image/boot_osp/versions.conf
                          sed -i "$now_version_fwnum5 s/$fwversion_1821_ioc_now/$fwversion_1821ioc/" /startup_disk/image/boot_osp/versions.conf
                          sed -i "$now_version_fwnum6 s/$fwversion_1821_ioc_now/$fwversion_1821ioc/" /startup_disk/image/boot_osp/versions.conf
						  if [ -f /startup_disk/image/boot_osp/patch_sha256sum ]; then
						  sed -i "/versions.conf/d" /startup_disk/image/boot_osp/patch_sha256sum
						  fi
		if [ $? != 0  ];then
			OS_echo	"[1821fw] update 1821fw version.conf fail."
			return 0
		fi	
		OS_echo	"[1821fw] begin to update 1821fw version.conf end"
	else
		OS_echo	"[1821fw] no need to update 1821fw version.conf"
	fi
    OS_echo "[1821fw] begin to update Hi1822 history hotpatch"
    if [ -d /OSM/coffer_bin/hotpatch/patch_bak ] ; then 
        OS_echo "[1821fw] patch_bak is exist , replace 1822 scripting in patch_bak."
		cd /OSM/coffer_bin/hotpatch/patch_bak
			if [ -f userpatch_script_replace_1822_card_firmware_SPH005_DTS2020071507363.sh ];then
				grep -B 1 "Patch End clean up" "userpatch_script_replace_1822_card_firmware_SPH005_DTS2020071507363.sh" | head -1 | grep "return"
				if_return_bak=$?
				OS_echo "[1821fw] if_return_bak is $if_return_bak"
				if [ $if_return_bak != 0 ]; then
					sudo mount -o remount,rw,async /OSM/coffer_bin/
					tar zxvf /OSM/replace_1822_card_SPH050_script.tar.gz > /dev/null
					if [ -f /OSM/coffer_bin/hotpatch/patch_bak/script_replace_1822_card_firmware_SPH050_update.sh ]; then
						mv /OSM/coffer_bin/hotpatch/patch_bak/script_replace_1822_card_firmware_SPH050_update.sh ./userpatch_script_replace_1822_card_firmware_SPH005_DTS2020071507363.sh
						OS_echo "[1821fw] get script in patch_bak"
					fi
					if [ -f patch_sha256sum ]; then
						sed -i "/userpatch_script_replace_1822_card_firmware_SPH005_DTS2020071507363.sh/d" ./patch_sha256sum
					fi
				fi
			else
				OS_echo	"[1821fw] patch_bak is no SPH005 script."
			fi
	else
	OS_echo "[1821fw] can not find patch_bak"
	fi
    if [ -d /OSM/coffer_bin/hotpatch/patch_cur ]; then
        OS_echo "[1821fw] ------patch_cur is exist , replace 1822 scripting in patch_cur.------"
        cd /OSM/coffer_bin/hotpatch/patch_cur
			if [ -f userpatch_script_replace_1822_card_firmware_SPH005_DTS2020071507363.sh ];then
				grep -B 1 "Patch End clean up" "userpatch_script_replace_1822_card_firmware_SPH005_DTS2020071507363.sh" | head -1 | grep "return"
				if_return_cur=$?
				OS_echo "[1821fw] if_return_cur is $if_return_cur"
				if [ $if_return_cur != 0 ]; then
					sudo mount -o remount,rw,async /OSM/coffer_bin/
					tar zxvf /OSM/replace_1822_card_SPH050_script.tar.gz > /dev/null
					if [ -f /OSM/coffer_bin/hotpatch/patch_cur/script_replace_1822_card_firmware_SPH050_update.sh ]; then
						mv /OSM/coffer_bin/hotpatch/patch_cur/script_replace_1822_card_firmware_SPH050_update.sh ./userpatch_script_replace_1822_card_firmware_SPH005_DTS2020071507363.sh
						OS_echo "[1821fw] get script in patch_cur"
					fi
					if [ -f patch_sha256sum ]; then
						sed -i "/userpatch_script_replace_1822_card_firmware_SPH005_DTS2020071507363.sh/d" ./patch_sha256sum
					fi
				fi
			else
				OS_echo	"[1821fw] patch_cur is no SPH005 script."
			fi
	else
    OS_echo "[1821fw] can not find patch_cur"
    fi

    return
fi
}

PATCH_ROLLBACK()
{
    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
