Files
bin/af
T

52 lines
2.4 KiB
Bash
Executable File

#!/usr/bin/env bash
# ~/bin/af - Antigravity Fleet Unified Session Connector
# Attaches to the central persistent Antigravity CLI session (default: bazina).
SESSION_NAME="agy"
DEFAULT_HOST="bazina"
DEFAULT_IP="192.168.1.104"
FALLBACK_HOST="boxC"
FALLBACK_IP="192.168.1.101"
REMOTE_PORT="2222"
EXTERNAL_HOST="home"
DEFAULT_CONV="9860fe31-3526-48d1-9063-3e58e630faf9"
# Determine current node
CURRENT_HOST="$(hostname -s 2>/dev/null || uname -n)"
if [ -n "$TERMUX_VERSION" ] || [ -d "/data/data/com.termux" ]; then
CURRENT_HOST="phone"
fi
# Build agy conversation parameter if not already supplied
AGY_INVOCATION="agy"
if [[ "$*" != *"--conversation"* ]] && [[ -n "$DEFAULT_CONV" ]]; then
AGY_INVOCATION="agy --conversation=$DEFAULT_CONV"
fi
if [[ "$CURRENT_HOST" == "bazina" ]]; then
# Already on default node (bazina): attach or create local tmux session
exec tmux new-session -A -s "$SESSION_NAME" $AGY_INVOCATION "$@"
elif [[ "$CURRENT_HOST" == "boxC" || "$CURRENT_HOST" == "B" ]]; then
# On boxC: connect to bazina if reachable, otherwise fall back to local boxC session
if ping -c 1 -W 1 "$DEFAULT_IP" &>/dev/null || ssh -o BatchMode=yes -o ConnectTimeout=2 "$DEFAULT_HOST" "true" &>/dev/null; then
exec ssh -t "$DEFAULT_HOST" "tmux new-session -A -s '$SESSION_NAME' $AGY_INVOCATION $*"
else
echo "[af] bazina is unreachable from boxC; falling back to local boxC session..."
exec tmux new-session -A -s "$SESSION_NAME" $AGY_INVOCATION "$@"
fi
else
# Remote node (t520, penguin, android/phone, etc.)
# 1. Try default node (bazina) on LAN
if ping -c 1 -W 1 "$DEFAULT_IP" &>/dev/null || ssh -o BatchMode=yes -o ConnectTimeout=2 "$DEFAULT_HOST" "true" &>/dev/null; then
exec ssh -t "$DEFAULT_HOST" "tmux new-session -A -s '$SESSION_NAME' $AGY_INVOCATION $*"
# 2. Fallback to boxC on LAN
elif ping -c 1 -W 1 "$FALLBACK_IP" &>/dev/null || ssh -o BatchMode=yes -o ConnectTimeout=2 "$FALLBACK_HOST" "true" &>/dev/null; then
echo "[af] bazina unreachable; falling back to boxC on LAN..."
exec ssh -t "$FALLBACK_HOST" "tmux new-session -A -s '$SESSION_NAME' $AGY_INVOCATION $*"
# 3. Fallback to external SSH via home gateway
else
echo "[af] LAN unreachable; attempting external gateway fallback..."
exec ssh -p "$REMOTE_PORT" -t "$EXTERNAL_HOST" "tmux new-session -A -s '$SESSION_NAME' $AGY_INVOCATION $*"
fi
fi