#!/usr/bin/fish

# 1. CLEANUP
rm -rf build
mkdir -p build/iso_root/boot/limine

# 2. COMPILE KERNEL
gcc -ffreestanding -mcmodel=kernel -fno-stack-protector -fno-pic -m64 -Isrc/include -c src/kernel/kernel.c -o build/kernel.o

# 3. LINK KERNEL
ld -nostdlib -static -m elf_x86_64 -z max-page-size=0x1000 -T src/kernel/linker.ld build/kernel.o -o build/kernel.elf

# 4. PREP ISO STRUCTURE (The "Fix")
cp build/kernel.elf build/iso_root/boot/
cp src/assets/terminal.fpf build/iso_root/boot/ # Make sure path to fpf is correct!
cp limine/limine-bios.sys build/iso_root/boot/limine/
cp limine/limine-bios-cd.bin build/iso_root/boot/limine/
cp limine/limine.conf build/iso_root/boot/limine/

# 5. BUILD ISO
xorriso -as mkisofs -b boot/limine/limine-bios-cd.bin \
        -no-emul-boot -boot-load-size 4 -boot-info-table \
        build/iso_root -o build/florid_os.iso

# 6. THE BLESSING
./limine-binary/limine bios-install build/florid_os.iso