#!/bin/sh
# Copyright (C) 2006 OpenWrt.org 
rae_partion=$(part_dev rae)
panic_partion=$(part_dev crashinfo)
RAE_DIR=/tmp/router-analytics
RAE_DATA=$RAE_DIR/ra_data
CONFIG=/bin/config

factory_mode_flag=`/bin/config get factory_mode`
if [ "x$factory_mode_flag" = "x1" ]; then
	exit 0
fi

start() {

	local enable_ra=`/bin/config get agree_collect_TC`

	killall -9  aws-iot
	rm /var/run/aws_iot.pid
	if [ "$enable_ra" == "0" ]; then
		rm /tmp/router-analytics/trigger_data/ -rf
		/usr/sbin/aws-iot -t 86400 -o eventtype4 &
		/bin/config set aws_start_flag=2
		/bin/config commit
	else
		if [ ! -d /tmp/router-analytics/trigger_data ]; then
			mkdir -p /tmp/router-analytics/trigger_data/raw_data/backup
			mkdir -p /tmp/router-analytics/trigger_data/publish_data
		fi
		/usr/sbin/aws-iot &
		/bin/config set aws_start_flag=1
		/bin/config commit
	fi

}

stop() 
{
	killall -9 aws-iot
	rm /var/run/aws_iot.pid
}

restart()
{
	stop
	start
}

erase()
{
	cd $RAE_DIR
	FILES="policy_version rae_version RAE_Policy.json /usr/sbin/aws-iot /usr/sbin/data_collector /usr/sbin/subscribe_publish_sample"
	rm $FILES -rf
	update
}

update()
{
	cd $RAE_DIR
	FILES="policy_version rae_version RAE_Policy.json /usr/sbin/aws-iot /usr/sbin/data_collector /usr/sbin/subscribe_publish_sample"
	tar -zcf rae.tar.gz $FILES
	dd if=/dev/zero bs=1024k count=1 |tr "\000" "\377" >1024kdata
	dd if=rae.tar.gz of=1024kdata
	flash_erase $rae_partion 0 0
	cat 1024kdata > $rae_partion
	rm rae.tar.gz
	rm 1024kdata
	echo 1 >$RAE_DIR/status
}

