#!/bin/sh
echo "running utscript.sh"

################ var for upgrade ################
UT_NAND_SIZE=2048
UT_DUAL_SD=0

################ check system HW ################
UT_SIZE_128M=134217728
UT_SIZE_256M=268435456
UT_SIZE_2G=2147483648
UT_MTD_SIZE=0

if [ -e /sys/block/mtdblock4 ]   
then
#### mtdblock4  exist ####

UT_MTD_SIZE=`cat /sys/block/mtdblock4/device/size`
echo mtd4 size:$UT_MTD_SIZE

if [ $UT_MTD_SIZE -lt $UT_SIZE_128M ]
then UT_NAND_SIZE=128
elif [ $UT_MTD_SIZE -lt $UT_SIZE_256M ]
then UT_NAND_SIZE=256
elif [ $UT_MTD_SIZE -lt $UT_SIZE_2G ]
then UT_NAND_SIZE=2048   
else
     UT_NAND_SIZE=4096
     echo nand is larger than 2G, we can only handle 4G currently.
fi
echo nand size: $UT_NAND_SIZE MB

else	
#### mtdblock4 is not exist ####
     UT_NAND_SIZE=0
     echo no nand flash found.
fi

if [ -e /sys/block/mmcblk1 ]
then echo dual SD card found
     UT_DUAL_SD=1
else
     echo single SD card
     UT_DUAL_SD=0
fi

################ start upgrade ################

echo "********************************"       > /dev/tty0
echo "* mid upgrade system v2.3"              > /dev/tty0
echo "********************************"       > /dev/tty0
echo "                                "       > /dev/tty0
echo "                                "       > /dev/tty0


echo "Erase system...                 "       > /dev/tty0
ubidetach /dev/ubi_ctrl -m 4
ubidetach /dev/ubi_ctrl -m 3
flash_eraseall /dev/mtd4

echo "make partitions...              "       > /dev/tty0
ubiattach /dev/ubi_ctrl -m 4 -d 0

if [ $UT_NAND_SIZE -ge 2048 ]
then
ubimkvol /dev/ubi0 -n 0 -N rootfs -s 128MiB
ubimkvol /dev/ubi0 -n 1 -N userdata -s 512MiB
ubimkvol /dev/ubi0 -n 2 -N cache -s 128MiB
ubimkvol /dev/ubi0 -n 3 -N flash -m

elif [ $UT_NAND_SIZE -eq 256 ]
then 
ubimkvol /dev/ubi0 -n 0 -N rootfs -s 100MiB
ubimkvol /dev/ubi0 -n 1 -N userdata -s 100MiB
ubimkvol /dev/ubi0 -n 2 -N cache -m

elif [ $UT_NAND_SIZE -eq 128 ]
then
ubimkvol /dev/ubi0 -n 0 -N rootfs -m

fi


if [ $UT_NAND_SIZE -gt 0 ]
then
################# nand / nand_sd upgrade ################


########## nand param upgrade ##########
mkdir /mnt/param/
mount -t yaffs2 /dev/mtdblock5 /mnt/param/

#### recover soft mac address ###
if [ -e /mnt/param/softmac ]
then
echo "found mac address:"                                     > /dev/tty0
echo  `cat /mnt/param/softmac`                                > /dev/tty0
else

if [ -e /mnt/mmc/mac.txt ]   
then
umount /mnt/param/
flash_eraseall /dev/mtd5
mount -t yaffs2 /dev/mtdblock5 /mnt/param/

for ADDR in $(cat /mnt/mmc/mac.txt)
do
  sed -ie "/$ADDR/d" /mnt/mmc/mac.txt 
  echo $ADDR > /mnt/param/softmac
  sync
  break
done
fi

sync
fi

########## nand system upgrade #########

