#!/bin/sh /etc/rc.common
# Copyright (C) 2008-2010 OpenWrt.org

START=99
EXEC_BIN=/usr/sbin

start() {
	pid=$(ps | grep "cloud-brd" | grep -v grep | awk '{print $1}')
	[ -n "$pid" ] && kill -9 $pid

	local all_in_one=`uci -c /etc/profile.d get profile.@global[0].all_in_one`
	if [ "$all_in_one" == "1" ]; then
		return
	fi

	if [ -f /usr/sbin/factory_mode_check ] ; then
		/usr/sbin/factory_mode_check
		RC=$?
		[ ${RC} -eq 1 ] && return 0
	fi

	. /usr/share/libubox/jshn.sh
	json_load "$(cat /tmp/device-info.json)"
	json_get_var mic_type "mic_type" "gateway"
	if [ "$mic_type" == "ROUTER.FESTA" ]; then
		sed -i 's/omada/festa/g' /etc/cloud_proc/cloud_config.cfg
	fi
	json_load "$(cat /etc/config/controller.conf)"
	json_get_var privacyPolicy "privacyPolicy" 1
	json_get_var allowDataCollection "allowDataCollection" 1
	if [ $privacyPolicy -eq 1 -o $allowDataCollection -eq 1 ];then
		service_start /usr/sbin/cloud-brd -c /etc/cloud_proc/cloud_config.cfg &
		# reset cloud_api in uhttpd
		if ubus list cloud; then
			ubus call cloud reset_cloud_api '{}'
		fi
	fi
}

stop() {
	pid=$(ps | grep "cloud-brd" | grep -v grep | awk '{print $1}')
	[ -n "$pid" ] && kill -9 $pid
}
