#!/bin/sh

. /etc/functions.sh
. /etc/apply_mapping

CGI_ERROR_CODE="/etc/cgi_error_code"

if   [ -n "$1" ]; then
    changed_conf=$1
else
    changed_conf=`uci fchanges`
	changed_conf=`echo $changed_conf`	#remove the initiated 'blank' returned by `uci fchanges`
    [ -z "$changed_conf" ] && {
		echo "config_apply: No argument or changed_conf input, exit" >/dev/console
		return
	}
fi
start_time=$(cat /proc/uptime | awk 'FS="[.]+" {print $1}')
echo "config_apply: Enter, argument=$1, changed_conf=$changed_conf" >/dev/console

ha_enable=`uci get ucarp_mode.general.status`
ha_method=`uci get ucarp_mode.general.method`
config_role=`uci get ucarp_mode.general.config_role`
[ "$ha_enable" = "enable" -a "$ha_method" = "HS" -a "$config_role" = "primary" ] && {
    revision=`tail -n 1 /tmp/ucarp/revision`
    revision=$(($revision+1))
    tar -cz -f "/tmp/ucarp/$revision.tar.gz" /tmp/.uci/* 2> /dev/null
    sed -i '3,$d' /tmp/ucarp/revision
    echo "$revision" >> /tmp/ucarp/revision
	echo "config_apply: HA primary router:update a new revision($revision)" >/dev/console
}


need_second_update="0"
####Disable second update mechanism
#echo "$changed_conf" | grep "object" 2>&- >&-
#if  [ "$?" = "0" ]; then
#   need_second_update="1"
#fi

if [ "$need_second_update" = "1" ]; then
	#echo "config_apply: changed_conf=$changed_conf case:need_second_update=true =>execute related scripts" >/dev/console
    for conf in $changed_conf; do
        eval \$$conf >/dev/null 2>&1
    done
    eval $ip_filter_set_rule
    eval $app_filter >/dev/null 2>&1
    eval $url_filter >/dev/null 2>&1
    eval $qq_filter >/dev/null 2>&1
    /etc/init.d/clear_obj apply 2>&- >&-
else
    for conf in $changed_conf; do
		#echo "config_apply($$): changed_conf=$changed_conf case:need_second_update=false =>execute script" >/dev/console
        eval \$$conf >/dev/null 2>&1
    done
fi

ret="$?"

#pid=$(ps -ef | awk '/evcpe/ {print $1}')

#if [ -n "$pid" ];then
#kill -s 1 $pid
#sleep 1
#fi
status=$(uci get cwmp.tr069.status)
if [ "$status" = "enable" ];then 
/etc/init.d/cwmp restart
fi

#echo "config_apply: ($changed_conf)after executing script, it return code=$ret" >/dev/console
if [ -n "$ret" ] ;then
	#lookup cgi_error_code table
	if [ -f "$CGI_ERROR_CODE" ] ;then
		error_code=`grep "^$changed_conf@$ret=" $CGI_ERROR_CODE`
		if [ "$?" = "0" ] ;then
			error_code=`echo $error_code |awk -F"=" '{print $2}'`
			#echo "config_apply: retrieve the error code:$error_code, send this code to CGI" >/dev/console
		else
			#echo "config_apply: the error code($ret) not found in table, use original return value" >/dev/console
			error_code=$ret
		fi
	else
		#echo "config_apply: error code table is not existed, use original return value" >/dev/console
		error_code=$ret
	fi
else
	#echo "config_apply: script return a null value, use default value:0" >/dev/console
	error_code=0
fi
finish_time=$(cat /proc/uptime | awk 'FS="[.]+" {print $1}')
echo "config_apply($$): final return code=$error_code, execution time=$(($finish_time - $start_time))" >/dev/console

#Do not echo retrun code if config is fw_cf_license (it's called by /cgi-bin/activate.cgi)
if [ "$changed_conf" = "fw_cf_license" ] ;then
	#echo "config_apply($$): fw_cf_license changes: restart url_filter" >/dev/console
	/etc/init.d/url_filter restart
else
	echo $error_code
fi