#!/usr/bin/env bash
# Quick script to copy deployment scripts to Proxmox host
# Usage: ./scripts/copy-scripts-to-proxmox.sh [host] [user]

# Suppress locale warnings
export LC_ALL=C
export LANG=C

HOST="${1:-192.168.11.10}"
USER="${2:-root}"
REMOTE_DIR="/opt/smom-dbis-138-proxmox"

echo "Copying scripts to $USER@$HOST:$REMOTE_DIR"

# Test connection (suppress locale warnings)
if ! ssh -o BatchMode=yes -o ConnectTimeout=5 "$USER@$HOST" "export LC_ALL=C; export LANG=C; exit" 2>/dev/null; then
    echo "❌ Cannot connect to $HOST"
    echo "   Run: ssh $USER@$HOST"
    exit 1
fi

# Create directory structure (suppress locale warnings)
ssh "$USER@$HOST" "export LC_ALL=C; export LANG=C; mkdir -p $REMOTE_DIR/{scripts/{deployment,validation,network},config,lib,install}" 2>/dev/null

# Copy deployment scripts
echo "Copying deployment scripts..."
scp smom-dbis-138-proxmox/scripts/deployment/deploy-phased.sh "$USER@$HOST:$REMOTE_DIR/scripts/deployment/" 2>/dev/null || true
scp smom-dbis-138-proxmox/scripts/deployment/pre-cache-os-template.sh "$USER@$HOST:$REMOTE_DIR/scripts/deployment/" 2>/dev/null || true
scp smom-dbis-138-proxmox/scripts/deployment/deploy-besu-nodes.sh "$USER@$HOST:$REMOTE_DIR/scripts/deployment/" 2>/dev/null || true
scp smom-dbis-138-proxmox/scripts/deployment/deploy-ccip-nodes.sh "$USER@$HOST:$REMOTE_DIR/scripts/deployment/" 2>/dev/null || true

# Copy library files (needed by deployment scripts)
echo "Copying library files..."
if [[ -d "smom-dbis-138-proxmox/lib" ]]; then
    scp smom-dbis-138-proxmox/lib/*.sh "$USER@$HOST:$REMOTE_DIR/lib/" 2>/dev/null || true
fi

# Copy configuration files
echo "Copying configuration files..."
scp smom-dbis-138-proxmox/config/proxmox.conf "$USER@$HOST:$REMOTE_DIR/config/" 2>/dev/null || true

# Copy validation scripts (optional but useful)
echo "Copying validation scripts..."
scp smom-dbis-138-proxmox/scripts/validation/check-prerequisites.sh "$USER@$HOST:$REMOTE_DIR/scripts/validation/" 2>/dev/null || true

# Make all scripts executable (suppress locale warnings)
ssh "$USER@$HOST" "export LC_ALL=C; export LANG=C; chmod +x $REMOTE_DIR/scripts/**/*.sh 2>/dev/null; chmod +x $REMOTE_DIR/lib/*.sh 2>/dev/null; true" 2>/dev/null

echo "✅ Scripts copied to $REMOTE_DIR/scripts/deployment/"
echo ""
echo "Run on Proxmox host:"
echo "  ssh $USER@$HOST"
echo "  cd $REMOTE_DIR"
echo "  sudo ./scripts/deployment/deploy-phased.sh --source-project /path/to/smom-dbis-138"

