#!/bin/sh

ustatus() {
TOTALSTEPS=5
echo "DLGSHOW^usb_update_progress^Update in progress [$1/$TOTALSTEPS]<br/>$2" >/tmp/umixpipe0
}


IMAGES="100-system.img 200-mixer.img 300-xorg.img 400-chrome.img"

mkdir -p /var/update
cd /var/update

rm -rf *.sh *.sig *.img

ustatus 1 "Unpacking images..."; unzip -o "$1" update.sh update.sh.sig $IMAGES
ustatus 2 "Unpacking kernel..."; unzip -o "$1" kernel.img kernel.img.sig
chmod a+x update.sh

if [ -f /var/update/kernel.img ] && [ "`md5sum /dev/mmcblk0p6`" != "`md5sum /var/update/kernel.img`" ]; then
    ustatus 3 "Flashing kernel..."
    /bin/verifyfile /var/update/kernel.img && ( cat /var/update/kernel.img >/dev/mmcblk0p6 )
fi

sync

rm -rf /fs/*.old

rm -rf /chrome/profile/Default/Cache
rm -rf /chrome/profile/Default/GPUCache
rm -rf /chrome/profile/ShaderCache

ustatus 4 "Moving images..."

for img in $IMAGES; do
    mv "/fs/$img"         "/fs/$img.old"
    mv "/var/update/$img" "/fs/$img"
    sync
done

ustatus 5 "Update complete. Please reboot."

echo -n ~\$FIRMWARE_UPDATED >/var/update/message.txt

exit 0
