#!/bin/bash
# Copy complete-vm-100-guest-agent-check.sh to both Proxmox nodes
# Uses password from .env file

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"

# Source .env file
if [ -f "$PROJECT_ROOT/.env" ]; then
    set -a
    source "$PROJECT_ROOT/.env"
    set +a
fi

# Get password (with fallback)
PROXMOX_PASS="${PROXMOX_ROOT_PASS:-L@kers2010}"

# Use exact password format provided by user
PROXMOX_PASS="L@kers2010"

# Get hostnames/IPs (with defaults from other scripts)
PROXMOX_1_HOST="${PROXMOX_1_HOST:-192.168.11.10}"
PROXMOX_2_HOST="${PROXMOX_2_HOST:-192.168.11.11}"

# Also try hostnames if IPs don't work
PROXMOX_1_HOSTNAME="${PROXMOX_1_HOSTNAME:-ml110-01}"
PROXMOX_2_HOSTNAME="${PROXMOX_2_HOSTNAME:-r630-01}"

SCRIPT_NAME="complete-vm-100-guest-agent-check.sh"
SCRIPT_PATH="$SCRIPT_DIR/$SCRIPT_NAME"
REMOTE_PATH="/usr/local/bin/$SCRIPT_NAME"

echo "=========================================="
echo "Copying Script to Proxmox Nodes"
echo "=========================================="
echo ""
echo "Script: $SCRIPT_NAME"
echo "Source: $SCRIPT_PATH"
echo "Target: $REMOTE_PATH"
echo ""

# Check if script exists
if [ ! -f "$SCRIPT_PATH" ]; then
    echo "❌ Error: Script not found: $SCRIPT_PATH"
    exit 1
fi

# Check if sshpass is available
if ! command -v sshpass &> /dev/null; then
    echo "❌ Error: sshpass is not installed"
    echo "Install it with: sudo apt-get install sshpass"
    exit 1
fi

# Function to copy to a node
copy_to_node() {
    local host=$1
    local node_name=$2
    
    echo "--------------------------------------"
    echo "Copying to $node_name ($host)..."
    echo "--------------------------------------"
    
    # Test connection first
    if ! sshpass -p "$PROXMOX_PASS" ssh -o StrictHostKeyChecking=no -o ConnectTimeout=5 root@$host "echo 'Connected'" > /dev/null 2>&1; then
        echo "❌ Failed to connect to $host"
        return 1
    fi
    
    # Copy script
    if sshpass -p "$PROXMOX_PASS" scp -o StrictHostKeyChecking=no "$SCRIPT_PATH" root@$host:"$REMOTE_PATH"; then
        echo "✅ Script copied successfully"
        
        # Make executable
        if sshpass -p "$PROXMOX_PASS" ssh -o StrictHostKeyChecking=no root@$host "chmod +x $REMOTE_PATH"; then
            echo "✅ Script made executable"
            
            # Verify
            if sshpass -p "$PROXMOX_PASS" ssh -o StrictHostKeyChecking=no root@$host "test -x $REMOTE_PATH"; then
                echo "✅ Script verified as executable"
                echo ""
                echo "You can now run on $node_name:"
                echo "  $REMOTE_PATH"
                echo ""
                return 0
            else
                echo "⚠️  Warning: Script may not be executable"
                return 1
            fi
        else
            echo "❌ Failed to make script executable"
            return 1
        fi
    else
        echo "❌ Failed to copy script"
        return 1
    fi
}

# Copy to both nodes
SUCCESS_COUNT=0

# Try IP first, then hostname
if copy_to_node "$PROXMOX_1_HOST" "ml110-01 (Site 1)"; then
    ((SUCCESS_COUNT++))
elif copy_to_node "$PROXMOX_1_HOSTNAME" "ml110-01 (Site 1 - hostname)"; then
    ((SUCCESS_COUNT++))
fi

if copy_to_node "$PROXMOX_2_HOST" "r630-01 (Site 2)"; then
    ((SUCCESS_COUNT++))
elif copy_to_node "$PROXMOX_2_HOSTNAME" "r630-01 (Site 2 - hostname)"; then
    ((SUCCESS_COUNT++))
fi

echo "=========================================="
echo "Summary"
echo "=========================================="
echo "✅ Successfully copied to $SUCCESS_COUNT/2 nodes"
echo ""

if [ $SUCCESS_COUNT -eq 2 ]; then
    echo "✅ All nodes updated successfully!"
    echo ""
    echo "To run the script on ml110-01:"
    echo "  ssh root@$PROXMOX_1_HOST"
    echo "  $REMOTE_PATH"
    echo ""
    echo "To run the script on r630-01:"
    echo "  ssh root@$PROXMOX_2_HOST"
    echo "  $REMOTE_PATH"
    exit 0
else
    echo "⚠️  Some nodes failed. Check the output above."
    exit 1
fi

