#!/usr/bin/env bash

source ${lib}/context.sh

dns_srv() {
  local protocol public targets comment
  local "${@}"

  echo "Adding DNS SRV '${protocol}' record "
  if [ -n "${public}" ]; then
    echo "  for '${public}' on port '${port}'..."
  else
    echo "  for apex on port '${port}'..."
  fi

  cat "${clusters}/${cluster}" |\
    yaml set - protocol "${protocol}" |\
    yaml set - public "${public}" |\
    yaml set - comment "${comment}" |\
    (cat && echo 'targets: ' && \
      printf -- '-  host: %s\n   port: %s\n' $targets) |\
    yaml template - $_P42_ROOT/share/dns/srv.yaml |\
    yaml json write - > "${tmpDir}/dns-srv-${public}.json"

    cat "${tmpDir}/dns-srv-${public}.json"
}

dns_srv \
  protocol=http \
  public=api \
  targets='api-00 32770 api-01 32771 api-02 32772' \
  comment='this is literally a test'
