
height=20
pointX=30
pointY=50

if [ -f /mnt/mmcblk0p1/script/android_fs.tgz ] ; then
    string="Update filesystem Start ......"
    echo $string
    gui-echo $pointX $pointY "$string"
else
    string="--> Not find script/android_fs.tgz in SD Card"
    echo $string
    gui-echo $pointX $pointY "$string"
    pointY=$(($pointY + $height + $height))
    string="Fail to update filesystem. Exit!"
    echo $string
    gui-echo $pointX $pointY "$string"
    exit 0
fi

# -----> Begin Operate File-System Partition <---- #

pointY=$(($pointY + $height + $height))
string="Erase File-System partition ......"
echo $string
gui-echo $pointX $pointY "$string"
#pointY=$(($pointY + $height))

flash_eraseall /dev/mtd7
if [ $? -ne 0 ] ; then
#   pointY=$(($pointY + $height))
    pointY=$(($pointY + $height + $height))
    string="--> Erase File-System partition Fail! Exit!"
    echo $string
    gui-echo $pointX $pointY "$string"
    pointY=$(($pointY + $height))
    string="Maybe the kernel don't support this NAND Flash"
    echo $string
    gui-echo $pointX $pointY "$string"
    exit 0
else
   string="Erase File-System partition OK"
   echo $string
   gui-echo $pointX $pointY "$string"
fi
#pointY=$(($pointY + $height))

mount -t yaffs2 /dev/mtdblock7 /mnt/mtd
if [ $? -ne 0 ] ; then
    #pointY=$(($pointY + $height))
    pointY=$(($pointY + $height + $height))
    string="--> Mount File-System partition Fail! Exit!"
    echo $string
    gui-echo $pointX $pointY "$string"
    exit 0
else
    string="Mount File-System partition OK"
    echo $string
    gui-echo $pointX $pointY "$string"
fi

pointY=$(($pointY + $height))
string="Now Copy System Files, Pleas wait ......"
echo $string
gui-echo $pointX $pointY "$string"
#pointY=$(($pointY + $height))

tar zxvf /mnt/mmcblk0p1/script/android_fs.tgz -C /mnt/mtd

string="Copy System Files to File-System partition OK"
echo $string
gui-echo $pointX $pointY "$string"
pointY=$(($pointY + $height))

