#!/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
        newUboot=1
else
        newUboot=2
fi

echo "Flashing u-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 "u-boot image not found! error: $?" platform; failureFound=1;
fi

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

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

