#!/bin/sh
notify () { glcdd cls: asc:0:0:2:0:3:"$1" fls:; }
updated=1
m=/media/mmcblk0p1
notify 'Set updater SD'
if ! mount|grep -q $m && [ -b /dev/mmcblk0p1 ]; then mount /dev/mmcblk0p1 $m -t msdos; fi
while ! mount|grep -q $m; do sleep 1; done
for i in tgz bin; do
	U=$m/updater.$i
	[ -r $U ] && break
	U=""
done
if [ -z "$U" ]; then
	umount $m
	notify $'No updater.bin\nPUSH ANY KEY'
else
	notify 'Extracting...'
	mkdir -p /tmp/u
	mount none /tmp/u -t tmpfs -o size=40m
	tar zxf $U -C /tmp/u
	es=$?
	umount $m
	if [ $es != 0 ]; then
		umount /tmp/u
		notify $'Extracting error\nPUSH ANY KEY'
	elif ! [ -x /tmp/u/update ]; then
		umount /tmp/u
		notify $'Wrong updater\nPUSH ANY KEY'
	else
		notify 'Writing...'
		/tmp/u/update
		notify 'Completed'
		umount /tmp/u
		updated=0
	fi
fi
exit $updated