if [ -d /mnt/mmcblk0p1/script/driver ] ; then
    echo "Now Copy Drivers to File-System partition ......"
    cp -a /mnt/mmcblk0p1/script/driver/* /mnt/mtd

    string="Copy Drivers to File-System partition OK"
    echo $string
    gui-echo $pointX $pointY "$string"
    pointY=$(($pointY + $height))
fi

if [ -f /mnt/mmcblk0p1/script/busybox_1.16.tgz ] ; then
    string="Now Copy busybox to File-System partition ......"
    echo $string
    gui-echo $pointX $pointY "$string"
#    pointY=$(($pointY + $height))
    tar zxvf /mnt/mmcblk0p1/script/busybox_1.16.tgz -C /mnt/mtd
    string="Copy busybox to File-System partition OK"
    echo $string
    gui-echo $pointX $pointY "$string"
    pointY=$(($pointY + $height))    
    
    if [ -x /mnt/mtd/busybox/bin/ash ] ; then
        mv /mnt/mtd/system/bin/sh /mnt/mtd/system/bin/sh-org
        ln -s /busybox/bin/busybox /mnt/mtd/system/bin/sh
    fi
fi

chmod 777 -R /mnt/mtd
string="Update su to bin"
echo $string
gui-echo $pointX $pointY "$string"
mv /mnt/mtd/busybox/bin/su /mnt/mtd/busybox/bin/su.bak
chown root:root /mnt/mtd/system/bin/su
chmod 4777 /mnt/mtd/system/bin/su
sync
sleep 6
string="Update su Done!"
echo $string
umount /mnt/mtd

if [ $? -ne 0 ] ; then
    pointY=$(($pointY + $height))
    string="Unmount File-System partition Fail! Exit!"
    echo $string
    gui-echo $pointX $pointY "$string"
    exit 0
fi

# -----> End Operate File-System Partition <---- #


# -----> Begin Operate Data Partition <---- #

if [ -d /mnt/mmcblk0p1/script/etc ] || [ -f /mnt/mmcblk0p1/script/data.tgz ] ; then
    string="Erase Data partition ......"
    echo $string
    gui-echo $pointX $pointY "$string"
    #pointY=$(($pointY + $height))

    flash_eraseall /dev/mtd8
    if [ $? -ne 0 ] ; then
        #pointY=$(($pointY + $height))
        pointY=$(($pointY + $height + $height))
        string="--> Erase Data partition Fail!. Exit!"
        echo $string
        gui-echo $pointX $pointY "$string"
        pointY=$(($pointY + $height))
        string="Maybe the kernel don't support this NAND Flash"
        echo $string
        gui-echo $pointX $pointY "$string"
        exit 0
    else
       string="Erase Data partition OK"
       echo $string
       gui-echo $pointX $pointY "$string"
    fi
    #pointY=$(($pointY + $height))

    mount -t yaffs2 /dev/mtdblock8 /mnt/mtd
    if [ $? -ne 0 ] ; then
        #pointY=$(($pointY + $height))
        pointY=$(($pointY + $height + $height))
        string="--> Mount Data partition Fail!. Exit!"
        echo $string
        gui-echo $pointX $pointY "$string"
        exit 0
    else
        string="Mount Data partition OK"
        echo $string
        gui-echo $pointX $pointY "$string"
    fi
    pointY=$(($pointY + $height))

    string="Now Copy Files to Data partition, Please wait ......"
    echo $string
    gui-echo $pointX $pointY "$string"
    #pointY=$(($pointY + $height))
    
    if [ -f /mnt/mmcblk0p1/script/data.tgz ] ; then
        tar zxvf /mnt/mmcblk0p1/script/data.tgz -C /mnt/mtd
    fi
    
    if [ -d /mnt/mmcblk0p1/script/etc ] ; then
        if [ -d /mnt/mtd/wmtpref ] ; then
            cp -a /mnt/mmcblk0p1/script/etc/* /mnt/mtd/wmtpref
        else
            mkdir /mnt/mtd/wmtpref
            cp -a /mnt/mmcblk0p1/script/etc/* /mnt/mtd/wmtpref
        fi
    fi

    string="Copy Files to Data partition OK"
    echo $string
    gui-echo $pointX $pointY "$string"
    pointY=$(($pointY + $height))

    chmod 777 -R /mnt/mtd
    sync
    umount /mnt/mtd

    if [ $? -ne 0 ] ; then
        pointY=$(($pointY + $height))
        string="Unmount Data partition Fail! Exit!"
        echo $string
        gui-echo $pointX $pointY "$string"
        exit 0
    fi
fi
# -----> End Operate Data Partition <---- #

# -----> Begin Operate Cache Partition <---- #

if [ -f /mnt/mmcblk0p1/script/cache.tgz ] ; then
    string="Erase Cache partition ......"
    echo $string
    gui-echo $pointX $pointY "$string"
    #pointY=$(($pointY + $height))

    flash_eraseall /dev/mtd9
    if [ $? -ne 0 ] ; then
        #pointY=$(($pointY + $height))
        pointY=$(($pointY + $height + $height))
        string="--> Erase Cache partition Fail!. Exit!"
        echo $string
        gui-echo $pointX $pointY "$string"
        pointY=$(($pointY + $height))
        string="Maybe the kernel don't support this NAND Flash"
        echo $string
        gui-echo $pointX $pointY "$string"
        exit 0
    else
       string="Erase Cache partition OK"
       echo $string
       gui-echo $pointX $pointY "$string"
    fi
    #pointY=$(($pointY + $height))
    
    mount -t yaffs2 /dev/mtdblock9 /mnt/mtd
    if [ $? -ne 0 ] ; then
        #pointY=$(($pointY + $height))
        pointY=$(($pointY + $height + $height))
        string="--> Mount Data partition Fail!. Exit!"
        echo $string
        gui-echo $pointX $pointY "$string"
        exit 0
    else
        string="Mount Cache partition OK"
        echo $string
        gui-echo $pointX $pointY "$string"
    fi
    pointY=$(($pointY + $height))
    
    string="Now Copy Files to Cache partition, Please wait ......"
    echo $string
    gui-echo $pointX $pointY "$string"
    #pointY=$(($pointY + $height))
    tar zxvf /mnt/mmcblk0p1/script/chache.tgz -C /mnt/mtd
    
    string="Copy Files to Cache partition OK"
    echo $string
    gui-echo $pointX $pointY "$string"
    pointY=$(($pointY + $height))

    chmod 777 -R /mnt/mtd
    sync
    umount /mnt/mtd

    if [ $? -ne 0 ] ; then
        pointY=$(($pointY + $height))
        string="Unmount Cache partition Fail! Exit!"
        echo $string
        gui-echo $pointX $pointY "$string"
        exit 0
    fi 
fi

# -----> End Operate Cache Partition <---- #

string="Update filesystem successful!"
echo $string
gui-echo $pointX $pointY "$string"
pointY=$(($pointY + $height + $height))

# -----> Begin Operate User Space <---- #

if [ -f /mnt/mmcblk0p1/script/driver/g_file_storage.ko ] ; then
    string="Erase User Space......"
    echo $string
    gui-echo $pointX $pointY "$string"
    #pointY=$(($pointY + $height))

    flash_eraseall /dev/mtd11

    if [ $? -ne 0 ] ; then
        #pointY=$(($pointY + $height))
        pointY=$(($pointY + $height + $height))
        string="--> Erase User Space Fail! Exit!"
        echo $string
        gui-echo $pointX $pointY "$string"
        exit 0
    else
        string="Erase User Space OK"
        echo $string
        gui-echo $pointX $pointY "$string"
    fi
    #pointY=$(($pointY + $height))
    mount -t yaffs2 /dev/mtdblock11 /mnt/mtd

    if [ $? -ne 0 ] ; then
        #pointY=$(($pointY + $height))
        pointY=$(($pointY + $height + $height))
        string="--> Mount User Space Fail! Exit!"
        echo $string
        gui-echo $pointX $pointY "$string"
        exit 0
    else
        string="Mount User Space OK"
        echo $string
        gui-echo $pointX $pointY "$string"
    fi

    pointY=$(($pointY + $height))
    string="Create Loop File ..."
    echo $string
    gui-echo $pointX $pointY "$string"
    #pointY=$(($pointY + $height))

    create_loopfile mtd11 /mnt/mtd/UserFile bs=1024

    if [ $? -ne 0 ] ; then
        #pointY=$(($pointY + $height))
        pointY=$(($pointY + $height + $height))
        string="--> Create Loop File Fail! Exit!"
        echo $string
        gui-echo $pointX $pointY "$string"
        exit 0
    else
        if [ -f /mnt/mtd/UserFile ] ; then
            string="Create Loop File Successful"
            echo $string
            gui-echo $pointX $pointY "$string"
        else
            #pointY=$(($pointY + $height))
            pointY=$(($pointY + $height + $height))
            string="--> Create Loop File Fail! Exit!"
            echo $string
            gui-echo $pointX $pointY "$string"
            exit 0
        fi
    fi
    pointY=$(($pointY + $height))

    string="Format Loop File ..."
    echo $string
    gui-echo $pointX $pointY "$string"
    #pointY=$(($pointY + $height))

    mkdosfs /mnt/mtd/UserFile

    if [ $? -ne 0 ] ; then
        #pointY=$(($pointY + $height))
        pointY=$(($pointY + $height + $height))
        string="--> Format Loop File Fail! Exit!"
        echo $string
        gui-echo $pointX $pointY "$string"
        exit 0
    else
        string="Format Loop File Successful"
        echo $string
        gui-echo $pointX $pointY "$string"
    fi
    
    if [ -d /mnt/mmcblk0p1/script/DemoFile ] ; then
        pointY=$(($pointY + $height))

        losetup /dev/loop/0 /mnt/mtd/UserFile
        if [ $? -ne 0 ] ; then
            pointY=$(($pointY + $height))
            string="--> losetup Fail! Exit!"
            echo $string
            gui-echo $pointX $pointY "$string"
            exit 0
        fi
        mkdir /LocalDisk
        mount -o iocharset=gb2312 -t vfat /dev/loop/0  /LocalDisk
        if [ $? -ne 0 ] ; then
            pointY=$(($pointY + $height))
            string="--> mount /dev/loop/0 to /LocalDisk Fail! Exit!"
            echo $string
            gui-echo $pointX $pointY "$string"
            exit 0
        fi
        string="Now Copy Demo Files to User Space, Please wait ......"
        echo $string
        gui-echo $pointX $pointY "$string"
        
        cp -a /mnt/mmcblk0p1/script/DemoFile /LocalDisk
        
        string="Copy Demo Files to User Space complete"
        echo $string
        gui-echo $pointX $pointY "$string"
        
        chmod 777 -R /LocalDisk
        sync
        umount /LocalDisk
        if [ $? -ne 0 ] ; then
            pointY=$(($pointY + $height))
            string="--> umount LocalDisk Fail! Exit!"
            echo $string
            gui-echo $pointX $pointY "$string"
            exit 0
        fi   
        losetup -d /dev/loop/0
        if [ $? -ne 0 ] ; then
            pointY=$(($pointY + $height))
            string="--> losetup -d /dev/loop/0 Fail! Exit!"
            echo $string
            gui-echo $pointX $pointY "$string"
            exit 0
        fi
    fi
    
    chmod 777 -R /mnt/mtd
    sync
    umount /mnt/mtd

    if [ $? -ne 0 ] ; then
        pointY=$(($pointY + $height + $height))
        string="Unmount User Space Fail! Exit!"
        echo $string
        gui-echo $pointX $pointY "$string"
        exit 0
    fi

    pointY=$(($pointY + $height + $height))
else
#### --- Not Exit /mnt/mmcblk0p1/script/driver/g_file_storage.ko ----
    if [ -d /mnt/mmcblk0p1/script/DemoFile ] ; then
        string="Erase User Space......"
        echo $string
        gui-echo $pointX $pointY "$string"
        #pointY=$(($pointY + $height))

        flash_eraseall /dev/mtd11

        if [ $? -ne 0 ] ; then
            #pointY=$(($pointY + $height))
            pointY=$(($pointY + $height + $height))
            string="--> Erase User Space Fail! Exit!"
            echo $string
            gui-echo $pointX $pointY "$string"
            exit 0
        else
            string="Erase User Space OK"
            echo $string
            gui-echo $pointX $pointY "$string"
        fi
        #pointY=$(($pointY + $height))
        mount -t yaffs2 /dev/mtdblock11 /mnt/mtd

        if [ $? -ne 0 ] ; then
            #pointY=$(($pointY + $height))
            pointY=$(($pointY + $height + $height))
            string="--> Mount User Space Fail! Exit!"
            echo $string
            gui-echo $pointX $pointY "$string"
            exit 0
        else
            string="Mount User Space OK"
            echo $string
            gui-echo $pointX $pointY "$string"
        fi

        pointY=$(($pointY + $height))
                
        string="Now Copy Demo Files to User Space, Please wait ......"
        echo $string
        gui-echo $pointX $pointY "$string"
        
        cp -a /mnt/mmcblk0p1/script/DemoFile /mnt/mtd
        
        string="Copy Demo Files to User Space complete"
        echo $string
        gui-echo $pointX $pointY "$string"
        
        chmod 777 -R /mnt/mtd
        sync
        umount /mnt/mtd
        
        if [ $? -ne 0 ] ; then
            pointY=$(($pointY + $height + $height))
            string="Unmount User Space Fail! Exit!"
            echo $string
            gui-echo $pointX $pointY "$string"
            exit 0
        fi

        pointY=$(($pointY + $height + $height))
    fi
fi

# -----> End Operate User Space <---- #

echo 0 > /proc/boot-splash
string="All Update successful... waiting for remove SD Card"
echo $string
gui-echo $pointX $pointY "$string"
pointY=$(($pointY + $height))

string="Please remove SD Card ......"
while [ -x /bin/ls ]
do
    if [ -x /mnt/mmcblk0p1/script/update.sh ] ; then
        echo $string
        gui-echo $pointX $pointY "$string"
        sleep 1       
    else
        break
    fi
done

pointY=$(($pointY + $height))

string="Reboot now..."
echo $string
gui-echo $pointX $pointY "$string"
reboot

