#!/usr/bin/env bash set -euo pipefail if [[ $# -ne 1 ]]; then echo "Usage: pipo-undeploy " >&2 exit 1 fi target="$1" path="$( TARGET="$target" python3 - <<'PY' import os import re target = os.environ["TARGET"].strip() def sanitize_segment(value: str) -> str: value = value.lower() value = re.sub(r"[^a-z0-9]+", "-", value) return value.strip("-") if "/" in target: raw_parts = [part for part in target.strip("/").split("/") if part] parts = [sanitize_segment(part) for part in raw_parts] parts = [part for part in parts if part] if not parts: raise SystemExit(1) print("/" + "/".join(parts) + "/") else: name = sanitize_segment(target) if not name: raise SystemExit(1) print(f"/preview/{name}/") PY )" || { echo "Name cannot be empty." >&2 exit 1 } if [[ "$path" == "/" ]]; then echo "Refusing to remove /." >&2 exit 1 fi "/home/ubuntu/.local/bin/openclaw-serve-guard" serve_json="$(tailscale serve status --json 2>/dev/null || printf '{}\n')" path_exists="$( python3 -c ' import json import sys path = sys.argv[1] try: data = json.load(sys.stdin) except json.JSONDecodeError: data = {} web = data.get("Web", {}) if isinstance(data, dict) else {} handlers = {} if isinstance(web, dict) and web: _, host_cfg = next(iter(web.items())) handlers = host_cfg.get("Handlers", {}) if isinstance(host_cfg, dict) else {} print("1" if path in handlers else "0") ' "$path" <<<"$serve_json" )" if [[ "$path_exists" == "1" ]]; then tailscale serve --bg --set-path "$path" off >/dev/null 2>&1 || true fi echo "Removed $path"