#!/bin/bash
set -e

# Routing
ip route del default 2>/dev/null || true
ip route add default via 100.64.0.1

# Seed writable zone files from the read-only fixtures.
#
# The DDNS simulator (RFC-2136-style /update) rewrites <domain>.zone on every
# registration, and Knot rewrites them on reload. If the zone files were
# bind-mounted directly from the repo (RW), those runtime writes would mutate
# version-controlled fixtures — and a DDNS update with no source IP used to
# stamp `@ IN A 0.0.0.0` straight into the repo's celilo.computer.zone (a
# prohibited cert-skip sentinel). Fix: fixtures are mounted READ-ONLY under
# /seed; copy them into the writable /config the sim + Knot actually use, so
# all runtime mutation stays container-local.
mkdir -p /config
if [ -d /seed ]; then
  cp -f /seed/*.zone /config/ 2>/dev/null || true
fi

# Start knot DNS
/usr/sbin/knotd --config /config/knot.conf --daemonize

# Start DDNS simulator (will be added in Phase 3)
if [ -f /simulator/server.ts ]; then
  cd /simulator && bun run server.ts &
fi

echo "namecheap-dns ready"
sleep infinity
