#!/bin/sh /etc/rc.common
 
USE_PROCD=1
 
START=99
STOP=01
CRON_FILE=/etc/crontabs/root
CONFIGURATION=autoipsetadder
EXTRA_COMMANDS="test_crontab"
EXTRA_HELP="
		test_crontab"
set_dnsmasq_log()
{
	sed -i '/log-facility/d' /etc/dnsmasq.conf
	sed -i '/log-queries/d' /etc/dnsmasq.conf
	uci set dhcp.@dnsmasq[0].logfacility='/tmp/dnsmasq.log'
	uci delete dhcp.@dnsmasq[0].logqueries
	echo log-queries >> /etc/dnsmasq.conf
	uci commit dhcp
	/etc/init.d/dnsmasq reload
}

stop_dnsmasq_log()
{
	sed -i '/log-queries/d' /etc/dnsmasq.conf
	uci delete dhcp.@dnsmasq[0].logfacility
	uci commit dhcp
	/etc/init.d/dnsmasq reload
}

reload_service()
{
	rm -f /var/run/AdGucitest 2>/dev/null
	kill $(cat /var/run/autoipsetadder.pid)
	start
}
service_triggers() {
	procd_add_reload_trigger "$CONFIGURATION"
}
start_service() {
	# Reading config
	config_load "${CONFIGURATION}"
	mkdir -p /tmp/run/autoipsetadder
	local enabled
	config_get_bool enabled $CONFIGURATION enabled 0
	do_crontab
	if [ "$enabled" == "1" ]; then
		set_dnsmasq_log
		procd_open_instance
		procd_set_param respawn
		# pass config to script on start
		procd_set_param command sh /usr/bin/autoipsetadder/autoaddlist.sh
		procd_close_instance
		echo "autoipsetadder turn on"
		echo "enabled=$enabled"
	else
		stop_dnsmasq_log
		echo "autoipsetadder turn off"
		echo "enabled=$enabled"
	fi
}

stop_service()
{	
	config_load "${CONFIGURATION}"
	stop_dnsmasq_log
	do_crontab
	kill $(cat /var/run/autoipsetadder.pid)
	echo "autoipsetadder turn off"
	echo "enabled="$enabled""
}
do_crontab(){
	config_get_bool enabled $CONFIGURATION enabled 0
	#config_get logfile $CONFIGURATION logfile "/tmp/addlist.log"
	#config_get dnslogfile $CONFIGURATION dnslogfile "/tmp/dnsmasq.log"
	config_get crontab $CONFIGURATION crontab ""
	cronreload=0
	findstr="echo qingkong > \$(uci get autoipsetadder.autoipsetadder.dnslogfile)"
	default="0 * * * * echo qingkong > \$(uci get autoipsetadder.autoipsetadder.dnslogfile)"
	#[ -n "$lastdnslogfile" ] && findstr="echo qingkong > $lastdnslogfile" && [ "$lastdnslogfile" != "$dnslogfile" ] && replace="${lastdnslogfile//\//\\/}/${dnslogfile//\//\\/}"
	[ "$enabled" == "0" ] || [ "${crontab//autodeldnslog/}" == "$crontab" ] && cronenable=0 || cronenable=1
	crontab_editor
	#[ "$lastdnslogfile" != "$dnslogfile" ] && uci set autoipsetadder.autoipsetadder.lastdnslogfile="$dnslogfile" && commit=1
	
	findstr="/usr/bin/autoipsetadder/tailto.sh [0-9]* \$(uci get autoipsetadder.autoipsetadder.logfile)"
	default="0 0 * * * /usr/bin/autoipsetadder/tailto.sh 2000 \$(uci get autoipsetadder.autoipsetadder.logfile)"
	#[ -n "$lastlogfile" ] && findstr="/usr/bin/autoipsetadder/tailto.sh [0-9]* $lastlogfile" && [ "$lastlogfile" != "$logfile" ] && replace="${lastlogfile//\//\\/}/${logfile//\//\\/}"
	[ "$enabled" == "0" ] || [ "${crontab//autotaillog/}" == "$crontab" ] && cronenable=0 || cronenable=1
	crontab_editor
	#[ -n "$logfile" ] && [ "$lastlogfile" != "$logfile" ] && uci set autoipsetadder.autoipsetadder.lastlogfile="$logfile" && commit=1
	
	[ "$cronreload" -gt 0 ] && /etc/init.d/cron restart
	#[ "$commit" -gt 0 ] && uci commit autoipsetadder
}
crontab_editor(){
	local testline reload
	local line="$(grep "$findstr" $CRON_FILE)"
	[ -n "$replace" ] && [ -n "$line" ] && eval testline="\${line//$replace}" && [ "$testline" != "$line" ] && line="$testline" && reload="1" && replace="" 
	if [ "${line:0:1}" != "#" ]; then
		if [ $cronenable -eq 1 ]; then
			[ -z "$line" ] && line="$default" && reload="1"
			if [ -n "$reload" ]; then
				sed -i "\,$findstr,d" $CRON_FILE
				echo "$line" >> $CRON_FILE
				cronreload=$((cronreload+1))
			fi
		elif [ -n "$line" ]; then
			sed -i "\,$findstr,d" $CRON_FILE
			echo "#$line" >> $CRON_FILE
			cronreload=$((cronreload+1))
		fi
	else
		if [ $cronenable -eq 1 ]; then
			sed -i "\,$findstr,d" $CRON_FILE
			echo "${line:1}" >> $CRON_FILE
			cronreload=$((cronreload+1))
		elif [ -z "$reload" ]; then
			sed -i "\,$findstr,d" $CRON_FILE
			echo "$line" >> $CRON_FILE
		fi
	fi
}
test_crontab(){
config_load "${CONFIGURATION}"
do_crontab
}
