#!/bin/sh

. /lib/config/uci.sh

set -e

CACHE_DOMAINS_SRC="https://api.github.com/repos/uklans/cache-domains/tarball/master"
CACHE_DOMAINS_DIR="/var/cache-domains"
CONFIG_FILE="/etc/cache-domains.json"

dnsmasq_conf() {
	local ACTION="${1}"
	local ADDN_CONF_DIR="${2}"
	local I=0
	local DNSMASQ_CONF_DIR
	local DNSMASQ_CONF_FILE

	while uci_get "dhcp" "@dnsmasq[${I}]" > /dev/null; do
		DNSMASQ_CONF_DIR="$(uci_get "dhcp" "@dnsmasq[${I}]" "confdir" "/tmp/dnsmasq.d" || :)"
		uci_remove_list "dhcp" "@dnsmasq[${I}]" "addnmount" "${ADDN_CONF_DIR}"

		mkdir -p "${DNSMASQ_CONF_DIR}"
		case "${ACTION}" in
			add)
				ln -s "${ADDN_CONF_DIR}/"* "${DNSMASQ_CONF_DIR}" 2> /dev/null || :
				uci_add_list "dhcp" "@dnsmasq[${I}]" "addnmount" "${ADDN_CONF_DIR}"
				;;
			remove)
				for DNSMASQ_CONF_FILE in "${DNSMASQ_CONF_DIR}/"*; do
					case "$(readlink "${DNSMASQ_CONF_FILE}" || echo "${DNSMASQ_CONF_FILE}")" in
						"${ADDN_CONF_DIR}"*)
							rm -f "${DNSMASQ_CONF_FILE}"
							;;
					esac
				done
				;;
			*)
				echo "ERROR: Invalid action '${ACTION}' for dnsmasq_conf()"
				return 1
				;;
		esac

		uci_commit "dhcp"

		I=$((${I} + 1))
	done

	/etc/init.d/dnsmasq "restart"
}

configure() {
	local INITIAL_DIR
	local SOURCE_DIR
	mkdir -p "${CACHE_DOMAINS_DIR}"
	rm -fr "${CACHE_DOMAINS_DIR:?}/"*

	if ! wget -qO - "${CACHE_DOMAINS_SRC}" | tar -xzC "${CACHE_DOMAINS_DIR}"; then
		rm -fr "${CACHE_DOMAINS_DIR}"
		echo "ERROR: Could not retrieve ${CACHE_DOMAINS_SRC}"
		exit 1
	fi
	SOURCE_DIR="${CACHE_DOMAINS_DIR}/$(ls "${CACHE_DOMAINS_DIR}")"

	if [ ! -f "${CONFIG_FILE}" ]; then
		cp "${SOURCE_DIR}/scripts/config.example.json" "${CONFIG_FILE}"
		echo "Using example config file ${CONFIG_FILE}"
	fi

	INITIAL_DIR="$(pwd)"
	cd "${SOURCE_DIR}/scripts/"
	cp "${CONFIG_FILE}" "config.json"
	./create-dnsmasq.sh > /dev/null
	cd "${INITIAL_DIR}"

	dnsmasq_conf add "${SOURCE_DIR}/scripts/output/dnsmasq"
}

cleanup() {
	dnsmasq_conf remove "${CACHE_DOMAINS_DIR}/$(ls "${CACHE_DOMAINS_DIR}")/scripts/output/dnsmasq"
}

case "${1}" in
	config*)
		configure
		;;
	clean*)
		cleanup
		;;
	*)
		echo "${0} <configure|cleanup>"
		;;
esac
