#!/bin/bash
ipruleclearconf="/etc/.ipruleclear.conf"
keystr=filter
localdeadtime=$(date  +'%Y-%m-%d%H:%M:%S')
while read deadtime runlevel line
do
	if [ -z "$line" ];then
		continue
	fi
        if [ "$runlevel" -eq 2 ];then
                iprulefile="/etc/iprule.high"
        elif [ "$runlevel" -eq 0 ];then
                iprulefile="/etc/iprule.low"
        else
                iprulefile="/etc/iprule.medium"
        fi
       
        if [ ! -f "$iprulefile" ];then
                continue
        fi
        
	echo $localdeadtime
	echo $deadtime
        #echo 02--$runlevel--$iprulefile--$localdeadtime--$deadtime >> /tmp/cron.log
        #echo 02--$line-- >> /tmp/cron.log
        if [[ "$localdeadtime" > "$deadtime" ]];then
        	echo $line
	        line="$(echo ${line}|awk 'BEGIN{MYESCAP="/";NEWESCAP="\\/"} \
	        {if(match($0,MYESCAP) > 0){gsub(MYESCAP,NEWESCAP,$0)}}END{print $0".*"}')"
                busybox sed -ni "/\*$keystr/,/\*.*/{
                /$line/d         
                }p" "$iprulefile"
		runlevel=$(. /etc/init.d/nas/iprule && noontec_getiprulerunlevel)
		if [ "$runlevel" = "high" ];then
			runlevel=2
		elif [ "$runlevel" = "medium" ];then
			runlevel=1
		else
			runlevel=0
		fi
		. /etc/init.d/nas/iprule && noontec_iprulesave $runlevel > /dev/null 2>&1
		if [ $? -ne 0 ];then
			echo $0 exec failed
		fi
        fi               
done < "$ipruleclearconf"
