#!/system/bin/sh
INACTIVE_SYSTEM_BANK=`busybox mount | busybox grep "/shadow_system" | busybox sed -e "s/\/dev\/block\/mmcblk0p\(.\).*/\1/"`
SYSTEM_A=5
SYSTEM_B=6

image_location=$1
image_location=${image_location%???}gz

failureFound=0
#BANK A -> 1-uboot / 3-boot / 5-system / 10-vendor / 7-recovery
#BANK B -> 2-uboot / 4-boot / 6-system / 11-vendor / 7-recovery

if [ $INACTIVE_SYSTEM_BANK = $SYSTEM_A ]; then #inactive bank is A
        newBoot=3
else
        newBoot=4
fi
 
echo -------$(date)------------ >> /var/log/upgrade.txt

echo "Flashing boot image on shadow bank starts:" >> /var/log/upgrade.txt

if [[ ! -f $image_location ]]; then
		echo "...: $image_location not found!" >> /var/log/upgrade.txt
		/system/bin/msg2plalog "boot image not found! error: $?" platform; failureFound=1;
fi

gunzip -c $image_location | busybox dd of=/dev/block/mmcblk0p$newBoot bs=2M
if [ $? -ne 0 ]; then
	/system/bin/msg2plalog "dd boot failed! error: $?" platform
	failureFound=1
	echo "boot image fail in command 1" >> /var/log/upgrade.txt
fi
resize2fs /dev/block/mmcblk0p$newBoot
if [ $? -ne 0 ]; then
	/system/bin/msg2plalog "resizefs boot failed! error: $?" platform
	failureFound=1
	echo "boot image fail in command 2" >> /var/log/upgrade.txt
fi

if [ failureFound -eq 0 ]; then
        echo "Flashing boot image on shadow bank passed!" >> /var/log/upgrade.txt
				/system/bin/msg2plalog "Flashing boot image on shadow bank passed!" platform
else
      echo "Flashing boot image on shadow bank failed!" >> /var/log/upgrade.txt
			/system/bin/msg2plalog "Flashing boot image on shadow bank failed!" platform
			touch /var/log/upgrade_fail
fi

