#!/bin/bash
# Deploy art-dag GPU code to a remote droplet
# Usage: ./deploy-to-gpu.sh <droplet-ip>

set -e

if [ -z "$1" ]; then
    echo "Usage: $0 <droplet-ip>"
    echo "Example: $0 159.223.7.100"
    exit 1
fi

DROPLET_IP="$1"
REMOTE_DIR="/opt/artdag-gpu/celery"
LOCAL_DIR="$(dirname "$0")/.."

echo "=== Deploying to $DROPLET_IP ==="

# Create remote directory
echo "[1/4] Creating remote directory..."
ssh "root@$DROPLET_IP" "mkdir -p $REMOTE_DIR/sexp_effects $REMOTE_DIR/streaming $REMOTE_DIR/scripts"

# Copy core files
echo "[2/4] Copying core files..."
scp "$LOCAL_DIR/sexp_effects/wgsl_compiler.py" "root@$DROPLET_IP:$REMOTE_DIR/sexp_effects/"
scp "$LOCAL_DIR/sexp_effects/parser.py" "root@$DROPLET_IP:$REMOTE_DIR/sexp_effects/"
scp "$LOCAL_DIR/sexp_effects/interpreter.py" "root@$DROPLET_IP:$REMOTE_DIR/sexp_effects/"
scp "$LOCAL_DIR/sexp_effects/__init__.py" "root@$DROPLET_IP:$REMOTE_DIR/sexp_effects/"
scp "$LOCAL_DIR/streaming/backends.py" "root@$DROPLET_IP:$REMOTE_DIR/streaming/"

# Copy effects
echo "[3/4] Copying effects..."
ssh "root@$DROPLET_IP" "mkdir -p $REMOTE_DIR/sexp_effects/effects $REMOTE_DIR/sexp_effects/primitive_libs"
scp -r "$LOCAL_DIR/sexp_effects/effects/"*.sexp "root@$DROPLET_IP:$REMOTE_DIR/sexp_effects/effects/" 2>/dev/null || true
scp -r "$LOCAL_DIR/sexp_effects/primitive_libs/"*.py "root@$DROPLET_IP:$REMOTE_DIR/sexp_effects/primitive_libs/" 2>/dev/null || true

# Test
echo "[4/4] Testing deployment..."
ssh "root@$DROPLET_IP" "cd $REMOTE_DIR && /opt/artdag-gpu/bin/python3 -c '
import sys
sys.path.insert(0, \".\")
from sexp_effects.wgsl_compiler import compile_effect_file
result = compile_effect_file(\"sexp_effects/effects/invert.sexp\")
print(f\"Compiled effect: {result.name}\")
print(\"Deployment OK\")
'" || echo "Test failed - may need to run setup script first"

echo ""
echo "=== Deployment complete ==="
echo "SSH: ssh root@$DROPLET_IP"
echo "Test: ssh root@$DROPLET_IP 'cd $REMOTE_DIR && /opt/artdag-gpu/bin/python3 -c \"from streaming.backends import get_backend; b=get_backend(\\\"wgpu\\\"); print(b)\"'"
