#!/bin/sh
#filepath="/home/kk"
filepath="/app/ccuApps"
#/etc/init.d/S90dcron stop

eval `flash get CUSTOMER_HW_KK_OPTION0`	#|cut -d '=' -f 2`;
curr_hostname=`cat /etc/hostname`;

if [ "$CUSTOMER_HW_KK_OPTION0" != "$curr_hostname" ]; then
   	#echo "$CUSTOMER_HW_KK_OPTION0" > /etc/hostname
   	hostname $CUSTOMER_HW_KK_OPTION0
	/etc/init.d/S01syslogd restart
fi

run_Dir="/data/kk"
tsl_Dir="${run_Dir}/tsl"
map_Dir="${run_Dir}/map"
smt_Dir="/etc/smarthome"
#dev_cfg_Dir="${run_Dir}/dev_config_table"

#cd /sbin/
#insmod /sbin/ssd1306-revision.ko
#/sbin/oled >/dev/null 2>&1 &
#sleep 1

if [ ! -d "$run_Dir" ]; then
	mkdir ${run_Dir}
fi

if [ ! -d "$tsl_Dir" ]; then
	cp -rf $filepath/tsl ${run_Dir}/
else
	if [ ! -f "${run_Dir}/tsl/version" ]; then
		cp -rf $filepath/tsl ${run_Dir}/
	else	
		defaultVer=`cat ${filepath}/tsl/version`
		curVer=`cat ${run_Dir}/tsl/version`
		if [ $defaultVer != $curVer ]; then
			cp -rf $filepath/tsl ${run_Dir}/
		fi
	fi
fi
#if [ ! -d "$map_Dir" ]; then
cp -rf $filepath/map ${run_Dir}/
#fi

export LD_LIBRARY_PATH=$filepath/lib

$filepath/bin/kk_midware &
sleep 1
$filepath/bin/kk_lan &
sleep 2
$filepath/bin/kk_cloud &
sleep 5
$filepath/bin/smarthome_z3gw_8197 -r0 -l3 --device-def=$filepath/config/kk_device_def.json --db=/data/kk/kk_z3gw.db --thingmodel-path=/data/kk/tsl/ --uart=/dev/ttyS2 &
#sleep 1
#if [ ! -f /etc/cron.d/ccu ]; then
#	echo "*/1 * * * *   /app/ccuApps/script/process_check.sh" > /etc/cron.d/ccu || { echo "ERROR:add ccu crontab rule failed."; exit 1;}
#fi
#/etc/init.d/S90dcron restart
#ntpd -n -q -d -p stdtime.gov.hk &
exit 0