#!/bin/sh

#version 1117

SYS_LITE_PATH="/test/DKLite"
USER_LITE_PATH="/mnt/us/DK_System/Lite"

#killall the app before installation happening 
killall -9 ebook
killall -9 KindleApp
killall -9 UsbSignal.bin
killall -9 BatterySignal.bin
killall -9 Charging.bin
killall -9 notCharging.bin
killall -9 PowerState.bin
killall -9 suspending.bin
killall -9 lipc-wait-event
lipc-set-prop com.lab126.powerd preventScreenSaver 0

mntroot rw

#rm all old DuoKan script
rm -f /etc/rc5.d/S79switch 
rm -f /etc/rc5.d/S95dkupdate 
rm -f /etc/rc5.d/duokantag 
rm -f /etc/rc5.d/rundk.sh 
rm -f /etc/rc5.d/S96rundk
rm -f /etc/rc5.d/S95framework
rm -f /etc/rc5.d/s95framework
rm -f /etc/rcS.d/S79switch
ln -s /etc/init.d/framework /etc/rc5.d/S95framework

rm -f /etc/init.d/DK_update
rm -f /etc/init.d/DK_switch
rm -f /etc/init.d/DK_run

rm -f /etc/rc5.d/S79DK_update
rm -f /etc/rc5.d/S95DK_switch
rm -f /etc/rc5.d/S96DK_run

sync

cp /mnt/us/DK_System/install/DK_update 	/etc/init.d/DK_update
cp /mnt/us/DK_System/install/DK_switch 	/etc/init.d/DK_switch
cp /mnt/us/DK_System/install/DK_run    	/etc/init.d/DK_run
sync

chmod +x /etc/init.d/DK_update
chmod +x /etc/init.d/DK_switch
chmod +x /etc/init.d/DK_run

ln -s /etc/init.d/DK_update /etc/rc5.d/S79DK_update
ln -s /etc/init.d/DK_switch /etc/rc5.d/S95DK_switch
ln -s /etc/init.d/framework /etc/rc5.d/S95framework
ln -s /etc/init.d/DK_run 	/etc/rc5.d/S96DK_run 

sync

		
mntroot rw
rm /core.* -f
mntroot rw
rm -rf $SYS_LITE_PATH/* 

mntroot rw
mkdir -p /test
mkdir -p $SYS_LITE_PATH

mntroot rw
mv $USER_LITE_PATH/KindleApp $SYS_LITE_PATH/KindleApp
chmod +x $SYS_LITE_PATH/KindleApp
		
mntroot rw
mv $USER_LITE_PATH/UsbSignal.bin $SYS_LITE_PATH/UsbSignal.bin
chmod +x $SYS_LITE_PATH/UsbSignal.bin

mntroot rw
mv $USER_LITE_PATH/BatterySignal.bin $SYS_LITE_PATH/BatterySignal.bin
chmod +x $SYS_LITE_PATH/BatterySignal.bin
	
mntroot rw
mv $USER_LITE_PATH/Charging.bin $SYS_LITE_PATH/Charging.bin
chmod +x $SYS_LITE_PATH/Charging.bin
	
mntroot rw
mv $USER_LITE_PATH/notCharging.bin $SYS_LITE_PATH/notCharging.bin
chmod +x $SYS_LITE_PATH/notCharging.bin	
	
mntroot rw
mv $USER_LITE_PATH/PowerState.bin $SYS_LITE_PATH/PowerState.bin
chmod +x $SYS_LITE_PATH/PowerState.bin
	
mntroot rw
mv $USER_LITE_PATH/suspending.bin $SYS_LITE_PATH/suspending.bin
chmod +x $SYS_LITE_PATH/suspending.bin
	
mntroot rw
mv $USER_LITE_PATH/lib $SYS_LITE_PATH/
				
mntroot rw
chmod +x $USER_LITE_PATH/installlib.sh
$USER_LITE_PATH/installlib.sh
rm $USER_LITE_PATH/installlib.sh
sync
				
#TODO: 假如用户放太多垃圾在拷贝的子目录，会让系统区空间
mntroot rw
#copy all pic to system
mkdir -p $SYS_LITE_PATH/res
cp $USER_LITE_PATH/res/* 			$SYS_LITE_PATH/res
rm $USER_LITE_PATH/res/*

mkdir -p $SYS_LITE_PATH/res/fontconfig
cp -r $USER_LITE_PATH/res/fontconfig/* $SYS_LITE_PATH/res/fontconfig
rm -rf $USER_LITE_PATH/res/fontconfig/
sync

mkdir -p $SYS_LITE_PATH/res/py
cp -r $USER_LITE_PATH/res/py/* $SYS_LITE_PATH/res/py
rm -rf $USER_LITE_PATH/res/py/
sync
	
mkdir -p $SYS_LITE_PATH/res/sysfonts
cp -r  $USER_LITE_PATH/res/sysfonts/* $SYS_LITE_PATH/res/sysfonts
rm -rf $USER_LITE_PATH/res/sysfonts/
sync
	
mkdir -p $SYS_LITE_PATH/res/CssAliases
cp $USER_LITE_PATH/res/CssAliases/Aliases $SYS_LITE_PATH/res/CssAliases
rm -rf $USER_LITE_PATH/res/CssAliases/Aliases
sync
	
mkdir -p $USER_LITE_PATH/res/ScreenSaver
mkdir -p $USER_LITE_PATH/res/UserScreenSaver
mkdir -p $USER_LITE_PATH/res/language 
mkdir -p $USER_LITE_PATH/res/dict
mkdir -p $USER_LITE_PATH/res/userfonts
mkdir -p $USER_LITE_PATH/SQMData
sync

mv $USER_LITE_PATH/res/ScreenSaver/*  $USER_LITE_PATH/res/UserScreenSaver

###本周专用修改屏保
mv $USER_LITE_PATH/res/UserScreenSaver/mickey.jpg  $USER_LITE_PATH/res/ScreenSaver/

##Duokan duocuments
mkdir -p /mnt/us/DK_Documents

rm /mnt/us/DK_System/install/liteinstall.sh
mntroot ro

if test $( pgrep -f "KindleApp" | wc -l) -eq 0  &&  test $( pgrep -f "ebook" | wc -l) -eq 0  
then
	sleep 3
	/etc/rc5.d/S96DK_run  lite &
fi

