#!/bin/sh
PATH=/sbin:/usr/sbin:/bin:/usr/bin:/home/mind/scripts

DESC="swupdate over the network"
NAME="swupdate-ota"
DAEMON=/usr/bin/swupdate

rootfs=`mount | grep "on / type" | cut -d':' -f 2 | cut -d' ' -f 1`

if [ $rootfs == '/dev/mmcblk0p2' ];then
	selection="-e stable,mmcblk0p3"
else
	selection="-e stable,mmcblk0p2"
fi

echo "Starting $DESC ..."
echo "rootfs is mounted on $rootfs and selection is $selection"

swupdate -v -P 'mount /dev/mmcblk0p1 /boot' -p 'umount /boot' ${selection} -w "-r /www"

cat /boot/uboot.env
if [ $? -ne 0 ]
then
    umount /dev/mmcblk0p1
    fsck -y /dev/mmcblk0p1
    mount /dev/mmcblk0p1 /boot
    rm /boot/uboot.env
    umount /boot
    poweroff
fi
