# If DNS_SERVER=auto, check if a DNS service is running on the LTSP server.
if [ "$DNS_SERVER" = "auto" ] && [ -n "$SERVER" ]; then
    if nc -w 1 -z "$SERVER" 53; then
        DNS_SERVER="$SERVER"
    else
        unset DNS_SERVER
    fi
fi

# set to defaults from DHCP if not specified in lts.conf
if [ -z "$DNS_SERVER" ]; then
    for dns in $IPV4DNS0 $IPV4DNS1 ; do
        # ignore nameserver of 0.0.0.0, which ipconfig may return if both
        # nameservers aren't specified.
        if [ "$dns" != "0.0.0.0" ]; then
            DNS_SERVER="$DNS_SERVER $dns"
        fi
    done
fi

if [ -z "$SEARCH_DOMAIN" ] && [ -n "$DNSDOMAIN" ]; then
    SEARCH_DOMAIN="$DNSDOMAIN"
fi

# Apply the dns info that was received from dhcp or from lts.conf
if [ -n "$DNS_SERVER" ] || [ -n "$SEARCH_DOMAIN" ]; then
    # Deal with resolvconf
    if [ -x /sbin/resolvconf ] && ( [ -L /etc/resolv.conf ] || [ -e /var/lib/resolvconf/convert ] ); then
        mkdir -p /run/resolvconf/interface/
        resolv_method=resolvconf
        resolv=/run/resolvconf/interface/LTSP
    else
        resolv_method=static
        resolv=/etc/resolv.conf
    fi

    # Check if $resolv is writable
    if echo '# Generated by ltsp' 2>/dev/null > "$resolv"; then
        if [ -n "$SEARCH_DOMAIN" ]; then
            echo "search $SEARCH_DOMAIN" >> "$resolv"
        fi
        for n in $DNS_SERVER; do
            echo "nameserver $n" >> "$resolv"
        done
    fi
fi
true
