#!/bin/sh
echo '$0: '$0
echo "pwd: "`pwd`
ls
echo $(cd `dirname $0`; pwd)
ls
cd $(cd `dirname $0`; pwd)

echo "Start Update" > Updatelog.txt
	
if [ -f "/home/root/Local.cal" ]; then
	echo "exist /home/root/Local.cal" >> Updatelog.txt
	if [ -f /home/root/Local.cal ]; then
		echo "exist /home/root/Local.cal" >> Updatelog.txt
#		cp /home/root/Local.cal ./
		sync
	else
		echo "No exist /home/root/Local.cal" >> Updatelog.txt
#		echo "cp /home/root/Local.cal ./AppData/" >> Updatelog.txt
#		cp /home/root/Local.cal ./AppData/
		sync
	fi
fi

if [ -f "./Htemp.fac" ]; then
	echo "cp ./Htemp.fac" >> Updatelog.txt
	cp ./Htemp.fac /home/root/
	sync
fi

if [ -f "./Ltemp.fac" ]; then
	echo "cp ./Ltemp.fac" >> Updatelog.txt
	cp ./Ltemp.fac /home/root/
	sync
fi
sync
	
if [ -d "./fonts" ]; then
	echo "./fonts" >> Updatelog.txt
	ls "./fonts/" >> Updatelog.txt

	echo "cp ./fonts/* /usr/share/fonts/" >> Updatelog.txt
	rm /usr/lib/fonts/* -fr
	sync
	cp ./fonts/* /usr/share/fonts/
	sync
	fc-cache -fv
	ls "/usr/share/fonts/" >> Updatelog.txt
fi

if [ -d "./ko" ]; then
	echo "cp ./ko/* /lib/modules/4.*/system/ -f" >> Updatelog.txt
	cp -f ./ko/* /lib/modules/4.*/system/ 
	sync
	
	depmod
	echo /lib/modules/4.*/ >> Updatelog.txt
	ls /lib/modules/4.*/ >> Updatelog.txt
	
	echo /lib/modules/4.*/system/ >> Updatelog.txt
	ls /lib/modules/4.*/system/ >> Updatelog.txt
	
	echo /lib/modules/4.*/kernel/ >> Updatelog.txt
	ls /lib/modules/4.*/kernel/ >> Updatelog.txt
fi

if [ -d "./Language" ]; then
	echo "cp ./Language/* /usr/share/Language/* -f" >> Updatelog.txt
	cp -rf ./Language /usr/share/ 
	sync
	
	ls /usr/share/Language/ >> Updatelog.txt
fi

if [ -d "./lib" ]; then
	cp ./lib/* /usr/lib/
fi

if [ -d "./ArbWaveLib/" ]; then
	echo "rm -fr /usr/share/ArbWaveLib*" >> Updatelog.txt
	rm -fr /usr/share/ArbWaveLib/*
	echo "cp -R ArbWaveLib/* /usr/share/ArbWaveLib" >> Updatelog.txt
	cp -Rf ArbWaveLib/* /usr/share/ArbWaveLib/
	sync
	sync
	sync
	sync
	sleep 1
fi

if [ -f "./mdev.conf" ]; then
	echo "cp mdev.conf /etc/" >> Updatelog.txt
	cp ./mdev.conf /etc/
	sync
fi

if [ -f "./udisk_hotplug.sh" ]; then
	echo "cp udisk_hotplug.sh /etc/hotplug/usb/" >> Updatelog.txt
	cp ./udisk_hotplug.sh /etc/hotplug/usb/
	sync
fi

if [ -f "./UTG5XXX" ]; then
	echo "cp UTG5XXX /usr/sbin" >> Updatelog.txt
	cp ./UTG5XXX /usr/sbin/
	sync
fi

if [ -f "./UTG5XXX.sh" ]; then
	echo "cp UTG5XXX.sh /etc/" >> Updatelog.txt
	cp cp UTG5XXX.sh /etc/
	sync
fi

if [ -f "./UTG9000T_TOP.bit" ]; then
	echo "cp UTG9000T_TOP.bit /home/root/" >> Updatelog.txt
	cp UTG9000T_TOP.bit /home/root/
	sync
fi

if [ -f "./flash_erase" ]; then
	echo "cp flash_erase /usr/sbin/" >> Updatelog.txt
	cp flash_erase /usr/sbin/
	sync
fi

if [ -f "./rcS" ]; then
	echo "cp rcS /etc/init.d/" >> Updatelog.txt
	cp ./rcS /etc/init.d/
	sync
fi

if [ -f "./fstab" ]; then
	echo "cp fstab /etc/" >> Updatelog.txt
	cp ./fstab /etc/
	sync
fi

if [ -f "./g_usbtmc.ko" ]; then
	echo "cp g_usbtmc.ko /lib/modules/4.14.67-*/user/" >> Updatelog.txt
	cp ./g_usbtmc.ko /lib/modules/4.14.67-*/user/
	depmod
	sync
fi

if [ -f "./g_zero.ko" ]; then
	echo "cp g_zero.ko /lib/modules/4.14.67-*/user/" >> Updatelog.txt
	cp ./g_zero.ko /lib/modules/4.14.67-*/user/
	depmod
	sync
fi

if [ -f "./zImage" ]; then
	echo "nand write zImage" >> Updatelog.txt
	flash_erase /dev/mtd8 0 0
	nandwrite  -p /dev/mtd8 zImage 
	#cp ./zImage /boot/
	sync
fi

if [ -f "./am335x-evm.dtb" ]; then
	echo "nand write am335x-evm.dtb " >> Updatelog.txt
	#cp ./am335x-evm.dtb /boot/
	flash_erase /dev/mtd4 0 0
	nandwrite  -p /dev/mtd4 am335x-evm.dtb
	sync
fi

if [ -f "./MLO" ]; then
	echo "write MLO " >> Updatelog.txt
	flash_erase /dev/mtd0 0 0
	flash_erase /dev/mtd1 0 0
	flash_erase /dev/mtd2 0 0
	flash_erase /dev/mtd3 0 0
	nandwrite  -p /dev/mtd0 MLO
	nandwrite  -p /dev/mtd1 MLO
	nandwrite  -p /dev/mtd2 MLO
	nandwrite  -p /dev/mtd3 MLO
	sync
	sync
	sync
fi

if [ -f "./u-boot.img" ]; then
	echo "write u-boot.img " >> Updatelog.txt
	flash_erase /dev/mtd5 0 0
	nandwrite  -p /dev/mtd5 u-boot.img
	sync
fi

echo "rm /home/root/Parm*" >> Updatelog.txt
rm /home/root/Parm*

#if [ -f "./Updatelog.txt" ]; then
#	cat ./Updatelog.txt
#	rm ./Updatelog.txt
#	sync
#fi

	sync
	sync
	sync
	sync
	
	


