#/bin/sh
#
# on-target updater script for Pandora
# Copyright (c) 2010, Gražvydas Ignotas

set -e

. /etc/updater_funcs.sh

status_uboot="missing"
status_uboot_env="already empty"
status_uimage="missing"
status_rootfs="missing"

mkdir /mnt/ubifs 2> /dev/null || true

if [ ! -e /dev/mmcblk0p1 ]
then
	# hmh what's up with the SD card?
	log "Can't access SD card, please reinsert and press enter"
	read a < /dev/tty3
	test -e /dev/mmcblk0p1
fi

mdevs="/dev/mmcblk0p1 /dev/mmcblk0p2"
bootfiles="bootf.tgz bootfiles.tar.gz uimage.tar.gz"

cd /tmp

for md in $mdevs
do
	mpoint="/mnt/`basename $md`"
	keep_mpoint="no"
	mkdir $mpoint 2> /dev/null || true
	if ! mount $md $mpoint
	then
		continue;
	fi

	for bf in $bootfiles
	do
		if [ -e $mpoint/$bf ]
		then
			tar xzvf $mpoint/$bf
		fi
	done

	if [ -x $mpoint/finalize.sh ]
	then
		finalize_mpoint=$mpoint
		keep_mpoint="yes"
	fi

	if [ -e $mpoint/rootfs.img -a -e $mpoint/rootfs.md5 -a -z "$rootfs_file" ]
	then
		rootfs_file=$mpoint/rootfs.img
		rootfs_md5=`head -n 1 $mpoint/rootfs.md5 | cut -c 1-32`
		rootfs_mpoint=$mpoint
		keep_mpoint="yes"
	fi

	if [ "$keep_mpoint" != "yes" ]
	then
		umount $mpoint
	fi
done

if [ -e u-boot.bin ]
then
	size=`stat -c %s u-boot.bin`
	dd if=/dev/mtdblock1 of=u-boot.bin.flashed bs=$size count=1
	if ! cmp u-boot.bin u-boot.bin.flashed
	then
		flash_eraseall /dev/mtd1
		nandwrite -p /dev/mtd1 u-boot.bin
		log "u-boot.bin flashed."
		status_uboot="flashed"
	else
		log "u-boot.bin already flashed, skipped."
		status_uboot="already there"
	fi
else
	log "u-boot is missing, skipped."
fi

if ! cmp /dev/mtdblock2 /usr/local/erasednandblk
then
	flash_eraseall /dev/mtd2
	status_uboot_env="cleared"
	log "u-boot environment cleared."
fi

if [ -e uImage ]
then
	# attempt to mount and check
	ubiattach /dev/ubi_ctrl -m 3 || true
	mount -t ubifs ubi0:boot /mnt/ubifs  2> /dev/null || true
	if ! cmp uImage /mnt/ubifs/uImage  2> /dev/null
	then
		log "flashing uImage.."
		umount /mnt/ubifs  2> /dev/null || true
		ubidetach /dev/ubi_ctrl -d 0  2> /dev/null || true
		# format just in case, should be harmless even it's formated already
		ubiformat /dev/mtd3 -s 512 -y
		ubiattach /dev/ubi_ctrl -m 3
		ubimkvol /dev/ubi0 -m -N boot
		mount -t ubifs ubi0:boot /mnt/ubifs
		cp uImage /mnt/ubifs/uImage
		status_uimage="flashed"
		log "done"
	else
		log "uImage already flashed, skipped."
		status_uimage="already there"
	fi
	umount /mnt/ubifs
	ubidetach /dev/ubi_ctrl -d 0
else
	log "uImage is missing, skipped."
fi

if [ -n "$rootfs_file" ]
then
	log "Calculating checksum, this might take a few minutes.."
	real_md5=`md5sum $rootfs_file | cut -c 1-32`
	if [ "$real_md5" = "$rootfs_md5" ]
	then
		log $real_md5
	else
		log "INCORRECT"
		false
	fi

	# be destructive here, too hard to validate rootfs to bother
	log "formatting.."
	ubiformat /dev/mtd4 -s 512 -y
	ubiattach /dev/ubi_ctrl -m 4
	ubimkvol /dev/ubi0 -m -N rootfs
	log "done."
	log "Writing rootfs, please wait. This may take up to 15 minutes.."
	ubiupdatevol /dev/ubi0_0 $rootfs_file
	ubidetach /dev/ubi_ctrl -d 0
	status_rootfs="flashed"
	log "done."
else
	log "root filesystem is missing, skipped."
fi

log "======================="
log "results:"
log "u-boot:     $status_uboot"
log "u-boot-env: $status_uboot_env"
log "kernel:     $status_uimage"
log "rootfs:     $status_rootfs $rootfs_file"

if [ -n "$finalize_mpoint" ]
then
	log ""
	log "running finalize script.."
	cd $finalize_mpoint
	if ! ./finalize.sh
	then
		log "finalize.sh failed."
		false
	fi
	log "done."
	cd /tmp
	umount $finalize_mpoint
fi

if [ -n "$rootfs_mpoint" ]
then
	umount $rootfs_mpoint 2> /dev/null || true
fi

exit 0
