#!/bin/sh

set -e

WORKER_ID=$1

if [ -z "$WORKER_ID" ]; then
  echo "arg-1 (WORKER_ID) is missing"
  exit 1;
fi;


if [ -z "$CLOUDFLARE_ACCOUNT_ID" ]; then
  echo "CLOUDFLARE_ACCOUNT_ID is missing"
  exit 1;
fi;

if [ -z "$CLOUDFLARE_TOKEN" ]; then
  echo "CLOUDFLARE_TOKEN is missing"
  exit 1;
fi;

curl -s https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/workers/services/$WORKER_ID/environments/production/bindings \
  -H "authorization: Bearer $CLOUDFLARE_TOKEN" \
  | jq -r '.result | map(select(.name == "KV_PAGES")) | .[0].namespace_id'