check()
{
	mkdir -p $RAE_DIR
	echo 0 >$RAE_DIR/status
	local first_bootstrap=`/bin/config get first_bootstrap`
	if [ "$first_bootstrap" != "1" ]; then
		/bin/config set first_bootstrap=1
	        /bin/config commit	
	else
		dd if=$rae_partion of=/tmp/rae.tar.gz
		tar zxfm /tmp/rae.tar.gz -C $RAE_DIR/
		tar_status=$?
		if [ -s $RAE_DIR/rae_version -a -s $RAE_DIR/policy_version -a -s $RAE_DIR/usr/sbin/aws-iot -a -s $RAE_DIR/usr/sbin/data_collector -a -s $RAE_DIR/usr/sbin/subscribe_publish_sample -a -s $RAE_DIR/RAE_Policy.json ];then
			mv $RAE_DIR/usr/sbin/* /usr/sbin/
			echo 1 >$RAE_DIR/status
			rm /tmp/rae.tar.gz -rf
			rm $RAE_DIR/usr -rf
		fi
	fi
}

partition_init()
{
	[ ! -d $RAE_DIR ] && mkdir -p $RAE_DIR
	mkdir -p /data/dni/ra_data
	ln -s /data/dni/ra_data $RAE_DATA
	[ ! -d $RAE_DATA/installation ] && mkdir -p $RAE_DATA/installation
	#Create folder for installation event.
}

boot()
{
	if [ "x$(/bin/config get RA_stage)" = "x" ];then
	/bin/config set RA_stage=prod
	/bin/config commit
	fi

	#As NTGR Analytic_opt-in_opt-out spec:
	#There's a default value NVRAM RA_enable, WW, GR, PR SKU default is 0 not send RA, other SKU default is 1, will send RA
	#After user opt-in/opt-out analytic, after firmware upgrade/downgrade, the setting should be the same
	region=`artmtd -r region |grep REGION |awk -F " " '{printf $2}'`
	if [ "x$(/bin/config get RA_enable)" = "x" ];then
		if [ "$region" = "WW" -o "$region" = "GR" -o "$region" = "PR" ];then
			/bin/config set agree_collect_TC=0
			/bin/config set RA_enable=0
		else
			/bin/config set agree_collect_TC=1
			/bin/config set RA_enable=1
		fi
	fi

	partition_init
	echo 1 > /tmp/support_ra_installation
	/usr/sbin/ra_session_id
	/usr/sbin/install_agent
	/sbin/install_ping &
	hijack_status=`$CONFIG get dns_hijack`
	install_state=`$CONFIG get installState`
	if [ "$hijack_status" = "1" ];then
		/usr/sbin/ra_installevent  sysReboot
	fi
	if [ "$install_state" = "13" ];then
		/usr/sbin/ra_installevent  fwupdatingsucess
	fi

	/bin/config set internetDisconnDuration=0
	/bin/config set gwDisconnDuration=0
	#if DUT not get ntp time, use firmware release time
	if [ "x$(date | grep 1970)" != "x" ]; then
		local date=`cat /firmware_time`
		local mon=`echo $date |awk '{print $2}'`
		local day=`echo $date |awk '{print $3}'`
		[ "$(echo $mon |wc -L)" = "1" ] && mon="0$mon"
		[ "$(echo $day |wc -L)" = "1" ] && day="0$day"
		local time=`echo $date |awk '{print $4}'`
		local hour=`echo $time |awk  -F ':' '{print $1}'`
		local min=`echo $time |awk -F ':' '{print $2}'`
		local year=`echo $date |awk '{print $6}'`
		case "$mon" in
			Jan) mon="01";;
			Feb) mon="02";;
			Mar) mon="03";;
			Apr) mon="04";;
			May) mon="05";;
			Jun) mon="06";;
			Jul) mon="07";;
			Aug) mon="08";;
			Sep) mon="09";;
			Oct) mon="10";;
			Nov) mon="11";;
			Dec) mon="12";;
		esac
		#date $mon$day$hour$min$year
	fi

	dd if=$panic_partion of=/tmp/ra_crashlog bs=1 skip=131072 count=16384
	/usr/bin/strings /tmp/ra_crashlog > /tmp/crash_aws_log
	rm /tmp/ra_crashlog

	if [ -f /proc/reboot_reason ];then
		reboot_reason=`cat /proc/reboot_reason`
	else
		reboot_reason=0
	fi

	#for net-cgi set
	[ "$(/bin/config get lastRebootReason)" = "4" ] && reboot_reason=0x10
	[ "$(/bin/config get lastRebootReason)" = "5" ] && reboot_reason=0x20
	[ "$(/bin/config get lastRebootReason)" = "6" ] && reboot_reason=0x40
	[ "$(/bin/config get lastRebootReason)" = "7" ] && reboot_reason=0x80

	/bin/config set lastRebootReason=0

	#reboot_reason & (0x1 << 0): unknow reboot
	#reboot_reason & (0x1 << 1): Kernel Crash						echo 0x01 >/proc/reboot_reason					0x2
	#reboot_reason & (0x1 << 2): Reboot via Reset button			echo 0x02 >/proc/reboot_reason					0x4
	#reboot_reason & (0x1 << 3): Factory Reset via Reset button 	echo 0x03 >/proc/reboot_reason					0x8
	#reboot_reason & (0x1 << 4): Factory Reset via GUI				echo 0x04 >/proc/reboot_reason					0x10
	#reboot_reason & (0x1 << 5): GUI Reboot							echo 0x05 >/proc/reboot_reason					0x20
	#reboot_reason & (0x1 << 6): Firmware Upgrade					echo 0x06 >/proc/reboot_reason					0x40
	#reboot_reason & (0x1 << 7): Reboot due to Config Changes	  	echo 0x07 >/proc/reboot_reason					0x80
	#0: Unknown
	#1: Kernel Crash
	#2: Reboot via Reset button
	#3: Factory Reset via Reset button
	#4: Factory Reset via GUI
	#5: GUI Reboot
	#6: Firmware Upgrade
	#7: Reboot due to Config Changes

	case "$reboot_reason" in
		"0x2")
			echo 1 >/tmp/reboot_reason
			;;
		"0x4")
			echo 2 >/tmp/reboot_reason
			;;
		"0x8")
			echo 3 >/tmp/reboot_reason
			;;
		"0x10")
			echo 4 >/tmp/reboot_reason
			;;
		"0x20")
			echo 5 >/tmp/reboot_reason
			;;
		"0x40")
			echo 6 >/tmp/reboot_reason
			;;
		"0x80")
			echo 7 >/tmp/reboot_reason
			;;
		"0x0")
			echo 0 >/tmp/reboot_reason
			;;
		*)
			echo 0 >/tmp/reboot_reason
			;;
	esac

	/bin/config set time_crash=`date +%s`

	check
	 /bin/config set cpu_flag=1
	 /bin/config set memory_flag=1
	 /bin/config set eventtype=0
	 /bin/config commit
	 /sbin/icqm &

	 if [ "$(cat $RAE_DIR/status)" = "1" ]; then
			start
	 fi

	# NTGR request that every reboot should check for update
	/usr/sbin/ra_check
}

cpu_test()
{
	for in in `seq $1`
	do 
		echo -ne "i=0;
		while true
		do 
			i=i+1;
		done" | /bin/sh &
		
	done
}

memory_test()
{
	if [ "$1" = 1 ]; then
		mkdir /tmp/memory
		mount -t tmpfs -o size=280M tmpfs /tmp/memory
		dd if=/dev/zero of=/tmp/memory/block
	else
		rm /tmp/memory/block
		umount /tmp/memory
		rmdir /tmp/memory
	fi
}

disable()
{
	killall -9 aws-iot
	rm /var/run/aws_iot.pid
	killall -9 ra_check
	rm /var/run/ra_check.pid
	killall -9 icqm
	rm /var/run/icqm_check.pid
}

install_count_reset()
{
	echo 0 >$RAE_DATA/installation_count
}

install_count_add()
{
	[ ! -f $RAE_DATA/installation_count ] && echo 0 >$RAE_DATA/installation_count
	count=`cat $RAE_DATA/installation_count`
	if [ $count -lt 32 ];then
		count=$(($count+1))
		echo $count > $RAE_DATA/installation_count
	fi
	rm $RAE_DATA/duration
	rm $RAE_DATA/eventId
	rm $RAE_DATA/installation -rf
}

case "$1" in 
	"boot")
		if [ "x$2" != "x" ]; then
			restart
		else
			boot
		fi
	;;
	"start")
		start
	;;
	"stop")
		stop
	;;
	"disable")
		disable
	;;
	"restart")
		restart
	;;
	"update")
		update
	;;
	"check")
		check
	;;
	"cpu")
		cpu_test  $2
	;;
	"memory")
		memory_test $2
	;;
	"reset")
		install_count_reset  ##only run when pot clean or ST SQA do test
	;;
	"default")      # run when default reset
		erase
		install_count_add
		rm $RAE_DATA/session_id
	;;
	*)
		echo "Unknow command" > /dev/console
		echo "Usage: $0 boot|start|stop|restart|update|disable|reset|default" > /dev/console
	;;
esac
