#!/bin/sh

fail() {
    echo "Failed"
    echo "$1"
    cleanup
}

cleanup() {
umount /data
if [ $RM_dataloc -eq 2 ] ; then
	losetup -d /dev/block/loop1
fi
exec sleep 3
}

doLog() {
## this is where the install and config takes place ##
if [ $RM_dataloc -eq 0 ] ; then
	mount -t yaffs2 /dev/block/mtdblock3 /data > /dev/null
	[ $? -eq 0 ] || fail "Failed to mount"
elif [ $RM_dataloc -eq 1 ] ; then
	mount -t ext2 /dev/block/mmcblk0p3 /data
	[ $? -eq 0 ] || fail "Failed to mount"
elif [ $RM_dataloc -eq 2 ] ; then
	losetup /dev/block/loop1 /sdcard/andboot/data.img
	[ $? -eq 0 ] || fail "Failed to mount"
	mount -t ext2 /dev/block/loop1 /data
	[ $? -eq 0 ] || fail "Failed to mount"
fi

echo "Installing..."
if [ -f /sdcard/andboot/androidinstall.tgz ] ; then
	tar -xzf /sdcard/andboot/androidinstall.tgz data -C /
else
	tar -xf /sdcard/andboot/androidinstall.tar data -C /
fi
[ $? -eq 0 ] || fail "Failed to extract"
cleanup
}
doLog 2>&1 | /bin/tee -a /bootlog.txt
