#!/usr/bin/env bash
# Open a jailed SSH session to the client's own cPanel account, using the key
# provisioned by bootstrap.mjs. Any extra args are run as a remote command
# (non-interactive); with none, opens an interactive shell.
#   ssh-connect.sh                 # interactive shell
#   ssh-connect.sh 'wp --info'     # one-off command
set -euo pipefail
CRED="$HOME/.awesomate/credentials.json"
[ -f "$CRED" ] || { echo "Not connected. Run bootstrap first (Connect Claude Code on hub.awesomate.ai/claude)."; exit 1; }

# Resolve WHICH account (folder pin / AWESOMATE_ACCOUNT / sole profile) and its
# ssh block via the shared resolver. Capture then eval so a resolver failure
# aborts with its own message instead of tripping set -u later.
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
if ! CREDS_EVAL="$(node "$SCRIPT_DIR/resolve-account.mjs" --ssh)"; then exit 1; fi
eval "$CREDS_EVAL"
echo "→ account: $ACCT" >&2

exec ssh -i "$K" -p "$P" -o StrictHostKeyChecking=accept-new "$U@$H" "$@"
