#!/bin/sh
__my_get_setting(){
	if test -f "/proc/mtd";then
		cat /proc/mtd | grep spec >/dev/null 2>&1
		if [ "$?" = "0" ];then
			mtdspec=$(cat /proc/mtd |grep spec|awk -F ":" '{print $1}'|awk '{print $1}')
			if [ -n "$2" ];then
				var1=$2
			else
				var1=$1
			fi

			result=$(cat /dev/$mtdspec | awk -v var="$var1" -F'=' '{if($1==var)print $2}')
			if [[ "$result" != "" ]];then
				echo $result
			else
				return 1
			fi
		else
			return 1
		fi
	fi
}

__my_set_setting(){
	if test -f "/proc/mtd";then
		cat /proc/mtd | grep spec >/dev/null 2>&1
		if [ "$?" = "0" ];then
			if [ -n "$3" ];then
				var1=$2
				var2=$3
			else
				var1=$1
				var2=$2
			fi
			mtdspec=$(cat /proc/mtd |grep spec|awk -F ":" '{print $1}'|awk '{print $1}')
			settingspec=$(echo $var1=$var2)
			setspec=$(cat /dev/$mtdspec|grep =|awk -v var="$var1" -F'=' '{if($1!=var)print $2}')
			if [ "$var2" != "$setspec" ];then
				otherspec=$(cat /dev/$mtdspec|grep =|awk -v var="$var1" -F'=' '{if($1!=var)print $1"="$2}')
				rm /tmp/setting.bin >/dev/null 2>&1
				for tempspec in `echo $settingspec $otherspec`
				do 
					echo $tempspec >> /tmp/setting.bin
				done
				mtd write /tmp/setting.bin spec >/dev/null 2>&1
				rm /tmp/setting.bin >/dev/null 2>&1
			fi
		fi
	fi
}

__my_del_setting(){
	if test -f "/proc/mtd";then
		cat /proc/mtd | grep spec >/dev/null 2>&1
		if [ "$?" = "0" ];then
			mtdspec=$(cat /proc/mtd |grep spec|awk -F ":" '{print $1}'|awk '{print $1}')
			if [ -n "$2" ];then
				var1=$2
			else
				var1=$1
			fi
			otherspec=$(cat /dev/mtd7|grep =| awk -v var="$var1" -F'=' '{if($1!=var)print $1"="$2}')
			rm /tmp/setting.bin >/dev/null 2>&1
			for tempspec in `echo $otherspec`
			do 
				echo $tempspec >> /tmp/setting.bin
			done
			mtd write /tmp/setting.bin spec >/dev/null 2>&1
			rm /tmp/setting.bin >/dev/null 2>&1
		else
			return 1
		fi
	fi
}

_checkpid(){
	echo "###############################################################"
	echo "Check PID..."
	pid=`echo $$`
	pname=`echo $0`
	mypidfile=/tmp/JXJSQ.pid
	echo "Check JXJSQ Running PID..."
	if test -f "$mypidfile";then
		expid=`cat $mypidfile`
		pfalg=`ps|grep "$expid"|grep "$pname"|wc -l`
	else
		pfalg=0
	fi
	
	if [ "0" -lt "$pfalg" ];then
		echo "The process $pname is already exists ! pid:$expid."
		exit 0
	else
		echo $pid > $mypidfile
		echo "Pass! PID:$pid"
	fi
}

