#!/usr/bin/env bash
#
# Open an SSH tunnel that exposes prod Postgres on localhost:5433. Useful
# for running `psql`, `supabase db push`, or any GUI DB client against prod.
#
# The prod Postgres container only listens on the host's localhost:5432, so
# we need an SSH tunnel to reach it from the Mac. Tunnel stays open until
# Ctrl+C.
#
# Usage:
#   ./scripts/prod/tunnel-db.sh
#   # then in another terminal:
#   psql -h localhost -p 5433 -U postgres -d postgres

set -euo pipefail

here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "${here}/config.sh"

local_port="${1:-5433}"

echo "tunnel: localhost:${local_port} → ${SSH_HOST}:localhost:5432"
echo "press Ctrl+C to close"
# shellcheck disable=SC2086
exec ssh ${SSH_OPTS} -N -L "${local_port}:localhost:5432" "${SSH_HOST}"
