#!/bin/bash
# SSH Commands for Proxmox Nodes
# Provides easy access to both Proxmox machines

set -e

# Load password from .env file
# Get the directory where this script is located
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Get the project root (one level up from scripts directory)
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
# Locate .env file in project root
ENV_FILE="$PROJECT_ROOT/.env"

if [[ -f "$ENV_FILE" ]]; then
    # Source the password from .env (lines 45-46)
    export PROXMOX_PASSWORD=$(sed -n '45,46p' "$ENV_FILE" | grep -E "PROXMOX_ROOT_PASS|^[^#]" | head -1 | cut -d'=' -f2 | tr -d '"' | tr -d "'")
else
    echo "Warning: .env file not found. Password will need to be entered manually."
    PROXMOX_PASSWORD=""
fi

# Colors
GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[1;33m'
NC='\033[0m'

# Configuration
ML110_01_IP="192.168.11.10"
R630_01_IP="192.168.11.11"
ML110_01_HOSTNAME="ml110-01"
R630_01_HOSTNAME="r630-01"
DEFAULT_USER="root"

echo "=========================================="
echo "Proxmox Node SSH Access"
echo "=========================================="
echo ""

# Function to display SSH command
show_ssh_command() {
    local node_name=$1
    local ip=$2
    local hostname=$3
    local user=$4
    
    echo -e "${BLUE}${node_name}${NC}"
    echo "  IP Address: $ip"
    echo "  Hostname: $hostname"
    echo "  User: $user"
    echo ""
    echo "  SSH Command:"
    echo -e "  ${GREEN}ssh ${user}@${ip}${NC}"
    echo ""
    if [[ -n "$hostname" ]]; then
        echo "  Or via hostname (if DNS configured):"
        echo -e "  ${GREEN}ssh ${user}@${hostname}${NC}"
    fi
    echo ""
}

# Display commands for both nodes
show_ssh_command "Site-1 (ML110-01)" "$ML110_01_IP" "$ML110_01_HOSTNAME" "$DEFAULT_USER"
show_ssh_command "Site-2 (R630-01)" "$R630_01_IP" "$R630_01_HOSTNAME" "$DEFAULT_USER"

echo "=========================================="
echo "Quick Access Commands"
echo "=========================================="
echo ""
echo "SSH to ML110-01:"
if command -v sshpass &> /dev/null && [[ -n "$PROXMOX_PASSWORD" ]]; then
    echo -e "  ${GREEN}sshpass -p '***' ssh root@${ML110_01_IP}${NC}"
    echo "  (Password loaded from .env)"
else
    echo -e "  ${GREEN}ssh root@${ML110_01_IP}${NC}"
fi
echo ""
echo "SSH to R630-01:"
if command -v sshpass &> /dev/null && [[ -n "$PROXMOX_PASSWORD" ]]; then
    echo -e "  ${GREEN}sshpass -p '***' ssh root@${R630_01_IP}${NC}"
    echo "  (Password loaded from .env)"
else
    echo -e "  ${GREEN}ssh root@${R630_01_IP}${NC}"
fi
echo ""

echo "=========================================="
echo "Verification Commands"
echo "=========================================="
echo ""
echo "Test connectivity to ML110-01:"
echo -e "  ${GREEN}ping -c 3 ${ML110_01_IP}${NC}"
echo ""
echo "Test connectivity to R630-01:"
echo -e "  ${GREEN}ping -c 3 ${R630_01_IP}${NC}"
echo ""
echo "Test SSH to ML110-01:"
if command -v sshpass &> /dev/null && [[ -n "$PROXMOX_PASSWORD" ]]; then
    echo -e "  ${GREEN}sshpass -p '***' ssh -o ConnectTimeout=5 root@${ML110_01_IP} 'hostname'${NC}"
else
    echo -e "  ${GREEN}ssh -o ConnectTimeout=5 root@${ML110_01_IP} 'hostname'${NC}"
fi
echo ""
echo "Test SSH to R630-01:"
if command -v sshpass &> /dev/null && [[ -n "$PROXMOX_PASSWORD" ]]; then
    echo -e "  ${GREEN}sshpass -p '***' ssh -o ConnectTimeout=5 root@${R630_01_IP} 'hostname'${NC}"
else
    echo -e "  ${GREEN}ssh -o ConnectTimeout=5 root@${R630_01_IP} 'hostname'${NC}"
fi
echo ""

echo "=========================================="
echo "Proxmox API Verification"
echo "=========================================="
echo ""
echo "Check Proxmox API on ML110-01:"
echo -e "  ${GREEN}curl -k https://${ML110_01_IP}:8006/api2/json/version${NC}"
echo ""
echo "Check Proxmox API on R630-01:"
echo -e "  ${GREEN}curl -k https://${R630_01_IP}:8006/api2/json/version${NC}"
echo ""

echo "=========================================="
echo "Useful Commands After SSH"
echo "=========================================="
echo ""
echo "Check Proxmox status:"
echo "  pveversion"
echo "  systemctl status pve-cluster"
echo ""
echo "List VMs:"
echo "  qm list"
echo ""
echo "Check storage:"
echo "  pvesm status"
echo ""
echo "Check network:"
echo "  ip addr show"
echo "  cat /etc/network/interfaces"
echo ""
echo "Check Ceph (if configured):"
echo "  ceph status"
echo "  ceph df"
echo ""

