#!/bin/sh

SD_DEV_1=/dev/mmcblk0p1
SD_DEV_2=/dev/mmcblk0p2
UPDATE_FOLDER=$(cd `dirname $0`; pwd)
KERNEL_FILENAME=$UPDATE_FOLDER/uImage_mtd2
KERNEL_PARTITION=/dev/mtd2
KERNEL_INFO_FILE=$UPDATE_FOLDER/KernelVersion.txt
KERNEL_FORCE_UPDATE=$UPDATE_FOLDER/KernelForceUpdate.txt

mount_check=`mount | grep $SD_DEV_2 | awk '{print $1}'`
if [ "$mount_check" == "" ] ; then
	LSP_DEMO_DIR=/opt
else	
        LSP_DEMO_DIR=/mnt/sd2/opt
fi

mount -t ubifs ubi0_0 -o remount,rw,sync /

cp -af $UPDATE_FOLDER/opt/io.ko $LSP_DEMO_DIR
cp -af $UPDATE_FOLDER/opt/SENSOR.ko $LSP_DEMO_DIR
cp -af $UPDATE_FOLDER/opt/GL965GW $LSP_DEMO_DIR
cp -af $UPDATE_FOLDER/opt/autorun.sh $LSP_DEMO_DIR
cp -af $UPDATE_FOLDER/opt/Gamma_Table_Normal.txt $LSP_DEMO_DIR
cp -af $UPDATE_FOLDER/opt/RGB_CCM2_Gray.txt $LSP_DEMO_DIR
#cp -af $UPDATE_FOLDER/opt/RGB_CCM2_GrayBlue.txt $LSP_DEMO_DIR
cp -af $UPDATE_FOLDER/opt/RGB_CCM2_GrayGreen.txt $LSP_DEMO_DIR
cp -af $UPDATE_FOLDER/opt/RGB_CCM2_GrayRed.txt $LSP_DEMO_DIR
cp -af $UPDATE_FOLDER/opt/RGB_CCM2_GrayRG.txt $LSP_DEMO_DIR

cp -af $UPDATE_FOLDER/bin/lowntfs-3g /bin
cp -af $UPDATE_FOLDER/bin/ntfs-3g /bin
cp -af $UPDATE_FOLDER/bin/ntfs-3g.probe /bin
cp -af $UPDATE_FOLDER/bin/ntfs-3g.secaudit /bin
cp -af $UPDATE_FOLDER/bin/ntfs-3g.usermap /bin
cp -af $UPDATE_FOLDER/bin/ntfscat /bin
cp -af $UPDATE_FOLDER/bin/ntfscluster /bin
cp -af $UPDATE_FOLDER/bin/ntfscmp /bin
cp -af $UPDATE_FOLDER/bin/ntfsfix /bin
cp -af $UPDATE_FOLDER/bin/ntfsinfo /bin
cp -af $UPDATE_FOLDER/bin/ntfsls /bin

cp -af $UPDATE_FOLDER/sbin/mkntfs /sbin
cp -af $UPDATE_FOLDER/sbin/ntfsclone /sbin
cp -af $UPDATE_FOLDER/sbin/ntfscp /sbin
cp -af $UPDATE_FOLDER/sbin/ntfslabel /sbin
cp -af $UPDATE_FOLDER/sbin/ntfsresize /sbin
cp -af $UPDATE_FOLDER/sbin/ntfsundelete /sbin
cp -af $UPDATE_FOLDER/sbin/ntfsls /sbin

cp -af $UPDATE_FOLDER/lib/libntfs-3g.a /lib
cp -af $UPDATE_FOLDER/lib/libntfs-3g.la /lib
cp -af $UPDATE_FOLDER/lib/libntfs-3g.so /lib
cp -af $UPDATE_FOLDER/lib/libntfs-3g.so.86 /lib
cp -af $UPDATE_FOLDER/lib/libntfs-3g.so.86.0.0 /lib

if [ -d $UPDATE_FOLDER/opt/res ] ; then 
#rm -rf $LSP_DEMO_DIR/res
cp -af $UPDATE_FOLDER/opt/res $LSP_DEMO_DIR
fi

#rm -rf $UPDATE_FOLDER

dev5="/dev/sda5"
if [ -f "$dev5" ]; then
 echo "Find /dev/sda5"
else
 echo "Create /dev/sda5"
 mknod -m 666 /dev/sda5 b 8 5
fi

sync;sync;sync;
mount -t ubifs ubi0_0 -o remount,ro,sync /

KERNEL_MODEL_NEW=`grep "KernelModel" $KERNEL_INFO_FILE | awk -F= {'printf $2'}`
KERNEL_VERSION_NEW=`grep "KernelVersion" $KERNEL_INFO_FILE | awk -F= {'printf $2'}`
KERNEL_DATE_NEW=`grep "KernelDate" $KERNEL_INFO_FILE | awk -F= {'printf $2'}`

if [ -f $KERNEL_FORCE_UPDATE ]; then
        echo "The version of update file is $KERNEL_VERSION_NEW(Force Update)."
        UPDATEKERNEL_FLAG=1
else
        UPDATEKERNEL_FLAG=0
        if [ -f $KERNEL_FILENAME ]; then
                if [ -f /proc/pieversion ]; then
                        KERNEL_MODEL_OLD=`grep "KernelModel" /proc/pieversion | awk -F= {'printf $2'}`
                        KERNEL_VERSION_OLD=`grep "KernelVersion" /proc/pieversion | awk -F= {'printf $2'}`
                        KERNEL_DATE_OLD=`grep "KernelDate" /proc/pieversion | awk -F= {'printf $2'}`
                        if [ $KERNEL_MODEL_OLD = $KERNEL_MODEL_NEW ]; then
                                if [ "$KERNEL_VERSION_NEW" -gt "$KERNEL_VERSION_OLD" ]; then
                                        UPDATEKERNEL_FLAG=1
                                else
                                        echo "The version of Glite965GW is $KERNEL_VERSION_OLD, version of update file is $KERNEL_VERSION_NEW."
                                fi
                        else
                                echo "The project does not match.($KERNEL_MODEL_OLD, $KERNEL_MODEL_NEW)"
                        fi
                else
                        echo "The version of update file is $KERNEL_VERSION_NEW(/proc/pieversion not exist)"
                        UPDATEKERNEL_FLAG=1
                fi
        fi
fi

if [ "$UPDATEKERNEL_FLAG" -eq 1 ]; then
        echo "=== update kernel: $KERNEL_FILENAME($KERNEL_VERSION_NEW) ==="
        echo "--- update kernel: earse $KERNEL_PARTITION ---"
        flash_eraseall $KERNEL_PARTITION
        sync;sync;sync;
        sleep 2

        echo "--- update kernel: write $KERNEL_PARTITION ---"
        nandwrite -p $KERNEL_PARTITION $KERNEL_FILENAME
        sync;sync;sync;
        sleep 2

        echo "--- update kernel: finished ---"
fi

reboot
