#!/bin/sh /etc/rc.common
# Copyright (C) 2020 IrineSistiana
# shellcheck disable=SC2034

START=91
USE_PROCD=1

#####  ONLY CHANGE THIS BLOCK  ######
PROG=/usr/bin/mosdns                        # where is mosdns
RES_DIR=/etc/mosdns/                        # resource dir / working dir / the dir where you store ip/domain lists
CONF=$(uci -q get mosdns.mosdns.configfile) # where is the config file, it can be a relative path to $RES_DIR
CRON_FILE=/etc/crontabs/root
#####  ONLY CHANGE THIS BLOCK  ######

inital_conf() {
	config_load "mosdns"
	config_get "enabled" "mosdns" "enabled" "0"
}
service_triggers() {
	procd_add_reload_trigger "mosdns"
}

restore_setting() {
	sed -i "/list server/d" /etc/config/dhcp
	uci set dhcp.@dnsmasq[0].noresolv='0'
	uci del dhcp.@dnsmasq[0].cachesize
	uci set dhcp.@dnsmasq[0].filter_aaaa='1'
	uci commit dhcp
}

prepare_setting() {
	redirect=$(uci -q get mosdns.mosdns.redirect)
	if [ "$redirect" -eq 1 ]; then
		sed -i "/list server/d" /etc/config/dhcp
		uci add_list dhcp.@dnsmasq[0].server='127.0.0.1#5335'
		uci set dhcp.@dnsmasq[0].rebind_protection='0'
		uci set dhcp.@dnsmasq[0].noresolv="1"
		uci set dhcp.@dnsmasq[0].cachesize='0'
		uci set dhcp.@dnsmasq[0].filter_aaaa='0'
		uci commit dhcp
	fi
}

restart_others() {
	/etc/init.d/network reload
	/etc/init.d/dnsmasq reload
}

reload_service() {
	stop
	sleep 2s
	echo "MosDNS is restarted!"
	start
}

setcron() {
	touch $CRON_FILE
	sed -i '/mosupdater.sh/d' $CRON_FILE 2>/dev/null && {
		[ "$(uci -q get mosdns.mosdns.geo_auto_update)" -eq 1 ] && echo "0 $(uci -q get mosdns.mosdns.geo_update_day_time) * * $(uci -q get mosdns.mosdns.geo_update_week_time) /etc/mosdns/mosupdater.sh" >>$CRON_FILE
	}
	crontab $CRON_FILE
}

delcron() {
	sed -i '/mosupdater.sh/d' $CRON_FILE 2>/dev/null
	/etc/init.d/cron restart
}

adblock() {
	cp -f /etc/mosdns/serverlist.txt /etc/mosdns/serverlist.bak
	modns_adblock=$(uci -q get mosdns.mosdns.adblock)
	if [ "$modns_adblock" = 0 ]; then
		: > /etc/mosdns/serverlist.txt
	else
		cat /etc/mosdns/serverlist.bak >/etc/mosdns/serverlist.txt
	fi
}

start_service() {

	# Reading config
	inital_conf
	if [ "$enabled" -eq 0 ]; then
		firstblood=$(uci -q get mosdns.mosdns.not_first_start)
		[ "${firstblood}" = "0" ] && restore_setting
		[ "${firstblood}" = "0" ] && restart_others
		uci set mosdns.mosdns.not_first_start='1'
		uci commit mosdns
		echo "MosDNS has turned off"
		return 1
	fi
	delcron
	setcron
	adblock
	procd_open_instance mosdns
	procd_set_param command $PROG -dir $RES_DIR -c "$CONF"
	procd_set_param user root
	procd_set_param stdout 1
	procd_set_param stderr 1
	procd_set_param respawn "${respawn_threshold:-3600}" "${respawn_timeout:-5}" "${respawn_retry:-5}"
	procd_close_instance mosdns

	configfile=$(uci -q get mosdns.mosdns.configfile)
	if [ "${configfile}" = "./def_config.yaml" ]; then
		restore_setting
		prepare_setting
		restart_others
	fi

	echo "MosDNS turn on"
	echo "enabled=$enabled"

}

stop_service() {

	pgrep -f /usr/bin/mosdns | xargs kill -9
	echo "MosDNS turn off"
	echo "enabled=$enabled"

	configfile=$(uci -q get mosdns.mosdns.configfile)
	if [ "${configfile}" = "./def_config.yaml" ]; then
		config_load "mosdns"
		enabled=$(uci -q get mosdns.mosdns.enabled)
		[ "${enabled}" = "0" ] && restore_setting
		restart_others
	fi
	delcron

}
