#!/bin/bash
# install-solaris.sh
# Run this from inside the Hetzner RESCUE system (not the custom-ISO boot mode).
# It downloads the Solaris 11.4 ISO, installs QEMU if needed, and boots a nested
# QEMU VM with /dev/sda passed through as a raw virtio-blk disk, so the Solaris
# installer sees a real disk regardless of Hetzner's custom-ISO boot limitations.
#
# Usage:
#   chmod +x install-solaris.sh
#   ./install-solaris.sh
#
# Connect afterwards via VNC on port 5901 (tunnel over SSH):
#   ssh -L 5901:localhost:5901 root@<rescue-ip>
# then point a VNC client at localhost:5901

set -euo pipefail

ISO_URL="https://files.mstechholdings.com/ISOs/sol-11_4-text-x86.iso"
ISO_PATH="/root/sol-11_4-text-x86.iso"
DISK="/dev/sda"
RAM_MB=4096
CPUS=2
VNC_DISPLAY=":1"

echo "=== Step 1: Sanity checks ==="

if [ "$(id -u)" -ne 0 ]; then
    echo "ERROR: must be run as root."
    exit 1
fi

if [ ! -b "$DISK" ]; then
    echo "ERROR: $DISK not found. Available block devices:"
    lsblk
    exit 1
fi

echo "Target disk confirmed: $DISK"
lsblk "$DISK"

echo
echo "=== Step 2: Check for KVM acceleration ==="
KVM_FLAG=""
if [ -e /dev/kvm ]; then
    echo "KVM acceleration available."
    KVM_FLAG="-enable-kvm -cpu host"
else
    echo "WARNING: /dev/kvm not found. Falling back to software emulation (slower)."
    KVM_FLAG="-cpu qemu64"
fi

echo
echo "=== Step 3: Install QEMU if missing ==="
if ! command -v qemu-system-x86_64 >/dev/null 2>&1; then
    echo "qemu-system-x86_64 not found, installing..."
    if command -v apt-get >/dev/null 2>&1; then
        apt-get update
        apt-get install -y qemu-system-x86 qemu-utils
    elif command -v yum >/dev/null 2>&1; then
        yum install -y qemu-kvm qemu-img
    else
        echo "ERROR: no supported package manager found (apt-get/yum). Install qemu manually."
        exit 1
    fi
else
    echo "qemu-system-x86_64 already present."
fi

echo
echo "=== Step 4: Download Solaris 11.4 ISO ==="
if [ -f "$ISO_PATH" ]; then
    echo "ISO already present at $ISO_PATH, checking size..."
    ls -lh "$ISO_PATH"
    echo "Skipping re-download. Delete $ISO_PATH first if you want to force a fresh download."
else
    echo "Downloading from: $ISO_URL"
    wget -O "$ISO_PATH" "$ISO_URL"
fi

if [ ! -s "$ISO_PATH" ]; then
    echo "ERROR: ISO download failed or file is empty."
    exit 1
fi

echo
echo "=== Step 5: Launch QEMU ==="
echo "Disk:        $DISK (raw passthrough, virtio-blk)"
echo "ISO:         $ISO_PATH"
echo "RAM:         ${RAM_MB}MB"
echo "CPUs:        $CPUS"
echo "VNC display: $VNC_DISPLAY  (connect to 127.0.0.1:5901 via SSH tunnel)"
echo
echo "Starting in 5 seconds... Ctrl+C to abort."
sleep 5

qemu-system-x86_64 \
    $KVM_FLAG \
    -m "$RAM_MB" \
    -smp "$CPUS" \
    -drive file="$DISK",if=none,id=disk0,format=raw \
    -device virtio-blk-pci,id=blk0,drive=disk0 \
    -cdrom "$ISO_PATH" \
    -boot d \
    -vnc "$VNC_DISPLAY" \
    -name "solaris-install"

echo
echo "=== QEMU session ended ==="
echo "If the install completed, exit rescue mode and reboot the server normally."
