#!/bin/sh /etc/rc.common
# Copyright (C) 2007 OpenWrt.org

START=60
PID_FILE=/var/run/dnsmasq.pid

trap 'echo "dnsmasq script will not exit with SIGUSR1"' SIGUSR1
trap 'echo "dnsmasq script will not exit with SIGUSR2"' SIGUSR2

set_hijack() {
	sleep 2
	# TRY TO MAKE SURE the `dnsmasq` got the siginal
	killall -SIGUSR1 dnsmasq
	sleep 1
	killall -SIGUSR1 dnsmasq
}

start() {
	if [ "$($CONFIG get wds_endis_fun)" = "1" -a "$($CONFIG get wds_repeater_basic)" = "0" -o "$($CONFIG get wla_wds_endis_fun)" = "1" -a "$($CONFIG get wds_repeater_basic_a)" = "0" ]; then
		# should not start dnsmasq in WDS repeater mode
		exit
	fi

	[ ! -f /tmp/resolv.conf ] && touch /tmp/resolv.conf

	local opt_argv=""
	local resolv_file="/tmp/resolv.conf"
	
	opt_argv="$opt_argv --lan-interface=$($CONFIG get lan_ifname)"
	# start parental ctl config
	# According to SPEC v13, ParentalControl is not supported in AP mode temporarily.
	if [ "$($CONFIG get ParentalControl)" = "1" -a "$($CONFIG get i_opmode)" != "apmode" ]; then
		$CONFIG get ParentalControl_table > /tmp/parentalcontrol.conf
		opt_argv="$opt_argv --parental-control"
		resolv_file="/tmp/resolv_opendns.conf"
		echo "nameserver 208.67.222.222" > $resolv_file
		echo "nameserver 208.67.220.220" >> $resolv_file
		if ! [ -n "$(cat /tmp/resolv.conf | grep "options single-request-reopen")" ];then
			echo "options single-request-reopen" >> /tmp/resolv.conf
		fi
	fi
	# end parental ctl config

	# start wan ifname config
	if [ "$($CONFIG get ap_mode)" = "1" -o "$($CONFIG get bridge_mode)" = "1" ]; then
		opt_argv="$opt_argv --wan-interface=$BR_IF"
#	else
#		if [ "$($CONFIG get wan_proto)" = "pppoe" -o "$($CONFIG get wan_proto)" = "pptp" -o "$($CONFIG get wan_proto)" = "l2tp" ]; then
#			opt_argv="$opt_argv --wan-interface=ppp0"
#		else
#			opt_argv="$opt_argv --wan-interface=$WAN_IF"
#		fi
	fi
	# end wan ifname config
	if [ "$($CONFIG get ap_mode)" = "1" ]; then
		opt_argv="$opt_argv --ap-mode"
	fi


	if [ "$($CONFIG get wan_proto)" = "pppoe" -o "$($CONFIG get wan_proto)" = "pptp" -o "$($CONFIG get wan_proto)" = "l2tp" -o "$($CONFIG get wan_proto)" = "mulpppoe1" ]; then
		local ppp=$($CONFIG get wan_proto)
		[ "$ppp" = "mulpppoe1" ] && ppp=mulpppoe
		local ppp_demand=$($CONFIG get wan_"$ppp"_demand)
		if [ "$ppp_demand" = "1" ]; then
			opt_argv="$opt_argv --ppp-dial-demand"
		fi
	fi
	# start static pptp config
	local static_pptp_enable=1
	[ "$($CONFIG get GUI_Region)" = "Russian" ] || static_pptp_enable=0
	[ "$($CONFIG get wan_proto)" = "pptp" ] || static_pptp_enable=0
	[ "$($CONFIG get wan_pptp_wan_assign)" = "1" ] || static_pptp_enable=0
	[ "$($CONFIG get wan_pptp_dns_assign)" = "1" ] || static_pptp_enable=0
	if [ "$static_pptp_enable" = "1" ]; then
		echo "interface $WAN_IF" > /tmp/pptp.conf
		echo "myip $($CONFIG get wan_pptp_local_ip)" >> /tmp/pptp.conf
		echo "gateway $($CONFIG get pptp_gw_static_route)" >> /tmp/pptp.conf
		echo "netmask $($CONFIG get wan_pptp_eth_mask)" >> /tmp/pptp.conf
		echo "resolv /tmp/pptp-resolv.conf" >> /tmp/pptp.conf
		echo "nameserver $($CONFIG get wan_ether_dns1)" > /tmp/pptp-resolv.conf
		echo "nameserver $($CONFIG get wan_ether_dns2)" >> /tmp/pptp-resolv.conf
		opt_argv="$opt_argv --static-pptp"
	else
		[ -f /tmp/pptp.conf ] && rm -f /tmp/pptp.conf
		[ -f /tmp/pptp-resolv.conf ] && rm -f /tmp/pptp-resolv.conf
	fi
	# end static pptp config

	#The source port used by DNS client must be greater or equal to 32768.
	opt_argv="$opt_argv --min-port=32768"
	
	region="$(/sbin/artmtd -r region | grep REGION | awk '{print $2}')"

	#If circle enabled, change listen port to 5300 to get query forwarded by circle
	if [ "$($CONFIG get enable_circle_plc)" = "1" -a "$($CONFIG get i_opmode)" = "normal" -a "$($CONFIG get reboot_flag_for_circle)" = "0" -a "$($CONFIG get dgc_func_have_circle)" = "1" ]; then
		/usr/sbin/dnsmasq -h -n -c 0 -N -i $BR_IF -r $resolv_file -u root -p 5300
	elif [ "$region" = "PR" -a "$($CONFIG get hijack_process)" = "3" ];then
		[ -f $PID_FILE ] || /usr/sbin/dnsmasq -r $resolv_file -u root -P 4096 $opt_argv -x $PID_FILE
	else
		[ -f $PID_FILE ] || /usr/sbin/dnsmasq --except-interface=lo -r $resolv_file -u root -P 4096 $opt_argv -x $PID_FILE
	fi

	[ "$($CONFIG get dns_hijack)" = "1" ] && set_hijack &
}

stop() {
	rm $PID_FILE
#	killall dnsmasq
	ps -w |grep /usr/sbin/dnsmasq |grep -v grep |awk -F ' ' '{print $1}' |xargs kill
}
