#!/bin/bash
# Runs *inside* a Taler container. Pasta regenerates /etc/hosts on start —
# call this from systemd (exchange) or start_base_*.sh (merchant/bank).
set -euo pipefail

PIN_IP="${PIN_IP:-212.51.151.254}"
MARKER="# hacktivism-goa-pin"

TMP=$(mktemp)
if [ -f /etc/hosts ]; then
  # drop any previous pin marker lines and lines that name our domains
  grep -vE "${MARKER}|hacktivism\\.ch" /etc/hosts >"$TMP" || true
else
  : >"$TMP"
fi

grep -qE '^127\.0\.0\.1[[:space:]]' "$TMP" 2>/dev/null \
  || echo '127.0.0.1 localhost' >>"$TMP"
grep -qE '^::1[[:space:]]' "$TMP" 2>/dev/null \
  || echo '::1 localhost ip6-localhost ip6-loopback' >>"$TMP"

{
  cat "$TMP"
  echo "$MARKER"
  echo "${PIN_IP} bank.hacktivism.ch exchange.hacktivism.ch taler.hacktivism.ch"
  echo "$MARKER"
} > /etc/hosts
rm -f "$TMP"

logger -t pin-hacktivism-hosts "pinned ${PIN_IP} → bank/exchange/taler.hacktivism.ch" 2>/dev/null || true
exit 0
