#!/bin/ksh
set -e -u -o pipefail

err() {
	printf "%s: %s\n" "${0##*/}" "$1" 1>&2
	exit 1
}

ident=${CERTBOT_IDENTIFIER%.}
fqdn=$(hostname)

zone=${fqdn#"$(hostname -s)."}
record=${ident%".$zone"}

# shellcheck disable=SC2053
[[ $ident == $fqdn || $ident == *.$zone ]] || err 'identifier not in/under local domain'

yc-dyndns "$@" --debug \
    --record "$record" \
    --zone   "$zone"   \
    --acme   "$CERTBOT_VALIDATION" |
	jq --slurp last
