#!/bin/sh /etc/rc.common

START=12

start() {
        CFG_FILE="/tmp/buildroot_cfg.ini"
        try_cnt=0
        # wait for tp_manage start
        while [ ! -f $CFG_FILE ]; do
            echo "ispfile is not ready, wait..."
            sleep 1
            let try_cnt++
            if [ $try_cnt == 5 ]
            then
                break
            fi
        done

        eval msg_alarm_enabled=$(uci show msg_alarm.chn1_msg_alarm_info.enabled | grep -o 'on' | wc -l)

        # if separate_light_support == 1, means support separate light
        eval separate_light_support=$(uci show function.module_spec.msg_alarm_separate_list | grep -o 'light' | wc -l)

        # if separate_sound_support == 1, means support separate sound
        eval separate_sound_support=$(uci show function.module_spec.msg_alarm_separate_list | grep -o 'sound' | wc -l)

	local sound_light_bitmap=$(uci show msg_alarm.chn1_msg_alarm_info.alarm_mode_bitmap)
	local sound_light_bitmap_num=${sound_light_bitmap:48}
        local light_alarm_string=$(uci show | grep light_alarm_enabled)
        local light_alarm_arr=$(echo $light_alarm_string | tr " ")
        local sound_alarm_string=$(uci show | grep sound_alarm_enabled)
        local sound_alarm_arr=$(echo $sound_alarm_string | tr " ")

	local sound_light_bitmap_exist=$(uci show msg_alarm | grep -o "alarm_mode_bitmap" | wc -l)
	local sound_alarm_enable=0
	local light_alarm_enable=0

	if [ "$sound_light_bitmap_num" == "3" ]; then
	    sound_alarm_enable=1
	    light_alarm_enable=1
	elif [ "$sound_light_bitmap_num" == "2" ]; then
	    light_alarm_enable=1
	elif [ "$sound_light_bitmap_num" == "1" ]; then
	    sound_alarm_enable=1
	fi

	if [ "$sound_light_bitmap_exist" -eq "0" ] || [ "$sound_light_bitmap_num" -eq "0" ]; then
	    exit 0
	fi

        if [ -e /tmp/.update_firmware ]; then

	    if [ "$separate_light_support" -eq "1" ]; then
		if [ "$light_alarm_enable" -eq "1" ]; then
		    for i in $light_alarm_arr;
                    do
                        local cut_str=${i%=*}
                        uci set $cut_str='on'
                        uci commit $cut_str
                    done
		else
		    for i in $light_alarm_arr;
                    do
                        local cut_str=${i%=*}
                        uci set $cut_str='off'
                        uci commit $cut_str
                    done
		fi
	    fi

            # if support separate sound
            if [ "$separate_sound_support" -eq "1" ]; then
		if [ "$sound_alarm_enable" -eq "1" ]; then
                    for i in $sound_alarm_arr;
                    do
                        local cut_str=${i%=*}
                        uci set $cut_str='on'
                        uci commit $cut_str
                    done
		else
		    for i in $sound_alarm_arr;
                    do
                        local cut_str=${i%=*}
                        uci set $cut_str='off'
                        uci commit $cut_str
                    done
		fi
            fi

            # if old uci sound-light switch is on
            if [ "$msg_alarm_enabled" -eq "1" ]; then
                # change msg_alarm.chn1_msg_alarm_info.enabled to off, avoid next upgrade do it again.
                uci set msg_alarm.chn1_msg_alarm_info.enabled=off
		uci set msg_alarm.chn1_msg_alarm_info.sound_alarm_enabled=on
		uci set msg_alarm.chn1_msg_alarm_info.light_alarm_enabled=on
	    else
		if [ "$sound_light_bitmap_num" != "0" ]; then
		    uci set msg_alarm.chn1_msg_alarm_info.sound_alarm_enabled=off
                    uci set msg_alarm.chn1_msg_alarm_info.light_alarm_enabled=off
		fi
            fi

	    uci set msg_alarm.chn1_msg_alarm_info.alarm_mode_bitmap=0
	    uci commit msg_alarm.chn1_msg_alarm_info
        fi
}
