/g' | tr '@' '\n' | grep edit.php | grep "$fulldomain")"
    _debug3 "subdomain_csv: $subdomain_csv"
    # The above beauty ends with striping out rows that do not have an
    # href to edit.php and do not have the domain name we are looking for.
    # So all we should be left with is CSV of table of subdomains we are
    # interested in.
    # Now we have to read through this table and extract the data we need
    lines="$(echo "$subdomain_csv" | wc -l)"
    i=0
    found=0
    DNSdataid=""
    while [ "$i" -lt "$lines" ]; do
      i="$(_math "$i" + 1)"
      line="$(echo "$subdomain_csv" | sed -n "${i}p")"
      _debug3 "line: $line"
      DNSname="$(echo "$line" | _egrep_o 'edit.php.*' | cut -d '>' -f 2 | cut -d '<' -f 1)"
      _debug2 "DNSname: $DNSname"
      if [ "$DNSname" = "$fulldomain" ]; then
        DNStype="$(echo "$line" | sed 's/| ' -f 2 | cut -d '<' -f 1)"
        _debug2 "DNStype: $DNStype"
        if [ "$DNStype" = "TXT" ]; then
          DNSdataid="$(echo "$line" | _egrep_o 'data_id=.*' | cut -d = -f 2 | cut -d '>' -f 1)"
          _debug2 "DNSdataid: $DNSdataid"
          DNSvalue="$(echo "$line" | sed 's/ | ' -f 2 | cut -d '<' -f 1)"
          if _startswith "$DNSvalue" """; then
            # remove the quotation from the start
            DNSvalue="$(echo "$DNSvalue" | cut -c 7-)"
          fi
          if _endswith "$DNSvalue" "..."; then
            # value was truncated, remove the dot dot dot from the end
            DNSvalue="$(echo "$DNSvalue" | sed 's/...$//')"
          elif _endswith "$DNSvalue" """; then
            # else remove the closing quotation from the end
            DNSvalue="$(echo "$DNSvalue" | sed 's/......$//')"
          fi
          _debug2 "DNSvalue: $DNSvalue"
          if [ -n "$DNSdataid" ] && _startswith "$txtvalue" "$DNSvalue"; then
            # Found a match. But note... Website is truncating the
            # value field so we are only testing that part that is not 
            # truncated.  This should be accurate enough.
            _debug "Deleting TXT record for $fulldomain, $txtvalue"
            _freedns_delete_txt_record "$FREEDNS_COOKIE" "$DNSdataid"
            return $?
          fi
        fi
      fi
    done
  done
  # If we get this far we did not find a match (after two attempts)
  # Not necessarily an error, but log anyway.
  _debug3 "$subdomain_csv"
  _info "Cannot delete TXT record for $fulldomain, $txtvalue. Does not exist at FreeDNS"
  return 0
}
####################  Private functions below ##################################
# usage: _freedns_login username password
# print string "cookie=value" etc.
# returns 0 success
_freedns_login() {
  export _H1="Accept-Language:en-US"
  username="$1"
  password="$2"
  url="https://freedns.afraid.org/zc.php?step=2"
  _debug "Login to FreeDNS as user $username"
  htmlpage="$(_post "username=$(printf '%s' "$username" | _url_encode)&password=$(printf '%s' "$password" | _url_encode)&submit=Login&action=auth" "$url")"
  if [ "$?" != "0" ]; then
    _err "FreeDNS login failed for user $username bad RC from _post"
    return 1
  fi
  cookies="$(grep -i '^Set-Cookie.*dns_cookie.*$' "$HTTP_HEADER" | _head_n 1 | tr -d "\r\n" | cut -d " " -f 2)"
  # if cookies is not empty then logon successful
  if [ -z "$cookies" ]; then
    _debug3 "htmlpage: $htmlpage"
    _err "FreeDNS login failed for user $username. Check $HTTP_HEADER file"
    return 1
  fi
  printf "%s" "$cookies"
  return 0
}
# usage _freedns_retrieve_subdomain_page login_cookies
# echo page retrieved (html)
# returns 0 success
_freedns_retrieve_subdomain_page() {
  export _H1="Cookie:$1"
  export _H2="Accept-Language:en-US"
  url="https://freedns.afraid.org/subdomain/"
  _debug "Retrieve subdomain page from FreeDNS"
  htmlpage="$(_get "$url")"
  if [ "$?" != "0" ]; then
    _err "FreeDNS retrieve subdomains failed bad RC from _get"
    return 1
  elif [ -z "$htmlpage" ]; then
    _err "FreeDNS returned empty subdomain page"
    return 1
  fi
  _debug3 "htmlpage: $htmlpage"
  printf "%s" "$htmlpage"
  return 0
}
# usage _freedns_add_txt_record login_cookies domain_id subdomain value
# returns 0 success
_freedns_add_txt_record() {
  export _H1="Cookie:$1"
  export _H2="Accept-Language:en-US"
  domain_id="$2"
  subdomain="$3"
  value="$(printf '%s' "$4" | _url_encode)"
  url="https://freedns.afraid.org/subdomain/save.php?step=2"
  htmlpage="$(_post "type=TXT&domain_id=$domain_id&subdomain=$subdomain&address=%22$value%22&send=Save%21" "$url")"
  if [ "$?" != "0" ]; then
    _err "FreeDNS failed to add TXT record for $subdomain bad RC from _post"
    return 1
  elif ! grep "200 OK" "$HTTP_HEADER" >/dev/null; then
    _debug3 "htmlpage: $htmlpage"
    _err "FreeDNS failed to add TXT record for $subdomain. Check $HTTP_HEADER file"
    return 1
  elif _contains "$htmlpage" "security code was incorrect"; then
    _debug3 "htmlpage: $htmlpage"
    _err "FreeDNS failed to add TXT record for $subdomain as FreeDNS requested security code"
    _err "Note that you cannot use automatic DNS validation for FreeDNS public domains"
    return 1
  fi
  _debug3 "htmlpage: $htmlpage"
  _info "Added acme challenge TXT record for $fulldomain at FreeDNS"
  return 0
}
# usage _freedns_delete_txt_record login_cookies data_id
# returns 0 success
_freedns_delete_txt_record() {
  export _H1="Cookie:$1"
  export _H2="Accept-Language:en-US"
  data_id="$2"
  url="https://freedns.afraid.org/subdomain/delete2.php"
  htmlheader="$(_get "$url?data_id%5B%5D=$data_id&submit=delete+selected" "onlyheader")"
  if [ "$?" != "0" ]; then
    _err "FreeDNS failed to delete TXT record for $data_id bad RC from _get"
    return 1
  elif ! _contains "$htmlheader" "200 OK"; then
    _debug2 "htmlheader: $htmlheader"
    _err "FreeDNS failed to delete TXT record $data_id"
    return 1
  fi
  _info "Deleted acme challenge TXT record for $fulldomain at FreeDNS"
  return 0
} |