#!/bin/sh

# This file is needed by the Unsafe Browser, and Tor while in bridge
# mode.

# Run only when the interface is not "lo":
if [ -z "$1" ] || [ "$1" = "lo" ]; then
   exit 0
fi

RESOLV_CLEARNET_CONF=/etc/resolv-over-clearnet.conf
# We are truncating the file as opposed to deleting + recreating it
# for a reason: we mount-bind this file over /etc/resolv.conf for
# processes (via mount namespaces) that we want to give clearnet DNS
# resolving, and deleting + recreating it would mean that the
# bind-mount would remain outdated.
echo -n > "${RESOLV_CLEARNET_CONF}"
IP4_REGEX='[0-9]{1,3}(\.[0-9]{1,3}){3}'
for ns in ${IP4_NAMESERVERS}; do
    if echo "${ns}" | grep --extended-regexp -q "^${IP4_REGEX}$"; then
        echo "nameserver ${ns}" >> "${RESOLV_CLEARNET_CONF}"
    fi
done

if [ ! -s "${RESOLV_CLEARNET_CONF}" ] && command -v nmcli >/dev/null 2>&1; then
    nmcli --get-values IP4.DNS device show "$1" 2>/dev/null \
        | while read -r ns; do
            if echo "${ns}" | grep --extended-regexp -q "^${IP4_REGEX}$"; then
                echo "nameserver ${ns}" >> "${RESOLV_CLEARNET_CONF}"
            fi
        done
fi

if [ ! -s "${RESOLV_CLEARNET_CONF}" ]; then
    printf '%s\n' \
        'nameserver 1.1.1.1' \
        'nameserver 9.9.9.9' \
        >> "${RESOLV_CLEARNET_CONF}"
fi

if [ "$(cat /etc/elizaos/privacy-mode 2>/dev/null || printf on)" = "off" ] \
        && [ -s "${RESOLV_CLEARNET_CONF}" ]; then
    cp "${RESOLV_CLEARNET_CONF}" /etc/resolv.conf
fi
