#!/usr/bin/env bash
set -e

echo "=== Arch VM Auto Installer ==="

# -------------------------------------
# Disk detection
# -------------------------------------
echo "[*] Detecting install disk..."

for D in /dev/sda /dev/vda /dev/nvme0n1; do
    if [[ -b "$D" ]]; then
        DISK="$D"
        break
    fi
done

if [[ -z "$DISK" ]]; then
    echo "No valid disk found! Expected sda, vda, or nvme0n1."
    exit 1
fi

echo "[*] Using disk: $DISK"
sleep 1

# -------------------------------------
# Partitioning
# -------------------------------------
echo "[*] Wiping and partitioning disk..."
wipefs -af "$DISK"
sgdisk --zap-all "$DISK"

sgdisk -n 1:0:+1M   -t 1:ef02 -c 1:"BIOSBOOT" "$DISK"
sgdisk -n 2:0:+512M -t 2:ef00 -c 2:"EFI"      "$DISK"
sgdisk -n 3:0:0     -t 3:8300 -c 3:"ROOT"     "$DISK"

if [[ "$DISK" == *"nvme"* ]]; then
    BIOSBOOT="${DISK}p1"
    EFI="${DISK}p2"
    ROOT="${DISK}p3"
else
    BIOSBOOT="${DISK}1"
    EFI="${DISK}2"
    ROOT="${DISK}3"
fi

# -------------------------------------
# Filesystems
# -------------------------------------
echo "[*] Creating filesystems..."
mkfs.fat -F32 "$EFI"
mkfs.ext4 -F "$ROOT"

# -------------------------------------
# Mounting
# -------------------------------------
echo "[*] Mounting partitions..."
mount "$ROOT" /mnt
mkdir -p /mnt/boot/efi
mount "$EFI" /mnt/boot/efi

# -------------------------------------
# Base Install
# -------------------------------------
echo "[*] Installing base system..."
pacstrap /mnt base linux linux-firmware grub efibootmgr networkmanager openssh

# -------------------------------------
# System config
# -------------------------------------
echo "[*] Generating fstab..."
genfstab -U /mnt > /mnt/etc/fstab

echo "[*] Setting root password..."
arch-chroot /mnt bash -c "echo 'root:root' | chpasswd"

# Enable NetworkManager and SSH
echo "[*] Enabling NetworkManager & SSH..."
arch-chroot /mnt systemctl enable NetworkManager
arch-chroot /mnt systemctl enable sshd

# -------------------------------------
# Enable root SSH password login
# -------------------------------------
echo "[*] Enabling root SSH login with password..."

arch-chroot /mnt bash -c "
sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config
sed -i 's/#PermitRootLogin yes/PermitRootLogin yes/' /etc/ssh/sshd_config
sed -i 's/#PasswordAuthentication yes/PasswordAuthentication yes/' /etc/ssh/sshd_config
sed -i 's/PasswordAuthentication no/PasswordAuthentication yes/' /etc/ssh/sshd_config
systemctl restart sshd
"

# -------------------------------------
# Automatic system update
# -------------------------------------
echo "[*] Running full system update..."
arch-chroot /mnt pacman -Syu --noconfirm

# -------------------------------------
# Bootloader (Automatic mode detection)
# -------------------------------------
echo "[*] Detecting firmware mode..."

if [[ -d /sys/firmware/efi ]]; then
    echo "[*] UEFI mode detected — installing GRUB EFI..."

    arch-chroot /mnt grub-install \
        --target=x86_64-efi \
        --efi-directory=/boot/efi \
        --bootloader-id=GRUB

else
    echo "[*] BIOS/Legacy mode detected — installing GRUB MBR..."

    arch-chroot /mnt grub-install \
        --target=i386-pc \
        "$DISK"
fi

echo "[*] Generating GRUB configuration..."
arch-chroot /mnt grub-mkconfig -o /boot/grub/grub.cfg

echo "=== Installation complete. You may reboot. ==="
