#!/bin/bash
# SSH to R630-01 using password from .env

set -e

# 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"

# Verify .env file exists
if [[ ! -f "$ENV_FILE" ]]; then
    echo "Error: .env file not found at $ENV_FILE"
    echo "Current directory: $(pwd)"
    echo "Script directory: $SCRIPT_DIR"
    echo "Project root: $PROJECT_ROOT"
    exit 1
fi

if [[ -f "$ENV_FILE" ]]; then
    # Extract password from .env (lines 45-46)
    PROXMOX_PASSWORD=$(sed -n '45,46p' "$ENV_FILE" | grep "PROXMOX_ROOT_PASS" | head -1 | cut -d'=' -f2- | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
    # Remove quotes if present
    PROXMOX_PASSWORD="${PROXMOX_PASSWORD#\"}"
    PROXMOX_PASSWORD="${PROXMOX_PASSWORD%\"}"
    PROXMOX_PASSWORD="${PROXMOX_PASSWORD#\'}"
    PROXMOX_PASSWORD="${PROXMOX_PASSWORD%\'}"
else
    echo "Error: .env file not found"
    exit 1
fi

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

echo "Connecting to R630-01 (192.168.11.11)..."
sshpass -p "$PROXMOX_PASSWORD" ssh -o StrictHostKeyChecking=no root@192.168.11.11 "$@"

