#!/bin/sh
#DSXUpdateUtility -- this line must always be the second line of this file
# START THE UPDATE PROCESS HERE

/usr/bin/dsxwdog 1000 &

/usr/bin/dsxtype
systype=$?

/usr/bin/dsxprogress 1

cd /ram
tar xvzf /ram/usbupdate.tgz

# Test and see if this is just an update of the dsxupdater program
if test -e /ram/dsxupdater
then

# put the new dsxupdater in the USB rfs
# remove it from the ram disk
# and restart (so the process uses the new dsxupdater)

	echo "Replacing DSX Updater..."
	cp /ram/dsxupdater /usr/bin/dsxupdater
	chmod +x /usr/bin/dsxupdater
	rm -f /ram/dsxupdater
	killall dsxwdog
	exec /etc/rc.d/rc.local
fi

/usr/bin/dsxprogress 5

# Test and see if this USB download is for other (maybe testing) purposes
if test -e /ram/dsxtest
then
	echo "Found dsxtest file on in /ram.  Executing dsxtest..."
        /ram/dsxtest
	exit 0
fi

/usr/bin/dsxprogress 6

# Test and see if this CF is for other (maybe testing) purposes
if test -e /ram/u-boot.bin
then
	echo "Found u-boot.bin file in /ram.  Executing dsxNOR to update NOR Flash (uboot)..."
	case "$systype" in
		0 )
		/usr/bin/dsxled 1 1
		/usr/bin/dsxled 2 0
		/usr/bin/dsxled 3 0
		/usr/bin/dsxled 4 0
		/usr/bin/dsxled 5 0
		/usr/bin/dsxled 6 1
		/usr/bin/dsxled 7 0
		/usr/bin/dsxNOR /ram/u-boot.bin
		/usr/bin/dsxled 6 0
		/usr/bin/dsxprogress 10
		;;

		1 )
		/usr/bin/dsxled 1 0
		/usr/bin/dsxled 2 1
		/usr/bin/dsxled 3 1
		/usr/bin/dsxNOR /ram/u-boot.bin
		/usr/bin/dsxled 1 1
		/usr/bin/dsxled 2 0
		/usr/bin/dsxled 3 0
		/usr/bin/dsxprogress 10
		;;
	esac
	echo "Done."
        echo "Turn off power or reset within the next 4 seconds to safely interrupt the update process..."
        sleep 7
fi

killall dsxledupdate
/usr/bin/dsxledupdate 2 &

echo "Starting the update from USB process..."
# START THE UPDATE PROCESS HERE
echo "Erasing kernel nand flash partition..."
/sbin/eraseall /dev/mtd/0

mount /nand/kernel

/usr/bin/dsxprogress 20

# First test and see if this usbupdate has the files available to update the USB boot partitions
if test -e /ram/usbboot.tgz
then
	echo "Found usbboot.tgz file in /ram.  Updating usbboot partitions..."
	/sbin/eraseall /dev/mtd/2
	/sbin/eraseall /dev/mtd/4
	mount /nand/usbroot
	sync
	cd /nand/usbroot
	echo "Updating usbboot root filesystem on NAND flash..."
	tar xvzf /ram/usbboot.tgz
	cd /
	umount /nand/usbroot
	mount /nand/usbkernel
	sync
	echo "Updating usbboot kernel on NAND flash..."
	cp /ram/vmlinux.pkg /nand/usbkernel
	umount /nand/usbkernel
	sync
fi

/usr/bin/dsxprogress 30

# Continuing the update process here
mount /nand/root
sync
# copy dsx Db to /ram (or anything else for that matter)
# Check if the /ram has a db.tar file, if so we are going to use this database
if test -e /ram/db.tar
then
	echo "Not Saving DSX DB or AA. Will copy Db usb upgrade (/ram) after update."
else
	echo "Saving DSX DB..."
	cd /nand/root/DSX
	tar cvf /ram/savedb.tar DB
	echo "Saving DSX AA..."
	tar cvf /ram/saveaa.tar AA/OGM
fi

cd /
umount /nand/root

echo "Erasing root nand flash partition..."
/sbin/eraseall /dev/mtd/6

/usr/bin/dsxprogress 35

mount /nand/root
sleep 4
sync

/usr/bin/dsxprogress 43 

cd /nand/root

echo "Updating root filesystem on NAND flash..."
tar xvzf /ram/root.tgz
/usr/bin/dsxprogress 85
# copy back dsx Db from cf (and possibly update (or anything else for that matter))
cd /nand/root/DSX
if test -e /ram/db.tar
then
	echo "Copying Db from from usb upgrade..."
	tar xvf /ram/db.tar
elif test -e /ram/aa.tar
then
	echo "Copying AA from from usb upgrade..."
	tar xvf /ram/aa.tar
else
	echo "Restoring DSX DB..."
	tar xvf /ram/savedb.tar
	rm -f /ram/savedb.tar
	echo "Restoring DSX AA..."
	tar xvf /ram/saveaa.tar
	rm -f /ram/saveaa.tar
fi

/usr/bin/dsxprogress 90

echo "Updating kernel on NAND flash..."
if [ -e /ram/linux26.pkg ]
then
cp /ram/linux26.pkg /nand/kernel/vmlinux.pkg
else
cp /ram/vmlinux.pkg /nand/kernel
fi
sync

/usr/bin/dsxprogress 97

cd /
sync
umount /nand/kernel
umount /nand/root
# END THE UPDATE PROCESS HERE
sync

echo "USB Update Process Complete."
/usr/bin/dsxprogress 99
reboot
#/usr/bin/dsxwdog -1
