#!/bin/bash
# SSH to Proxmox Server

set -e

# Default values
USERNAME="root"
PORT="22"
IP_ADDRESS=""

# Colors
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# Function to display usage
usage() {
    echo "Usage: $0 <IP_ADDRESS> [OPTIONS]"
    echo ""
    echo "Options:"
    echo "  -u, --user USERNAME    SSH username (default: root)"
    echo "  -p, --port PORT        SSH port (default: 22)"
    echo "  -h, --help             Show this help message"
    echo ""
    echo "Examples:"
    echo "  $0 192.168.11.10"
    echo "  $0 192.168.11.10 -u root -p 22"
    echo "  $0 10.1.0.10"
    echo ""
    echo "Known Proxmox IPs from config:"
    echo "  • Site 1: 192.168.11.10, 10.1.0.10"
    echo "  • Site 2: 192.168.11.11, 10.2.0.10"
    exit 1
}

# Parse arguments
if [ $# -eq 0 ]; then
    usage
fi

IP_ADDRESS=$1
shift

while [[ $# -gt 0 ]]; do
    case $1 in
        -u|--user)
            USERNAME="$2"
            shift 2
            ;;
        -p|--port)
            PORT="$2"
            shift 2
            ;;
        -h|--help)
            usage
            ;;
        *)
            echo -e "${RED}Unknown option: $1${NC}"
            usage
            ;;
    esac
done

# Validate IP address
if [ -z "$IP_ADDRESS" ]; then
    echo -e "${RED}Error: IP address is required${NC}"
    usage
fi

# Test connectivity
echo -e "${YELLOW}Testing connectivity to $IP_ADDRESS...${NC}"
if ping -c 1 -W 2 "$IP_ADDRESS" &>/dev/null; then
    echo -e "${GREEN}✓ Host is reachable${NC}"
else
    echo -e "${YELLOW}⚠ Host may not be reachable (ping failed, but continuing...)${NC}"
fi

# SSH connection
echo -e "${YELLOW}Connecting to Proxmox at $IP_ADDRESS...${NC}"
echo -e "${GREEN}SSH Command: ssh -p $PORT $USERNAME@$IP_ADDRESS${NC}"
echo ""

ssh -p "$PORT" "$USERNAME@$IP_ADDRESS"

