#!/usr/bin/env bash # Host-local on koopa. Replaces dossiers.ngi.hacktivism.ch site-block body # with cookie→editions sticky-bar switch. Leaves .2/.3 alone. # Next: sudo ~/bin/caddy-apply set -euo pipefail CADDY="${CADDY:-$HOME/koopa-caddy/Caddyfile}" SITE="dossiers.ngi.hacktivism.ch" SNIPPET="${SNIPPET:-$HOME/bin/caddy-dossiers-ngi.snippet}" if [[ ! -f "$CADDY" ]]; then echo "FAIL: missing $CADDY" >&2 exit 1 fi if [[ ! -f "$SNIPPET" ]]; then echo "FAIL: missing snippet $SNIPPET" >&2 exit 1 fi ts="$(date +%Y%m%d-%H%M%S)" bak="${CADDY}.bak-dossiers-edition-${ts}" cp -a "$CADDY" "$bak" echo "bak: $bak" python3 - "$CADDY" "$SITE" "$SNIPPET" <<'PY' import sys from pathlib import Path caddy_path, site, snippet_path = Path(sys.argv[1]), sys.argv[2], Path(sys.argv[3]) text = caddy_path.read_text() raw_snip = snippet_path.read_text().splitlines() body_lines = [] for line in raw_snip: if line.startswith("#") or not line.strip(): continue body_lines.append(line if line.startswith("\t") else "\t" + line.lstrip()) body = "\n".join(body_lines).rstrip() + "\n" if "edition_dir" not in body or "tdpd_edition" not in body: print("FAIL: snippet body looks empty/wrong", file=sys.stderr) sys.exit(1) needle = site + " {" start = text.find(needle) if start < 0: # allow no space needle = site + "{" start = text.find(needle) if start < 0: print(f"FAIL: site block not found: {site}", file=sys.stderr) sys.exit(1) brace_open = text.find("{", start) if brace_open < 0: print("FAIL: no opening brace", file=sys.stderr) sys.exit(1) depth = 0 end = None for i, ch in enumerate(text[brace_open:], start=brace_open): if ch == "{": depth += 1 elif ch == "}": depth -= 1 if depth == 0: end = i break if end is None: print("FAIL: unclosed site block", file=sys.stderr) sys.exit(1) # keep optional newline after closing brace close_end = end + 1 if close_end < len(text) and text[close_end] == "\n": close_end += 1 new_text = text[: brace_open + 1] + "\n" + body + "}" + ("\n" if text[end+1:end+2] == "\n" or True else "") # normalize: site {\n BODY }\n new_text = text[: brace_open + 1] + "\n" + body + "}\n" + text[close_end:] caddy_path.write_text(new_text) print(f"OK: replaced body of {site} ({len(body_lines)} lines)") print("Next: sudo ~/bin/caddy-apply") PY