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

START=99
STOP=01

APP_NAME="ruantiblock"
APP_EXEC="/usr/bin/${APP_NAME}"
VAR_DIR="/tmp"

get_dnsmasq_confdir() {
	local _first_instance_dir
	if [ -d "${VAR_DIR}/dnsmasq.d" ]; then
		printf "${VAR_DIR}/dnsmasq.d"
		return 0
	else
		_first_instance_dir=`ubus call service list | jsonfilter -e "VAR=$.dnsmasq.instances.*.mount" | awk '
			BEGIN {
				RS = " ";
			}
			{
				sub("VAR=", "", $0);
				gsub(/[\047,\134,\073]/, "", $0);
				if($0 ~ /^\/tmp\/dnsmasq\./ && !($0 in a)) {
					a[length(a)] = $0;
				};
			}
			END {
				print a[0];
			}'`
		if [ -n "$_first_instance_dir" ]; then
			printf "$_first_instance_dir"
			return 0
		fi
	fi
	return 1
}

start() {
	local _update_at_startup _dnsmasq_confdir
	config_load $APP_NAME
	config_get _update_at_startup config update_at_startup
	config_get _dnsmasq_confdir config dnsmasq_confdir ""
	if [ -z "$_dnsmasq_confdir" ]; then
		_dnsmasq_confdir=`get_dnsmasq_confdir`
		if [ $? -eq 0 -a -n "$_dnsmasq_confdir" ]; then
			uci set "${APP_NAME}.config.dnsmasq_confdir"="$_dnsmasq_confdir"
			uci commit ruantiblock
		else
			exit 1
		fi
	fi
	$APP_EXEC start
	if [ $? -eq 0 -a "$_update_at_startup" = "1" ]; then
		$APP_EXEC update
	else
		/etc/init.d/dnsmasq restart
	fi
}

stop() {
	$APP_EXEC stop
}

restart() {
	$APP_EXEC restart
}

reload() {
	$APP_EXEC reload
}
