#!/usr/bin/env bash

# ACMEDNS env variables can be set in a config file at domain level
acme_config="$DOMAIN_DIR/acme-dns.cfg"
[ -s "$acme_config" ] && . "$acme_config"

# Need to add your API user and key below or set as env variable
apiuser=${ACMEDNS_API_USER:-''}
apikey=${ACMEDNS_API_KEY:-''}
apisubdomain=${ACMEDNS_SUBDOMAIN:-''}

# This script adds a token to an ACME DNS (default to acme-dns.io) for the ACME challenge
# usage: dns_add_acme-dns "domain name" "token"
# return codes are:
# 0 - success
# 1 - error returned from server

fulldomain="${1}"
token="${2}"

# You can set the env var ACMEDNS_URL to use a specific ACME-DNS server
# Otherwise we use acme-dns.io
API=${ACMEDNS_URL:-'https://auth.acme-dns.io'}/update

# Check initial parameters
if [[ -z "$fulldomain" ]]; then
  echo "DNS script requires full domain name as first parameter"
  exit 1
fi
if [[ -z "$token" ]]; then
  echo "DNS script requires challenge token as second parameter"
  exit 1
fi

curl_params=(
  -H "accept: application/json"
  -H "X-Api-Key: $apikey"
  -H "X-Api-User: $apiuser"
  -H 'Content-Type: application/json'
)

generate_post_data()
{
  cat <<EOF
{
  "subdomain": "$apisubdomain",
  "txt": "$token"
}
EOF
}

resp=$(curl --silent \
  "${curl_params[@]}" \
  -X POST "${API}" \
  --data "$(generate_post_data)")

# If adding record failed (returned json includes "error" then print error message
if [[ "$resp" = *"\"error\""* ]]; then
  echo "Error: DNS challenge not added: unknown error - ${resp}"
  exit 1
fi
exit 0
