#!/bin/sh

update_ing="/mnt/nand1-2/update/update_ing"
update_ok="/mnt/nand1-2/update/update_ok"
update_kernel="/mnt/nand1-2/update/update_kernel"
#内核、程序和资源目录
kernel_path="/mnt/nand1-1"
app_path="/mnt/nand1-2/working"
resource_path="/mnt/nand1-2/working/Resources"
#update_log="/mnt/nand1-2/working/update.log"

#更新文件存放目录
updatePath="/mnt/nand1-2/update"
kernelPath="$updatePath/kernel"
appPath="$updatePath/app"
audioPath="$updatePath/audio"
databasePath="$updatePath/database"
driverPath="$updatePath/driver"
fontPath="$updatePath/font"
imgPath="$updatePath/img"
toolsPath="$updatePath/tools"
CustomizationPath="$updatePath/Customization"
xmlPath="$updatePath/xml"
xml_default_Path="/mnt/nand1-2/default"

sync
sync

#创建更新目录
if [ ! -d "$updatePath" ]; then 
	echo "creating update path..."
	mkdir -p "$updatePath"
	mkdir -p "$kernelPath"
	mkdir -p "$appPath"
	mkdir -p "$audioPath"
	mkdir -p "$databasePath"
	mkdir -p "$driverPath"
	mkdir -p "$fontPath"
	mkdir -p "$imgPath"
	mkdir -p "$toolsPath"
	mkdir -p "$xmlPath"
	mkdir -p "$CustomizationPath"
	sync
	echo "creat update path done!"
fi

if [ ! -d "$CustomizationPath" ]; then 
	mkdir -p "$CustomizationPath"
	sync
	echo "creat update path done!"
fi

#更新
if [ ! -f "$update_ing" ]; then
	echo "This is the last version,don't need to update..."	
else

	#touch $update_log
	#echo -e "update start...\n"  >> $update_log
	for dir in `ls $updatePath`
	do
		if [ $dir == "kernel" ]; then
			if [ ! -d "$kernel_path" ]; then 
				echo "mkdir -p $kernel_path"
				mkdir -p $kernel_path
			fi
			#echo "updating kernel..." >> $update_log
			for file in `ls $updatePath/$dir`
			do
				#echo "cp -af $file $kernel_path."
				cp -af $updatePath/$dir/$file $kernel_path
				sync
				#echo "$file copy finish." >> $update_log
				rm -rf $updatePath/$dir/$file
				touch $update_kernel
				sync
			done
			#echo -e "kernel update finish.\n" >> $update_log
			continue;
		fi
		
		if [ $dir == "app" ]; then
			if [ ! -d "$app_path" ]; then 
				echo "mkdir $app_path"
				mkdir -p $app_path
			fi
			#echo "updating app..." >> $update_log
			for file in `ls $updatePath/$dir`
			do
				#echo "cp -af $file $kernel_path"
				cp -af $updatePath/$dir/$file $app_path
				sync
				#echo "$file copy finish." >> $update_log
				rm -rf $updatePath/$dir/$file
				sync
			done
			#echo -e "app update finish. \n" >> $update_log
			continue;
		fi
		
		if [ $dir == "update_ing" ]; then
			continue;
		fi
		
		if [ ! -d "$resource_path/$dir" ]; then 
			echo "mkdir -p $resource_path/$dir"
			mkdir -p $resource_path/$dir
		fi
		#echo "updating $resource_path/$dir ..." >> $update_log
		for file in `ls $updatePath/$dir`
		do
			#echo "cp -af $file $resource_path/$dir" 
			cp -af $updatePath/$dir/$file $resource_path/$dir
			if [ $dir == "xml" ]; then
				#echo "cp -f $file $xml_default_Path"
				cp -af $updatePath/$dir/$file $xml_default_Path
			fi
			if [ $dir == "database" ]; then
				#echo "cp -f $file $xml_default_Path"
				cp -af $updatePath/$dir/$file $xml_default_Path
			fi
			rm -rf $updatePath/$dir/$file
			sync
			#echo "$file copy finish." >> $update_log
		done
		#echo -e "$resource_path/$dir update finish.\n" >> $update_log
	done
	rm -rf $update_ing
	touch $update_ok
	sync
	#echo -e "update done!\n" >> $update_log
	sync
fi

