#!/bin/sh
set -euC

# This is a naive guess. A better implementation would use e.g. the Public
# Suffix List.
re='\(\(.*\)\.\)\?\([^.]\+\.[^.]\+\)'
domain="$(echo "$CERTBOT_DOMAIN" | sed -n "s/$re/\3/p")"
name="$(echo "$CERTBOT_DOMAIN" | sed -n "s/$re/\2/p")"

name="_acme-challenge${name:+".$name"}"
data="$CERTBOT_VALIDATION"

dir="$(cd "$(dirname "$0")" ; pwd)"

"$dir/nfsn-send" "POST" "/dns/$domain/removeRR" \
    "name" "$name" \
    "type" "TXT" \
    "data" "$data"