#!/bin/sh
set -eu
(set -o pipefail) 2>/dev/null && set -o pipefail

HIPERISO_ROOT="$(cd "$(dirname "$0")/.." && pwd)"
DL_DIR="$HIPERISO_ROOT/build/downloads"
MODSRC_TARBALL="$HIPERISO_ROOT/vendor/grub2-modsrc.tar.xz"
BUILD_DIR="$HIPERISO_ROOT/build/grub2-204"
SRC_DIR="$BUILD_DIR/SRC/grub-2.04"

rm -rf "$BUILD_DIR"
mkdir -p "$BUILD_DIR/SRC"

tar -xf "$DL_DIR/grub-2.04.tar.xz" -C "$BUILD_DIR/SRC/"

# Overlay Ventoy-derived GRUB2 modifications (vendored, no external dependency)
tar -xf "$MODSRC_TARBALL" -C "$BUILD_DIR/SRC/"

find "$SRC_DIR" -type f \( -name '*.c' -o -name '*.h' -o -name '*.S' \
    -o -name '*.sh' -o -name '*.cfg' -o -name '*.txt' \
    -o -name 'Makefile*' -o -name '*.def' -o -name '*.am' \
    -o -name '*.py' -o -name '*.lst' -o -name '*.rst' \) -exec sed -i \
    -e 's/ventoy\.net/hiperiso.net/g' \
    -e 's/Ventoy\.sh/Hiperiso.sh/g' \
    -e 's/VENTOY/HIPERISO/g' \
    -e 's/Ventoy/Hiperiso/g' \
    -e 's/ventoy2disk/hiperiso2disk/g' \
    -e 's/VENTOY2DISK/HIPERISO2DISK/g' \
    -e 's/ventoyctl/hiperisoctl/g' \
    -e 's/vtoyboot/hisoboot/g' \
    -e 's/vtoy_/hiso_/g' \
    -e 's/VTOY_/HISO_/g' \
    -e 's/vtoy/hiso/g' \
    -e 's/VTOY/HISO/g' \
    -e 's/ventoy/hiperiso/g' \
    -e 's/Ventoy/Hiperiso/g' \
    {} +

find "$SRC_DIR" -depth -type d \( -name '*ventoy*' -o -name '*Ventoy*' \) | while read d; do
    newd=$(echo "$d" | sed -e 's/ventoy/hiperiso/g' -e 's/Ventoy/Hiperiso/g')
    mv "$d" "$newd"
done

find "$SRC_DIR" -type f \( -name '*ventoy*' -o -name '*Ventoy*' \) | while read f; do
    newf=$(echo "$f" | sed -e 's/ventoy/hiperiso/g' -e 's/Ventoy/Hiperiso/g')
    mv "$f" "$newf"
done

cp "$HIPERISO_ROOT/src/grub2/hiperiso_cmd.c" \
   "$SRC_DIR/grub-core/hiperiso/hiperiso_cmd.c"
cp "$HIPERISO_ROOT/src/grub2/grub/grub.cfg" "$SRC_DIR/"

cd "$SRC_DIR"
make distclean 2>/dev/null || true
./autogen.sh
./configure --with-platform=efi --target=x86_64 \
    --prefix="$BUILD_DIR/INSTALL/" --disable-werror \
    CFLAGS="-std=gnu99 -Wno-error" HOST_CFLAGS="-std=gnu99 -Wno-error"
make -j"$(nproc)" 2>&1 | tail -40

echo ">>> Installing GRUB tools and modules..."
make install 2>&1 | tail -5

INSTALL_DIR="$BUILD_DIR/INSTALL"
GRUB_LIB="$INSTALL_DIR/lib/grub/x86_64-efi"
EFI_OUTPUT="$HIPERISO_ROOT/grub2/bin/BOOTX64.EFI"
mkdir -p "$(dirname "$EFI_OUTPUT")"

echo ">>> Building BOOTX64.EFI with hiperiso module..."

MODULES="file setkey blocklist hiperiso test true regexp newc search \
at_keyboard usb_keyboard gcry_md5 hashsum gzio xzio lzopio \
ext2 xfs read halt sleep serial terminfo png password_pbkdf2 \
gcry_sha512 pbkdf2 part_gpt part_msdos ls tar squash4 loopback \
part_apple minicmd diskfilter linux relocator jpeg iso9660 udf \
hfsplus halt acpi mmap gfxmenu video_colors trig bitmap_scale \
gfxterm bitmap font fat exfat ntfs fshelp efifwsetup reboot echo \
configfile normal terminal gettext chain priority_queue bufio \
datetime cat extcmd crypto boot all_video efi_gop efi_uga \
video_bochs video_cirrus video video_fb gfxterm_background \
gfxterm_menu mouse smbios"

PATH="$INSTALL_DIR/bin:$INSTALL_DIR/sbin:$PATH" \
grub-mkimage \
    --directory "$GRUB_LIB" \
    --prefix '(,2)/grub' \
    --output "$EFI_OUTPUT" \
    --format 'x86_64-efi' \
    --compression 'auto' \
    $MODULES

echo ">>> Built: $EFI_OUTPUT ($(du -h "$EFI_OUTPUT" | cut -f1))"
echo ">>> Verifying module integrity..."
_VT=$(strings "$EFI_OUTPUT" | grep -c 'vt_' || true)
_HISO=$(strings "$EFI_OUTPUT" | grep -c 'hiperiso_' || true)
echo "    vt_* symbols: $_VT"
echo "    hiperiso_* symbols: $_HISO"
