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

START=90
EXTRA_COMMANDS="toggle stats cfgup envchk query"
EXTRA_HELP="	toggle	Toggle adblock 'on' or 'off'
	stats	Update adblock statistics
	cfgup	Update adblock configuration file
	envchk	Check/Set adblock environment
	query	<DOMAIN> Query active blocklists for specific domain"

adb_debug=0
adb_pid="${$}"
adb_script="/usr/bin/adblock-update.sh"
adb_helper="/usr/bin/adblock-helper.sh"
adb_pidfile="/var/run/adblock.pid"
bg_parm="&"

if [ $((adb_debug)) -eq 0 ]
then
    exec 2>/dev/null
fi

if [ -r "${adb_pidfile}" ]
then
    logger -s -t "adblock[${adb_pid}] error" "adblock service already running ($(cat ${adb_pidfile}))" 2>&1
    exit 255
fi

. "${adb_helper}"
f_envload

if [ "${adb_restricted}" = "1" ]
then
    adb_uci="$(which true)"
fi

boot()
{
    return 0
}

start()
{
    if [ -t 1 ]
    then
        unset bg_parm
    fi
    eval "${adb_script}" ${bg_parm}
    return 0
}

restart()
{
    stop
    start
}

reload()
{
    reload="true"
    stop
    start
}

stop()
{
    f_rmdns
    f_rmuhttpd
    config_foreach f_rmconfig source
    if [ -z "${reload}" ]
    then
        f_rmfirewall
    fi
    if [ -n "${rm_dns}" ] || [ -n "${rm_uhttpd}" ] || [ -n "${rm_fw}" ] || [ -n "$(${adb_uci} -q changes adblock)" ]
    then
        "${adb_uci}" -q commit adblock
        f_log "all adblock related services stopped"
    fi
    return 0
}

toggle()
{
    if [ -d "${adb_dnshidedir}" ]
    then
        list_dns="$(find "${adb_dnsdir}" -maxdepth 1 -type f -name "${adb_dnsprefix}*" -print)"
        list_dnshide="$(find "${adb_dnshidedir}" -maxdepth 1 -type f -name "${adb_dnsprefix}*" -print)"
        if [ -n "${list_dns}" ]
        then
            source="${adb_dnsdir}/${adb_dnsprefix}"
            target="${adb_dnshidedir}"
            pos="off"
        elif [ -n "${list_dnshide}" ]
        then
            source="${adb_dnshidedir}/${adb_dnsprefix}"
            target="${adb_dnsdir}"
            pos="on"
        fi
        if [ -n "${list_dns}" ] || [ -n "${list_dnshide}" ]
        then
            mv -f "${source}"* "${target}"
            /etc/init.d/dnsmasq restart
            "${adb_uci}" -q set "adblock.global.adb_dnstoggle=${pos}"
            "${adb_uci}" -q commit "adblock"
            f_log "adblock toggle switched '${pos}'"
        fi
    fi
    return 0
}

stats()
{
    f_statistics
    "${adb_uci}" -q commit "adblock"
    return 0
}

cfgup()
{
    stop
    cp -pf "/etc/adblock/adblock.conf.default" "/etc/config/adblock"
    rc=$?
    if [ $((rc)) -eq 0 ]
    then
        f_log "default adblock configuration applied, please check the settings in '/etc/config/adblock'"
    else
        f_log "default adblock configuration not found, please re-install the package via 'opkg install adblock --force-maintainer'"
    fi
    return 0
}

query()
{
    domain="${1}"
    tld="${domain#*.}"
    list_dns="$(find "${adb_dnsdir}" -maxdepth 1 -type f -name "${adb_dnsprefix}*" -print)"
    if [ -z "${list_dns}" ]
    then
        f_log "no active blocklists found, please start adblock first"
    elif [ -z "${domain}" ] || [ "${domain}" = "${tld}" ]
    then
        f_log "invalid domain query input, please submit a specific (sub-)domain, i.e. 'www.abc.xyz'"
    else
        while [ "${domain}" != "${tld}" ]
        do
            search="${domain//./\.}"
            result="$(grep -Hm 1 "[/\.]${search}/" "${adb_dnsdir}/adb_list"* | awk -F ':|/' '{print "   "$4"\t: "$6}')"
            count="$(grep -hc "[/\.]${search}/" "${adb_dnsdir}/adb_list"* | awk '{sum += $1} END {printf sum}')"
            printf "%s\n" "=> distinct results for domain '${domain}' (overall ${count})"
            if [ -z "${result}" ]
            then
                printf "%s\n" "   no matches in active blocklists"
            else
                printf "%s\n" "${result}"
            fi
            domain="${tld}"
            tld="${domain#*.}"
        done
    fi
    return 0
}

envchk()
{
    adb_loglevel=0
    f_envcheck
    rm -f "${adb_tmpfile}"
    rm -rf "${adb_tmpdir}"
    adb_loglevel=1
    f_log "adblock environment check finished successfully"
    return 0
}
