#!/bin/bash
# Deploy script — always uses persistent Fly auth
set -euo pipefail

cd "$(dirname "$0")/.."

# Source .env to get FLY_API_TOKEN if set
if [ -f .env ]; then
    export FLY_API_TOKEN=$(grep FLY_API_TOKEN .env | sed 's/^FLY_API_TOKEN=//' | tr -d '"')
fi

# Verify token is available
if [ -z "${FLY_API_TOKEN:-}" ]; then
    echo "Error: FLY_API_TOKEN not found in .env or environment"
    echo "Run: fly auth login"
    exit 1
fi

echo "Deploying to Fly.io..."
flyctl deploy --remote-only --wait-timeout 10m "$@"
