#! /bin/sh

vmlinuz=/boot/vmlinuz

[ x"$1" = x"-v" ] && VERBOSE="-v"

if [ ! -x /usr/sbin/flashcp ]; then
	[ -n "$VERBOSE" ] && echo Could not find /usr/sbin/flashcp executable.
	exit 0
fi

if ! diff -u - /proc/mtd <<EOF ; then
dev:    size   erasesize  name
mtd0: 00060000 00020000 "cfe"
mtd1: 00020000 00020000 "config"
mtd2: 00140000 00020000 "linux"
mtd3: 00620000 00020000 "jffs"
mtd4: 00020000 00020000 "nvram"
EOF
	[ -n "$VERBOSE" ] && echo Unexpected mtd flash configuration.
	exit 0
fi

if [ ! -f $vmlinuz ] || [ ! -L $vmlinuz ] || \
[ 7f454c4601010100000000000000000002000800010000003400000034002000 != \
`zcat $vmlinuz|hexdump -vn 48 -e '/1 "%02x"'|cut -c 1-48,57-64,81-88` ]; then
	[ -n "$VERBOSE" ] && echo Could not find a valid $vmlinuz to flash.
	exit 0
fi

if cmp -n `du -bL $vmlinuz` /dev/mtd2 >/dev/null; then
	[ -n "$VERBOSE" ] && echo $vmlinuz has already been flashed.
	exit 0
fi

exec /usr/sbin/flashcp $VERBOSE $vmlinuz /dev/mtd2
