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

START=99

dusr_bata() {
	net_on=$(uci get nettask.main.network)
	word_on=$(uci get nettask.main.word)
	button_on=$(uci get nettask.main.button)
	open_on=$(uci get nettask.main.open)
	timi_on=$(uci get nettask.main.timi)
	fen_u=$(uci get nettask.main.minute)
	shi_u=$(uci get nettask.main.shi)
	ri_u=$(uci get nettask.main.day)
	yue_u=$(uci get nettask.main.month)
	zhou_u=$(uci get nettask.main.week)

}

start() {
	dusr_bata
    if [ "$net_on" -eq 1 ]; then
		pgrep -f /etc/nettask/ping.sh | xargs kill -9 >/dev/null 2>&1
		sh /etc/nettask/ping.sh &
	else
		pgrep -f /etc/nettask/ping.sh | xargs kill -9 >/dev/null 2>&1
	fi

    if [ "$word_on" -eq 1 ]; then
		pgrep -f /etc/nettask/word.sh | xargs kill -9 >/dev/null 2>&1
		sh /etc/nettask/word.sh &
	else
		pgrep -f /etc/nettask/word.sh | xargs kill -9 >/dev/null 2>&1
	fi

    if [ "$timi_on" -eq 1 ]; then
		[ -s /etc/crontabs/root ] || flag=1 && touch /etc/crontabs/root #若为空文件，需重启定时服务
		if grep -q "brukamen" /etc/crontabs/root; then
			sed -i '/brukamen/d' /etc/crontabs/root #如果存在旧任务，就要先删掉原来的
			echo "${fen_u} ${shi_u} ${ri_u} ${yue_u} ${zhou_u} sh /etc/nettask/timing.sh & #brukamen" >>/etc/crontabs/root  #需保留其他定时任务，故使用追加
		else
			echo "${fen_u} ${shi_u} ${ri_u} ${yue_u} ${zhou_u} sh /etc/nettask/timing.sh & #brukamen" >>/etc/crontabs/root
		fi
		[ "$flag" -eq 1 ] && /etc/init.d/cron restart
	else
		sed -i '/brukamen/d' /etc/crontabs/root
	fi

    if [ "$button_on" -eq 1 ]; then
        if  [ "$open_on" -eq 1 ]; then
            :
        else
            [ -x /etc/nettask/bucc ] || chmod +x /etc/nettask/bucc
            mv -f /etc/rc.button/reset /etc/rc.button/reset_bak  #原文件备份
            ln -s /etc/nettask/bucc /etc/rc.button/reset
            uci set nettask.main.open="1" && uci commit nettask
        fi
	else
		[ -f /etc/rc.button/reset_bak ] && mv -f /etc/rc.button/reset_bak /etc/rc.button/reset
		[ "$open_on" -eq 1 ] && uci set nettask.main.open="0" && uci commit nettask
	fi
}

stop() {
	echo "Error"
}

reload() {
	stop
	start
}
