#!/bin/bash
source /home/johan/.config/cloudflare.env

add_record() {
  local zone_id="$1" type="$2" name="$3" content="$4" priority="$5" ttl="${6:-3600}"
  
  local data="{\"type\":\"$type\",\"name\":\"$name\",\"content\":\"$content\",\"ttl\":$ttl,\"proxied\":false"
  if [ -n "$priority" ]; then
    data="$data,\"priority\":$priority"
  fi
  data="$data}"
  
  result=$(curl -s -X POST "https://api.cloudflare.com/client/v4/zones/$zone_id/dns_records" \
    -H "Authorization: Bearer $CF_API_TOKEN" \
    -H "Content-Type: application/json" \
    -d "$data")
  
  success=$(echo "$result" | jq -r '.success')
  if [ "$success" = "true" ]; then
    echo "  ✅ $type $name → $content"
  else
    error=$(echo "$result" | jq -r '.errors[0].message // "unknown"')
    echo "  ❌ $type $name → $error"
  fi
}

echo "=== zavtra.nl ==="
Z="39f155b627f50f9cb08108d97d549427"
add_record $Z A "zavtra.nl" "47.206.57.208" "" 3600
add_record $Z MX "zavtra.nl" "mail.protonmail.ch" 10 86400
add_record $Z MX "zavtra.nl" "mailsec.protonmail.ch" 20 86400
add_record $Z TXT "zavtra.nl" "v=spf1 include:_spf.protonmail.ch mx ~all" "" 86400
add_record $Z TXT "zavtra.nl" "protonmail-verification=9dec252a54e7dd7d2e5014eac16030927e00db38" "" 86400
# FIX: _dmark → _dmarc (typo in OpenProvider!)
add_record $Z TXT "_dmarc.zavtra.nl" "v=DMARC1; p=none" "" 86400
add_record $Z CNAME "protonmail._domainkey.zavtra.nl" "protonmail.domainkey.dwy3psy43e2pxergrrzf67iyutnxke7lr75bxuc2wdrwbi2l6owsq.domains.proton.ch" "" 86400
add_record $Z CNAME "protonmail2._domainkey.zavtra.nl" "protonmail2.domainkey.dwy3psy43e2pxergrrzf67iyutnxke7lr75bxuc2wdrwbi2l6owsq.domains.proton.ch" "" 86400
add_record $Z CNAME "protonmail3._domainkey.zavtra.nl" "protonmail3.domainkey.dwy3psy43e2pxergrrzf67iyutnxke7lr75bxuc2wdrwbi2l6owsq.domains.proton.ch" "" 86400

echo ""
echo "=== flourishevents.nl ==="
F="a3a91eaf0772145613b04bd71073192b"
add_record $F A "flourishevents.nl" "185.104.29.52" "" 900
add_record $F CNAME "www.flourishevents.nl" "flourishevents.nl" "" 86400
add_record $F CNAME "ftp.flourishevents.nl" "flourishevents.nl" "" 86400
add_record $F CNAME "webmail.flourishevents.nl" "flourishevents.nl" "" 86400
add_record $F MX "flourishevents.nl" "mail.protonmail.ch" 10 86400
add_record $F MX "flourishevents.nl" "mailsec.protonmail.ch" 20 86400
add_record $F TXT "flourishevents.nl" "protonmail-verification=ce3269501b35ecc4df44bd1c2de1f45a5f6d735f" "" 900
# FIX: SPF was on spf.flourishevents.nl (wrong!) — put on root
add_record $F TXT "flourishevents.nl" "v=spf1 include:_spf.protonmail.ch ~all" "" 900
# FIX: DMARC was on dmarc.flourishevents.nl (wrong!) — put on _dmarc
add_record $F TXT "_dmarc.flourishevents.nl" "v=DMARC1; p=quarantine" "" 900
# NOTE: Missing DKIM CNAMEs in OpenProvider — adding Proton DKIM would need the key from Proton console
# Leaving without DKIM for now — will add Stalwart DKIM later

echo ""
echo "=== muskepo.nl ==="
M="7c117164578e0dbfae563be3117a6760"
# Ancient records — Google MX from way back. No active mail.
add_record $M A "muskepo.nl" "71.122.219.188" "" 14400
add_record $M A "www.muskepo.nl" "71.122.219.188" "" 14400
add_record $M A "*.muskepo.nl" "71.122.219.188" "" 14400
add_record $M MX "muskepo.nl" "ALT1.ASPMX.L.GOOGLE.COM" 10 3600

echo ""
echo "=== busel.nl ==="
B="cb0a0d59a5001e134769f84a8584330e"
add_record $B A "busel.nl" "64.74.162.250" "" 14400
add_record $B A "www.busel.nl" "64.74.162.250" "" 14400
add_record $B A "*.busel.nl" "64.74.162.250" "" 14400
add_record $B MX "busel.nl" "mail.busel.nl" 5 3600

echo ""
echo "Done! Skipped localhost records (useless)."
