#!/bin/sh
# T3 KILL SWITCH TRIGGER — fails CLOSED. Engagement requires human-provisioned
# credentials; with none configured this script refuses and does nothing.
set -u
refuse(){ printf 'killswitch: REFUSED (%s) — fail-closed\n' "$1" >&2; exit 1; }
[ -n "${KILL_SWITCH_TOKEN:-}" ]   || refuse "no credential configured"
[ -n "${KILL_SWITCH_SHA256:-}" ]  || refuse "no seal configured"
actual=$(printf '%s' "$KILL_SWITCH_TOKEN" | sha256sum | cut -d' ' -f1)
[ "$actual" = "$KILL_SWITCH_SHA256" ] || refuse "credential mismatch"
[ -n "${CF_API_TOKEN:-}" ]        || refuse "no control-plane credential configured"
curl -fsS --max-time 20 -X PUT \
  "https://api.cloudflare.com/client/v4/accounts/87f71cf268d9b69fdfd2725afbfb4483/storage/kv/namespaces/b56c6987760b424b88bdb0d2d7b30605/values/arch%2Fkillswitch" \
  -H "Authorization: Bearer $CF_API_TOKEN" -H "Content-Type: application/json" \
  --data '{"value":"engaged"}' >/dev/null || refuse "control-plane write failed"
printf 'killswitch: ENGAGED\n'
exit 0
