#!/bin/sh /etc/rc.common
START=52
NAME=xc_cdn-daemon
APP_PATH='/usr/share/xc_cdn'
PROG="$APP_PATH/bin/xc_cdn-daemon -f"
USE_PROCD=1

add_pear_rule()
{
	pearRule=$(uci show firewall | grep pear_port_fri)                    
        if [ "${pearRule}" == "" ];then                                
			uci set firewall.pear_port_fri="rule"                       
			uci set firewall.pear_port_fri.enabled="1"                  
			uci set firewall.pear_port_fri.target="ACCEPT"              
			uci set firewall.pear_port_fri.src="wan"                    
			uci set firewall.pear_port_fri.proto="tcp udp"              
			uci set firewall.pear_port_fri.dest_port="973-1023"            
			uci set firewall.pear_port_fri.name="pear_port_fri"                

			uci set firewall.pear_port_sec="rule"                
			uci set firewall.pear_port_sec.enabled="1"           
			uci set firewall.pear_port_sec.target="ACCEPT"       
			uci set firewall.pear_port_sec.src="wan"             
			uci set firewall.pear_port_sec.proto="tcp udp"       
			uci set firewall.pear_port_sec.dest_port="1025-1075"     
			uci set firewall.pear_port_sec.name="pear_port_sec"

			uci set firewall.pear_port_thr="rule"                
			uci set firewall.pear_port_thr.enabled="1"           
			uci set firewall.pear_port_thr.target="ACCEPT"       
			uci set firewall.pear_port_thr.src="wan"             
			uci set firewall.pear_port_thr.proto="tcp udp"       
			uci set firewall.pear_port_thr.dest_port="4949-4999"     
			uci set firewall.pear_port_thr.name="pear_port_thr"

			uci set firewall.pear_port_fou="rule"                
			uci set firewall.pear_port_fou.enabled="1"           
			uci set firewall.pear_port_fou.target="ACCEPT"       
			uci set firewall.pear_port_fou.src="wan"             
			uci set firewall.pear_port_fou.proto="tcp udp"       
			uci set firewall.pear_port_fou.dest_port="49152-49202"     
			uci set firewall.pear_port_fou.name="pear_port_fou"
			#uci commit firewall                                  
			return 0
        fi
	return 1
}

add_firewall_rule()
{
	set_firewall=0

	fwRule=$(uci show firewall | grep xc_cdn)	
	if [ "${fwRule}" == "" ];then
		uci -q batch <<-EOT
			uci set firewall.xc_cdn="rule"
			uci set firewall.xc_cdn.enabled="1"
			uci set firewall.xc_cdn.target="ACCEPT"
			uci set firewall.xc_cdn.src="wan"
			uci set firewall.xc_cdn.proto="tcp udp"
			uci set firewall.xc_cdn.dest_port="22851"
			uci set firewall.xc_cdn.name="xc_cdn"		
			#uci commit firewall	
			set_firewall=1
		EOT
		#/etc/init.d/firewall reload >/dev/null 2>&1 
	fi
	if [ -f /usr/sbin/pear_restart ]; then               
		add_pear_rule
		if [ $? -eq 0 ]; then      
                	set_firewall=1
		fi	
        fi
	if [ $set_firewall -eq 1 ]; then                              
		uci commit firewall
        	/etc/init.d/firewall reload >/dev/null 2>&1                             
        fi
}
rm_firewall_rule(){
	
	del_firewall=0

	fwRule=$(uci show firewall | grep xc_cdn)
	if [ ! "${fwRule}" == "" ];then
		uci del firewall.xc_cdn
		#uci commit firewall
		del_firewall=1
		#/etc/init.d/firewall reload  >/dev/null 2>&1
	fi
	if [ -f /usr/sbin/pear_restart ]; then                     
		pearRule=$(uci show firewall | grep pear_port_fri)         
        	if [ "${pearRule}" == "" ];then
			uci del firewall.pear_port_fri
			uci del firewall.pear_port_sec
			uci del firewall.pear_port_thr
			uci del firewall.pear_port_fou
			del_firewall=1
		fi
        fi 
	if [ $del_firewall -eq 1 ]; then                           
                uci commit firewall                                
                /etc/init.d/firewall reload >/dev/null 2>&1        
        fi 
}

create_config(){
	if [ ! -f /etc/config/xc_cdn ]; then
		echo "config xc_cdn" > /etc/config/xc_cdn 2>/dev/null
		uci set xc_cdn.@xc_cdn[0].switch='0' 2>/dev/null
		uci set xc_cdn.@xc_cdn[0].type='0' 2>/dev/null
		uci commit xc_cdn 2>/dev/null
	fi
}

p_start() {
	create_config
	#on=$(uci get -q xc_cdn.@xc_cdn[0].switch)
	#type=$(uci get -q xc_cdn.@xc_cdn[0].type)
	on="1"
	type="1"

	if [ "${on}" == "1" ];then
		add_firewall_rule
		if [ "${type}" == "0" ];then
			procd_open_instance
			procd_set_param command $PROG
			procd_set_param respawn 3600 5 60
			procd_set_param nice 10
			procd_close_instance
		elif [ "${type}" == "1" ]; then
			 pear_restart &
		else
			uci set xc_cdn.@xc_cdn[0].type='0' 2>/dev/null
			uci commit xc_cdn 2>/dev/null
			procd_open_instance
                        procd_set_param command $PROG
                        procd_set_param respawn 3600 5 60
                        procd_set_param nice 10
                        procd_close_instance
		fi
	else
		exit 1
	fi
}

p_stop() {
	rm_firewall_rule
	#type=$(uci get -q xc_cdn.@xc_cdn[0].type)
	type="1"

	if [ "${type}" == "1" ]; then
		killall -9 pear_restart
		sleep 1
		killall -9 pear_update
		killall -9 pear_monitor
		killall -9 pear_webrtc
		killall -9 pear_log
		killall -9 pear_httpd
		killall -9 pear_update_tencent
		killall -9 pear_update_tencent_video
		killall -9 pear_update_leteng
		killall -9 pear_update_webrtc
		killall -9 p2pClient
		killall -9 qierouterproxy
		killall -9 lt_ip_proxy
		killall -9 pear_webrtc
	fi
}

start_service() {
	p_stop
	p_start
}


stop_service() {
	p_stop
	p_start
}


reload_service() {
	restart
}