echo "writing system, please wait for a few minutes..."       > /dev/tty0
mount -t ubifs ubi0:rootfs /mnt/mtd
cd /mnt/mtd
tar zxvf /mnt/mmc/utv210_root.tgz
#tar zxvf /mnt/mmc/board*.tgz
cp /mnt/mmc/customer/* ./ -rf
cp /mnt/mmc/utmodules/ ./system/ -rf
for UT_BOARD_PATCHES in `find /mnt/mmc/patches/ -name "*patch.tgz"`
do
	tar zxvf $UT_BOARD_PATCHES
	cat init.rc.append >> init.rc
	cat init.smdkv210.rc.append >> init.smdkv210.rc
	cat build.prop.append >> system/build.prop
	rm init.rc.append
	rm init.smdkv210.rc.append
	rm build.prop.append
done
cp /mnt/mmc/pic.gif ./system/media/pic.gif
cp /mnt/mmc/bootanimation.zip ./system/media/bootanimation.zip
cd /
cp /mnt/param/softmac /mnt/mtd/system/wifi/softmac
sync
umount /mnt/param
umount /mnt/mtd

echo "writing user data, please wait for a few minutes..."       > /dev/tty0
if [ $UT_DUAL_SD -eq 1 ]
then
sd_fdisk /dev/mmcblk0
sleep 1
dd if=./sd_mbr.dat of=/dev/mmcblk0
sync
fdisk /dev/mmcblk0 <<UT_FDISK_END
w
UT_FDISK_END
mkdosfs /dev/mmcblk0p1 -n sdcard
mkfs.ext4 /dev/mmcblk0p2
mkfs.ext4 /dev/mmcblk0p3
mount -t ext4 /dev/mmcblk0p3 /mnt/mtd
else
mount -t ubifs ubi0:userdata /mnt/mtd
fi

cd /mnt/mtd
tar zxvf /mnt/mmc/utv210_userdata.tgz
cd /
sync
umount /mnt/mtd

if [ $UT_NAND_SIZE -ge 2048 ]
then
echo "make NandDisk..."                        > /dev/tty0
mount -t ubifs ubi0:flash /mnt/mtd
cd /mnt/mtd
dd if=/dev/zero of=./vfat.img bs=1M count=0 seek=1100
mkdosfs ./vfat.img -n NandDisk
cd /
sync
fi

else
################# pure sd system upgrade ################
mkdosfs /dev/mmcblk0p1 -n sdcard
mkfs.ext4 /dev/mmcblk0p2
mkfs.ext4 /dev/mmcblk0p3
mkfs.ext4 /dev/mmcblk0p4

echo "writing system, please wait for a few minutes..."       > /dev/tty0
mount -t ext4 /dev/mmcblk0p4 /mnt/mtd
cd /mnt/mtd
tar zxvf /mnt/mmc/utv210_root.tgz
#tar zxvf /mnt/mmc/board*.tgz
cp /mnt/mmc/customer/* ./ -rf
cp /mnt/mmc/utmodules/ ./system/ -rf
for UT_BOARD_PATCHES in `find /mnt/mmc/patches/ -name "*patch.tgz"`
do
	tar zxvf $UT_BOARD_PATCHES
	cat init.rc.append >> init.rc
	cat init.smdkv210.rc.append >> init.smdkv210.rc
	cat build.prop.append >> system/build.prop
	rm init.rc.append
	rm init.smdkv210.rc.append
	rm build.prop.append
done
cp /mnt/mmc/pic.gif ./system/media/pic.gif
cp /mnt/mmc/bootanimation.zip ./system/media/bootanimation.zip
cd /
sync
umount /mnt/mtd

echo "writing user data, please wait for a few minutes..."     > /dev/tty0
mount -t ext4 /dev/mmcblk0p3 /mnt/mtd
cd /mnt/mtd
tar zxvf /mnt/mmc/utv210_userdata.tgz
cd /
sync
umount /mnt/mtd

fi


echo "finish!"                                 > /dev/tty0
echo "shutdown now."                           > /dev/tty0
reboot
