#!/bin/sh
KERNEL_VERSION="V2"
mcode=""
if [ ! -f "/root/vendor/firmware/machinecode" ];then
    echo "not excist machinecode"
else
	  mount -o remount,rw /dev/root
	  
    mcode=$(cat /root/vendor/firmware/machinecode)
    echo "model="$mcode
    if [ $mcode == "IT4PRO" ];then
        echo "mode equal"
    else
        echo "mode not equal"		
        rm -rf /root/vendor/update.zip
        rm -rf /root/data/update.zip
        rm /root/data/updateflag.txt
        reboot
        exit 1
    fi
fi

CUR_DIR="/tmp/update/"

KDIR="kernel/"
KERNEL="uImage"

UDIR="uboot/"
UBOOT="uboot.bin"

if [ -d $CUR_DIR$KDIR ]; then
	echo "kernel updata start"
	mount -t ext4 /dev/mmcblk0p4 /boot/
	cp $CUR_DIR$KDIR$KERNEL  /boot/
	sync
	umount /boot
	echo "Update kernel done"
fi

if [ -d $CUR_DIR$UDIR ]; then	
	echo "uboot updata start"
	dd if=$CUR_DIR$UDIR$UBOOT  of=/dev/mmcblk0p3 bs=1M
	sync
	echo "Update uboot done"
fi
rm -r /root/vendor/app/http/*

echo "start update firmware"

cp /tmp/update/fonts/zhonghei.ttf /root/data/PPL/ZPL/FONTS
cp /tmp/update/fonts/xihei.ttf /root/data/PPL/ZPL/FONTS
cp /tmp/update/fonts/msyhbd.ttf /root/data/PPL/ZPL/FONTS
cp /tmp/update/fonts/msyh.ttf /root/data/PPL/ZPL/FONTS

cp /tmp/update/fonts/* /root/data/fonts


cp /tmp/update/it4_app /root/vendor/firmware/
cp /tmp/update/it4_mcu.elf /root/vendor/firmware/

cp /tmp/update/http -r /root/vendor/app/

cp /tmp/update/snmpd -r /root/vendor/app/
cp /tmp/update/snmpd.conf -r /root/vendor/app/

cp /tmp/update/iot -r /root/vendor/app/
cp /tmp/update/path/gadget_function.service -r /root/vendor/driver/
cp /tmp/update/path/start_test.sh -rf /root/usr/local/config/shell/

chmod +x /root/vendor/firmware/*
chmod 777 /root/vendor/app/snmpd
chmod 777 /root/vendor/driver/gadget_function.service

rm -rf /root/etc/init.d/S50sshd

sync
############升级成功后回写md5file，recovery保证只升级一次#####################
############如果前面没挂载boot分区需要在这边挂载####################
#md5sum /mnt/usb/update.zip |awk '{print $1}' >/root/boot/md5file
#save md5 before delete update
if [ -f /root/data/update.zip ]; then
    md5sum /mnt/usb/update.zip > /root/boot/md5file
    rm -rf /root/data/update.zip
    rm /root/data/updateflag.txt
fi

if [ -f /mnt/usb/update.zip ]; then
    md5sum /mnt/usb/update.zip > /root/boot/md5file
fi

if [ -f /root/vendor/update.zip ]; then
     rm -rf /root/vendor/update.zip
fi
############升级成功后回写md5file，recovery保证只升级一次#####################
sync

reboot