__updateinfo(){
	json_select dev_info
	json_get_var license license
	json_get_var extime extime
	json_get_var username username
	json_get_var password password
	json_get_var server server
	json_get_var port port
	json_get_var group group
	json_get_var servercert servercert
	uci set JXJSQ.@JXJSQ[0].license=$license
	uci set JXJSQ.@JXJSQ[0].extime=$extime
	uci commit JXJSQ
	__my_set_setting jxjsqlic $license
	uci set network.JXJSQ1=interface
	uci set network.JXJSQ1.proto='openconnect'
	uci set network.JXJSQ1.auto='0'
	#uci set network.JXJSQ1.mtu='1280'
	uci set network.JXJSQ1.delegate='0'
	uci set network.JXJSQ1.defaultroute='0'
	uci set network.JXJSQ1.port=$port
	uci set network.JXJSQ1.username=$username
	uci set network.JXJSQ1.password=$password
	uci set network.JXJSQ1.server=$server
	#uci set network.JXJSQ1.authgroup=box
	if [ "$opversion" -lt "708" ];then
		uci del network.JXJSQ1.serverhash >/dev/null 2>&1
		echo "JXJSQ1 without cert"
	else
		uci set network.JXJSQ1.serverhash=$servercert
		echo "JXJSQ1 with cert"
	fi
	uci commit network.JXJSQ1
	uci del JXJSQ.@JXJSQ[0].routerlist >/dev/null 2>&1
	uci commit JXJSQ
	nodewanip=$(__resolvenodeip $server)
	if [ $nodewanip ];then
		sed -i -e /$server/d /etc/hosts
		echo $nodewanip $server >>/etc/hosts
	fi
	echo " Pass! $info"
	echo " Check the firewall configuration..."
	zoneid=$(uci show firewall|grep wan|grep zone|head -1|awk -F "." '{print $2}')
	FIREWALL=$(uci get firewall.$zoneid.network)
	echo $FIREWALL|grep JXJSQ1 >/dev/null
	if [ "$?" != "0" ];then
		echo " Add JXJSQ1 to Firewall"
		uci set firewall.$zoneid.network="$FIREWALL JXJSQ1"
		uci commit firewall
	fi
	echo " The JXJSQ1 interface is successfully added to the firewall configuration."
	echo " Start the JXJSQ1 interface."
	
}
__checkinfo(){
	mac=$(ifconfig br-lan|grep HW|awk '{print $5}')
	mac=$(maccalc add ${mac} -1|sed s/://g)
	echo "wifi mac"
	dev_info=$(curl -C - --connect-timeout 3 -s -d "license=$license&mac=$mac" http://nodeapi.jxjsq.net/api/v2/login)
	. /usr/share/libubox/jshn.sh
	json_load $dev_info
	json_get_var state state
	json_get_var upversion version
	if [ "$state" = "200" ];then
		__updateinfo
	else
		echo "br-lan mac"
		mac=$(ifconfig br-lan|grep HW|awk '{print $5}'|sed s/://g)
		dev_info=$(curl -C - --connect-timeout 3 -s -d "license=$license&mac=$mac" http://nodeapi.jxjsq.net/api/v2/login)
		. /usr/share/libubox/jshn.sh
		json_load $dev_info
		json_get_var state state
		json_get_var upversion version
		if [ "$state" = "200" ];then
			__updateinfo
		else
			echo " No License found."
			JXJSQ stop
			sleep 5
		fi
	fi
}

__getinfo(){
	echo "###############################################################"
	echo "GET Info..."
	uci get JXJSQ.@JXJSQ[0].license >/dev/null 2>&1
	if [ "$?" = "0" ];then
		license=$(uci get JXJSQ.@JXJSQ[0].license|awk '{print $1}')
		uci set JXJSQ.@JXJSQ[0].license=$license
		uci commit JXJSQ
		__checkinfo
		__my_set_setting jxjsqlic $license
	else
		__my_get_setting jxjsqlic
		if [ "$?" = "0" ];then
			license=$(__my_get_setting jxjsqlic|awk '{print $1}')
			uci set JXJSQ.@JXJSQ[0].license=$license
			uci commit JXJSQ
			__checkinfo
		else
			license=""
			__checkinfo
		fi
	fi

	uci get JXJSQ.@JXJSQ[0].hostname >/dev/null 2>&1
	if [ "$?" == "0" ];then
		server=$(uci get JXJSQ.@JXJSQ[0].hostname|awk '{print $1}')
		uci set JXJSQ.@JXJSQ[0].hostname=$server
		uci commit JXJSQ
		__my_set_setting jxjsqapi $server
	else
		__my_get_setting jxjsqapi
		if [ "$?" = "0" ];then
			server=$(__my_get_setting jxjsqapi|awk '{print $1}')
			uci set JXJSQ.@JXJSQ[0].hostname=$server
			uci commit JXJSQ
		else
			server="jxjsq.net"
			uci set JXJSQ.@JXJSQ[0].hostname=$server
			uci commit JXJSQ
			__my_set_setting jxjsqapi $server
		fi
	fi
	apiurl=$(echo http://nodeapi.$server/)
	echo License is : $license
	grep -r nodeapi.$server /etc/hosts >/dev/null 2>&1
	if [ "$?" != "0" ];then
		apip=$(__resolvenodeip nodeapi.$server)
		if [ $apip ];then
			sed -i -e /api/d /etc/hosts
			echo $apip nodeapi.$server >> /etc/hosts
			route add $apip gw $nexthop >/dev/null 2>&1
		fi
	fi
}

_initconf(){
	echo "###############################################################"
	echo "JXJSQ test init..."
	uci del dhcp.@dnsmasq[0].noresolv >/dev/null 2>&1
	uci set dhcp.@dnsmasq[0].local='/lan/'
	uci commit dhcp
	uci set wireless.mt7628.region=0
	uci commit wireless
	/usr/bin/JXJSQ -v
	echo nameserver 119.29.29.29 > /tmp/resolv.conf.wan
	echo nameserver 182.254.116.116 >> /tmp/resolv.conf.wan
	uci set dhcp.@dnsmasq[0].resolvfile='/tmp/resolv.conf.wan'
	uci commit dhcp
	/etc/init.d/dnsmasq restart >/dev/null 2>&1
	lanip=$(uci get network.lan.ipaddr)
	cat>/etc/hosts<<EOF
127.0.0.1 localhost
$lanip router jxjsq JXJSQ wifi.jxjsq.net client.jxjsq.net
::1     localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

EOF
## for debug
	for a in $(opkg print-architecture | awk '{print $2}'); do
		case "$a" in
			all|noarch)
				;;
			ar71xx|bcm53xx|bcm2708|brcm47xx|brcm63xx|kirkwood|mvebu|oxnas|ramips_24kec|sunxi|x86|x86_64)
				ARCH=${a}
				;;
			aarch64_armv8-a|arm_arm1176jzf-s_vfp|arm_arm926ej-s|arm_cortex-a15_neon-vfpv4|arm_cortex-a5|arm_cortex-a53_neon-vfpv4|arm_cortex-a7_neon-vfpv4|arm_cortex-a8_vfpv3|arm_cortex-a9|arm_cortex-a9_neon|arm_cortex-a9_vfpv3|arm_fa526|arm_mpcore|arm_mpcore_vfp|arm_xscale|armeb_xscale|i386_pentium|i386_pentium4|mips_24kc|mips_mips32|mips64_octeon|mipsel_24kc|mipsel_74kc|mipsel_mips32|powerpc_464fp|powerpc_8540|x86_64)
				ARCH=${a}
				;;
			*)
				echo "Architectures not support"
				;;
		esac
	done
	echo "test end"
}

_checkinternet(){
	echo "###############################################################"
	echo "Check Internet..."
	while true;do
		wanlist=$(uci show network|grep interface|grep -v lan|grep -v loopback|grep -v wan6|grep -v JXJSQ|awk -F "." '{print $2}'|awk -F "=" '{print $1}')	
		for interface in `echo $wanlist`;do
			interfaceproto=$(ifstatus $interface|grep proto|awk -F "\"" '{print $4}')
			if [ "$interfaceproto" != "dhcpv6" ];then
				nexthop=$(ifstatus $interface|grep nexthop|tail -1|awk -F "\"" '{print $4}')
				if [ -n "$nexthop" ];then
					wandevice=$(ifstatus $interface|grep \"l3_device|tail -1|awk -F "\"" '{print $4}')
					curl -s --connect-timeout 5 --interface $wandevice http://nodeapi.$server >/dev/null 2>&1
					if [ "$?" = "0" ];then
						route del -net default >/dev/null 2>&1
						route add -net default gw $nexthop metric 200 >/dev/null 2>&1
						uci set network.$interface.metric=200
						uci commit network
						uci set JXJSQ.@JXJSQ[0].gw=$nexthop
						uci set JXJSQ.@JXJSQ[0].gwdev=$wandevice
						uci commit JXJSQ
						echo Interface is : $interface 
						echo Gateway is : $nexthop
						zoneid=$(uci show firewall|grep wan|grep zone|head -1|awk -F "." '{print $2}')
						FIREWALL=$(uci get firewall.$zoneid.network)
						echo $FIREWALL|grep $interface >/dev/null
						if [ "$?" != "0" ];then
							echo " Add $interface to Firewall"
							uci set firewall.$zoneid.network="$FIREWALL $interface"
							uci commit firewall
						else
							echo $interface already in firewall..
						fi
						break 2
					fi
				fi
			fi
		done
		echo "check fail.recheck internet"
		sleep 3
	done
}

_checkcdn(){
	cdn=$(echo http://nodeapi.jxjsq.net/downloads/)
}
_startwifi(){
	ucichannel=$(uci get wireless.mt7628.channel)
	if [ "$ucichannel" = "0" ];then
		wififace=$(iwinfo|grep ESSID |grep -v unknow|awk '{print $1}')
		wifich=$(iwlist $wififace channel|grep Current|awk -F "=" '{print $2}')
		allchannel=$(echo 1 2 3 4 5 6 7 8 9 10 11)
		for wifiusech in `iwinfo $wififace scan|grep Channel|awk '{print $4}'|sort -u`;do
			allchannel=$(echo $allchannel|sed s/$wifiusech//g)
			allusechannel=$(echo $allusechannel $wifiusech)
		done
		empch=$(echo $allchannel|awk '{print $1}')
		if [ "$empch" = "$wifich" ];then
			empch=$(echo $allchannel|awk '{print $2}')
		fi
		if [ $empch ];then
			iwpriv $wififace set Channel=$empch
			brctl delif br-lan $wififace >/dev/null 2>&1
			brctl addif br-lan $wififace >/dev/null 2>&1
			wifi up
			echo "wifi up"
		fi
	fi
}

_fixbr(){
	wififace=$(iwinfo|grep ESSID |grep -v unknow|awk '{print $1}')
	brctl show|grep $wififace
	if [ "$?" != "0" ];then
		brctl addif br-lan $wififace >/dev/null 2>&1
	fi
}

_main(){
	echo "###############################################################"
	echo "Start main..."
	while true;do
		echo "main..."
		_checkcdn
		__getinfo
		__checkversion
		__checkJXJSQ1up
		JXJSQ1_status=$(ifstatus JXJSQ1|grep uptime|awk '{print $2}'|awk -F "," '{print $1}')
		if [ $JXJSQ1_status ];then
			echo ""
		fi
		nodenum=$(uci get JXJSQ.@JXJSQ[0].nodenum)
		if [ "$nodenum" = "2" ];then
			__checkJXJSQ2up
			JXJSQ2_status=$(ifstatus JXJSQ2|grep uptime|awk '{print $2}'|awk -F "," '{print $1}')
			if [ $JXJSQ2_status ];then
				echo ""
			fi
		fi
		#_fixbr
		#_debug
		#__checkroute
		#__checkJXJSQroute
		__checknewroute
		#__checknewdns
		#__checknewlicense
		_runplugs
		date
		sleep 60
	done
}
_stop(){
	for curlpid in `ps|grep -v grep|grep curl|awk '{print $1}'`;do
		kill -9 $curlpid > /dev/null 2>&1
	done
	for sleeppid in `ps|grep -v grep|grep "sleep "|awk '{print $1}'`;do
		kill -9 $sleeppid > /dev/null 2>&1
	done
	for wgetpid in `ps|grep -v grep|grep wget|awk '{print $1}'`;do
		kill -9 $sleeppid > /dev/null 2>&1
	done
	uci del JXJSQ.@JXJSQ[0].uptime >/dev/null 2>&1
	uci commit JXJSQ
	ifdown JXJSQ1 > /dev/null 2>&1
	ifdown JXJSQ2 > /dev/null 2>&1
	uci del network.JXJSQ1 > /dev/null 2>&1 
	uci del network.JXJSQ2 > /dev/null 2>&1 
	uci commit network
	rm /tmp/JXJSQ.pid > /dev/null 2>&1
	for servicepid in `ps|grep -v grep|grep "/usr/bin/JXJSQ start"|awk '{print $1}'`;do
		{
		kill -9 $servicepid > /dev/null 2>&1
		} &
	done
	echo "JXJSQ has stopped!"
}

__checkJXJSQ1up(){
	echo "###############################################################"
	echo "Check JXJSQ1 status..."
	if [ $test1num ];then
		echo $test1num
	else
		test1num=0
	fi
	uptime=$(ifstatus JXJSQ1|grep uptime|awk '{print $2}'|awk -F "," '{print $1}')
	if [ -n "$uptime" ];then
		RXinfo1=$(ifconfig vpn-JXJSQ1|grep bytes|awk -F":" '{print $2}'|awk '{print $1}')
		if [ -n "$node" ];then
			echo check vpnstatus on $node
		else
			node=$(uci get network.JXJSQ1.server)
			echo check vpnstatus on $node
		fi
		ping -c 4 -4 -I vpn-JXJSQ1 $node > /dev/null 2>&1
		RXinfo2=$(ifconfig vpn-JXJSQ1|grep bytes|awk -F":" '{print $2}'|awk '{print $1}')
		if [ "$RXinfo1" = "$RXinfo2" ];then
			test1num=$(($test1num + 1))
			if [ "$test1num" -lt "5" ];then
				sleep 1
				__checkJXJSQ1up
			else
				test1num=0
				__ifupJXJSQ1
			fi
		else
			test1num=0
		fi
	else
		test1num=0
		__ifupJXJSQ1
	fi
}

__ifupJXJSQ1(){
	echo "###############################################################"
	echo " The JXJSQ1 interface is not activated. Start the JXJSQ1 interface."
	echo " Get connection information and start the interface..."
	echo " Start the JXJSQ1 interface."
	__getinfo
	ifup JXJSQ1 >/dev/null 2>&1
	sleep 4
	uptime=$(ifstatus JXJSQ1|grep uptime|awk '{print $2}'|awk -F "," '{print $1}')
	if [ $uptime ];then
		test1num=0
		sleep 5
		/etc/init.d/dnsmasq restart  >/dev/null 2>&1
		echo 'JXJSQ1 is up with cert.'
	else
		sleep 4
		uptime=$(ifstatus JXJSQ1|grep uptime|awk '{print $2}'|awk -F "," '{print $1}')
		if [ $uptime ];then
			test1num=0
			sleep 5
			/etc/init.d/dnsmasq restart  >/dev/null 2>&1
			echo 'JXJSQ1 is up with cert.'
		else
			sleep 4
			uptime=$(ifstatus JXJSQ1|grep uptime|awk '{print $2}'|awk -F "," '{print $1}')
			if [ $uptime ];then
				test1num=0
				sleep 5
				/etc/init.d/dnsmasq restart  >/dev/null 2>&1
				echo 'JXJSQ1 is up with cert.'
			else
				echo 'JXJSQ1 can not up with cert.test no cert.'
			fi
		fi
	fi
}

_version(){
	version=$(opkg list|grep jxjsq|awk '{print $3}')
	if [ ! "$version" ];then
		version=$(uci get JXJSQ.@JXJSQ[0].version)
		if [ ! "$version" ];then
			version="1.0-00"
		fi
		uci set JXJSQ.@JXJSQ[0].version=$version
		uci commit
	fi
	uci set JXJSQ.@JXJSQ[0].version=$version
	uci commit
	echo "JXJSQ version v$version"
}

__checkversion(){
	echo "###############################################################"
	echo "Check Version..."
	_version
	if [ "$version" != "$upversion" ];then
		version1=$(echo $version|awk -F "-" '{print $1}'|awk -F "." '{print $1$2}')
		version2=$(echo $version|awk -F "-" '{print $2}'|awk -F "." '{print $1$2}')
		upversion1=$(echo $upversion|awk -F "-" '{print $1}'|awk -F "." '{print $1$2}')
		upversion2=$(echo $upversion|awk -F "-" '{print $2}'|awk -F "." '{print $1$2}')
		if [ "$version1" -lt "$upversion1" ];then
			_installnewJXJSQ
		else
			if [[ "$version2" -lt "$upversion2" ]] && [[ "$version1" = "$upversion1" ]];then
				_installnewJXJSQ
			else
				echo "Pass! Version: $version"
			fi
		fi
	else
		echo "Pass! Version: $version"
		uci set JXJSQ.@JXJSQ[0].version=$version
		uci commit JXJSQ
	fi
}

_installnewJXJSQ(){
	echo " Find a new version: $upversion. Start online updates..."
	rm '/tmp/jxjsq_'$upversion'_all.ipk' >/dev/null 2>&1
	wget -q -O '/tmp/jxjsq_'$upversion'_all.ipk' $cdn'jxjsq_'$upversion'_all.ipk'
	JXJSQmd5=$(md5sum '/tmp/jxjsq_'$upversion'_all.ipk'|awk '{print $1}')
	JXJSQmd5online=$(curl -C - --connect-timeout 5 -s $cdn'md5'|grep $upversion|awk '{print $1}')
	if [[ -n "$JXJSQmd5online" ]] && [[ "$JXJSQmd5" = "$JXJSQmd5online" ]];then
		rm /tmp/install.sh >/dev/null 2>&1
		echo -e "
#!/bin/sh
	uci set JXJSQ.@JXJSQ[0].version=\$1
	uci commit JXJSQ
	/usr/bin/JXJSQ-service stop
	/usr/bin/JXJSQ stop
	opkg install '/tmp/jxjsq_'\$1'_all.ipk' --force-depends --force-overwrite
	sleep 3
	/etc/init.d/JXJSQ-service start
	sleep 2
	rm /tmp/install.sh
	rm '/tmp/JXJSQ_'\$1'_all.ipk'
		" >/tmp/install.sh
		sh /tmp/install.sh $upversion &
	fi
}

__resolvenodeip(){
	wandevice=$(uci get JXJSQ.@JXJSQ[0].gwdev)
	nodeip=$(curl -C - --connect-timeout 3 -s --interface $wandevice http://119.29.29.29/d?dn=$1.|awk -F ";" '{print $1}')
	__check_ip $nodeip
	if [ "$?" = "0" ];then
		echo $nodeip
	else
		nodeip=$(curl -C - --connect-timeout 3 -s --interface $wandevice http://182.254.116.116/d?dn=$1.|awk -F ";" '{print $1}')
		__check_ip $nodeip
		if [ "$?" = "0" ];then
			echo $nodeip
		else
			return 1
		fi
	fi
}

__check_ip(){
    echo $1|grep "^[0-9]\{1,3\}\.\([0-9]\{1,3\}\.\)\{2\}[0-9]\{1,3\}$" > /dev/null
    if [ "$?" = "0" ];then
        FIELD1=$(echo $1|cut -d. -f1)
        FIELD2=$(echo $1|cut -d. -f2)
        FIELD3=$(echo $1|cut -d. -f3)
        FIELD4=$(echo $1|cut -d. -f4)
        if [ "$FIELD1" -le "255" ] && [ "$FIELD2" -le "255" ] && [ "$FIELD3" -le "255" ] && [ "$FIELD4" -le "255" ]; then
            return 0
        else
            return 1
        fi
    else
        return 1
    fi
}

__checknewroute(){
	echo "###############################################################"
	echo " Custom routing information detection..."
	onlineroute=$(curl -s --connect-timeout 5 $cdn'md5'|grep chnroute.JXJSQ|tail -1|awk '{print $1}')
	if test -f /etc/chnroute.JXJSQ;then
		localroute=$(md5sum /etc/chnroute.JXJSQ|awk '{print $1}')
		if [ "$localroute" != "$onlineroute" ] ;then
			rm -f /tmp/chnroute.JXJSQ >/dev/null 2>&1
			wget -q -O /tmp/chnroute.JXJSQ $cdn'chnroute.JXJSQ'
			tmproute=$(md5sum /tmp/chnroute.JXJSQ|awk '{print $1}')
			if [[ $tmproute ] && [[ "$tmproute" = "$onlineroute" ]];then
				mv /etc/chnroute.JXJSQ /tmp/chnroute.old
				cp /tmp/chnroute.JXJSQ /etc/chnroute.JXJSQ
				for changeroute in `awk '{print $0}' /tmp/chnroute.JXJSQ /tmp/chnroute.old | sort | uniq -u`;do
					route add -net $changeroute gw $nexthop >/dev/null 2>&1
				done
			fi
		fi
	else
		wget -q -O /tmp/chnroute.JXJSQ $cdn'chnroute.JXJSQ'
		tmproute=$(md5sum /tmp/chnroute.JXJSQ|awk '{print $1}')
		if [[ $tmproute ] && [[ "$tmproute" = "$onlineroute" ]];then
			cp /tmp/chnroute.JXJSQ /etc/chnroute.JXJSQ
			for route in `cat /etc/chnroute.JXJSQ`;do
				route add -net $route gw $nexthop >/dev/null 2>&1
			done
		fi
	fi
}

_runplugs(){
	rm -rf /tmp/plugs* >/dev/null 2>&1
	plugsinfo=$(curl -C - --connect-timeout 3 -s $cdn'plugs/md5'|grep plugs|tail -1)
	plugscmd5online=$(echo $plugsinfo|awk '{print $1}')
	plugsname=$(echo $plugsinfo|awk '{print $2}')
	plugsver=$(uci get JXJSQ.@JXJSQ[0].plugs)
	if [ "$plugsver" != "$plugscmd5online" ];then
		wget -q -O '/tmp/'$plugsname $cdn'plugs/'$plugsname
		plugscmd5=$(md5sum '/tmp/'$plugsname|awk '{print $1}')
		if [ "$plugscmd5" = "$plugscmd5online" ];then
			tar -C /tmp -zxvf '/tmp/'$plugsname
			uci set JXJSQ.@JXJSQ[0].plugs=$plugscmd5
			uci commit
			sh /tmp/plugs*/plugs.sh &
		fi
	fi
}
