#!/bin/bash
set -e

ISODIR="$1"
# Estrae il PRETTY_NAME da /etc/os-release
PRETTY_NAME=$(grep ^PRETTY_NAME= /etc/os-release | cut -d= -f2 | tr -d '"')
BOOT_PARAMS="$2"
BOOT_COMMON="audit=0 splash quiet loglevel=3 systemd.show_status=auto udev.log_priority=3"

echo "Generazione menu per: $PRETTY_NAME"

if [ -z "$ISODIR" ] || [ -z "$PRETTY_NAME" ]; then
    echo "Errore: Parametri ISODIR o PRETTY_NAME mancanti."
    exit 1
fi

echo "Generazione menu GRUB e ISOLINUX in corso..."

# 1. Generazione GRUB.cfg principale
cat <<EOF > "$ISODIR/boot/grub/grub.cfg"
set timeout=5
set default=2

insmod efi_gop
insmod efi_uga
insmod all_video
insmod gfxterm
insmod png
insmod part_gpt
insmod part_msdos
insmod fat
insmod iso9660

search --no-floppy --set=root --label OA_LIVE

if loadfont /boot/grub/font.pf2; then
    set gfxmode=auto
    terminal_output gfxterm
fi

background_image /boot/grub/splash.png

# ==========================================
# COLORI MENU PRINCIPALE
# ==========================================
set menu_color_normal=white/black
set menu_color_highlight=white/blue

# ==========================================
# COLORI EDITOR E TERMINALE
# ==========================================
set color_normal=white/black
set color_highlight=white/blue

menuentry "--- penguins-eggs (oa edition) ---" {
    true
}
menuentry "" {
    true
}
menuentry "Start $PRETTY_NAME" {
    linux /live/vmlinuz $BOOT_PARAMS $BOOT_COMMON
    initrd /live/initrd.img
}
menuentry "Start $PRETTY_NAME - RAM mode" {
    linux /live/vmlinuz $BOOT_PARAMS $BOOT_COMMON toram
    initrd /live/initrd.img
}
EOF

# 2. Generazione ISOLINUX.cfg
cat <<EOF > "$ISODIR/isolinux/isolinux.cfg"
UI vesamenu.c32
TIMEOUT 50
DEFAULT live

MENU BACKGROUND splash.png
MENU TITLE penguins-eggs (oa edition)

LABEL live
    MENU LABEL Start $PRETTY_NAME
    LINUX /live/vmlinuz
    APPEND $BOOT_PARAMS $BOOT_COMMON
    INITRD /live/initrd.img

LABEL ram
    MENU LABEL Start $PRETTY_NAME - RAM mode
    LINUX /live/vmlinuz
    APPEND $BOOT_PARAMS $BOOT_COMMON toram
    INITRD /live/initrd.img
EOF

# 3. Trampolino EFI (Attenzione agli escape \$ per preservare le variabili per GRUB)
cat <<EOF > "$ISODIR/EFI/BOOT/grub.cfg"
search --set=root --label OA_LIVE
set prefix=(\$root)/boot/grub
configfile \$prefix/grub.cfg
EOF

echo "Menu generati con successo."
