#! /usr/bin/env bash
# NS1 Add DNS Record

if [[ -z "$NS1_API_KEY" ]]; then
  echo "NS1_API_KEY variable not set"
  exit 1
fi

api_url="https://api.nsone.net/v1/"
api_key=${NS1_API_KEY:-''}

domain="$1"
challenge="$2"

root=$(echo "$domain" | awk -F\. '{print $(NF-1) FS $NF}')
subdomain="_acme-challenge.${domain%}"

function create {

curl "${api_url}/zones/${root}/${subdomain}/TXT" -X DELETE \
  --header "X-NSONE-Key: $api_key"

curl "${api_url}/zones/${root}/${subdomain}/TXT" -X PUT \
  --header "X-NSONE-Key: $api_key" \
  --header "Content-Type: application/json" \
  --data "{ \"zone\": \"${root}\", \"domain\": \"${subdomain}\", \"type\": \"TXT\", \"answers\": [ { \"answer\": [ \"${challenge}\" ] } ] }"

}

create $root $subdomain
