
env_scripts = [
    '''
    #!@duckscript
    if eq ${TARGET} "x86_64-unknown-none"
        set_env ARCH "x86-64"
        set_env QEMUCOMMAND "qemu-system-x86_64"
        set_env LIMINEBIN "BOOTX64.EFI"
    else if eq ${TARGET} "aarch64-unknown-none"
        set_env ARCH "aarch64"
        set_env QEMUCOMMAND "qemu-system-aarch64"
        set_env LIMINEBIN "BOOTAA64.EFI"
    else if eq ${TARGET} "riscv64gc-unknown-none-elf"
        set_env ARCH "riscv64"
        set_env QEMUCOMMAND "qemu-system-riscv64"
        set_env LIMINEBIN "BOOTRISCV64.EFI"
    else if eq ${TARGET} "loongarch64-unknown-none"
        set_env ARCH "loongarch64"
        set_env QEMUCOMMAND "qemu-system-loongarch64"
        set_env LIMINEBIN "BOOTLOONGARCH64.EFI"
    end
    '''
]

[config]
default_to_workspace = false

[env]
# Directory for Limine binaries
LIMINEDIR = "/usr/share/limine"
# Compile target
TARGET = "x86_64-unknown-none"
# Binaries included in the OS initramfs
BINS = []

[env.development]
PROFILE = "dev"
PROFILEDIR = "debug"
ARTIFACTDIR = "target/${TARGET}/${PROFILEDIR}"

[env.dev]
PROFILE = "dev"
PROFILEDIR = "debug"
ARTIFACTDIR = "target/${TARGET}/${PROFILEDIR}"

[env.debug]
PROFILE = "dev"
PROFILEDIR = "debug"
ARTIFACTDIR = "target/${TARGET}/${PROFILEDIR}"

[env.release]
PROFILE = "release"
PROFILEDIR = "release"
ARTIFACTDIR = "target/${TARGET}/${PROFILEDIR}"

[tasks.default]
alias = "run"





[tasks.clean_all]
run_task = { name = ["clean_kernel", "clean_initramfs", "clean_iso"] }

[tasks.clean_kernel]
command = "cargo"
args = ["clean"]

[tasks.prepare]
script = "rustup install nightly; rustup target add ${TARGET}"

[tasks.clean_iso]
command = "rm"
args = ["-rf", "build/iso", "build/gila.iso"]

[tasks.clean_initramfs]
command = "rm"
args = ["-rf", "build/initramfs", "build/initramfs.tar.lzma"]

[tasks.lib]
condition = { files_modified = { input = ["libgila/src/**/*.rs"], output = ["${ARTIFACTDIR}/libgila.rlib"] }, fail_message = "(inputs unchanged)" }
dependencies = []
command = "cargo"
args = ["build", "--profile", "${PROFILE}", "-p", "libgila"]

[tasks.kernel]
condition = { files_modified = { input = [
    "kernel/src/**/*.rs", 
    "kernel/src/**/*.ld", 
    "${ARTIFACTDIR}/libgila.rlib"
], output = ["${ARTIFACTDIR}/kernel"] }, fail_message = "(inputs unchanged)" }
dependencies = ["lib"]
command = "cargo"
args = ["build", "--profile", "${PROFILE}", "-p", "kernel"]

[tasks.bins]
dependencies = []





[tasks.initramfs]
condition = { files_modified = { input = [
    # This will not depend on *every* binary we build.
    # If we mount a disk later in boot we can store other binaries there.
    "configs/server_config.toml"
], output = ["build/${ARCH}/initramfs.tar.lzma"] }, fail_message = "(inputs unchanged)" }
dependencies = ["bins"]
script = '''
    mkdir -p build/initramfs/system/bin
    mkdir -p build/initramfs/system/cfg
    mkdir -p build/${ARCH}

    for bin in $(echo ${BINS} | tr ";" " ")
    do
        cp ${ARTIFACTDIR}/$bin build/initramfs/system/bin/
    done
    cp configs/server_config.toml build/initramfs/system/cfg/

    tar -c --lzma -f build/${ARCH}/initramfs.tar.lzma build/initramfs
'''

[tasks.iso]
condition = { files_modified = { input = [
    "${ARTIFACTDIR}/kernel", 
    "configs/limine.conf",
    "${LIMINEDIR}/limine-bios.sys",
    "${LIMINEDIR}/limine-bios-cd.bin",
    "${LIMINEDIR}/limine-uefi-cd.bin",
    "${LIMINEDIR}/${LIMINEBIN}"
], output = ["build/gila.iso"] }, fail_message = "(inputs unchanged)" }
dependencies = ["kernel", "initramfs"]
script = '''
    mkdir -p build/iso/boot/limine
	mkdir -p build/iso/EFI/BOOT
    mkdir -p build/iso/boot/${ARCH}

	cp -f configs/limine.conf build/iso/
	cp -f ${LIMINEDIR}/limine-bios.sys build/iso/boot/limine/
	cp -f ${LIMINEDIR}/limine-bios-cd.bin build/iso/boot/limine/
	cp -f ${LIMINEDIR}/limine-uefi-cd.bin build/iso/boot/limine/
	cp -f ${LIMINEDIR}/${LIMINEBIN} build/iso/EFI/BOOT
    cp -f build/${ARCH}/initramfs.tar.lzma build/iso/boot/${ARCH}

	cp -f ${ARTIFACTDIR}/kernel build/iso/boot/${ARCH}/gila

	mkdir -p build/iso/system/bin
	mkdir -p build/iso/system/tmp
	mkdir -p build/iso/system/cfg

	xorriso -as mkisofs \
    -b boot/limine/limine-bios-cd.bin \
    -no-emul-boot \
    -boot-load-size 4 \
    -boot-info-table \
    --efi-boot boot/limine/limine-uefi-cd.bin \
    -efi-boot-part \
    --efi-boot-image \
    --protective-msdos-label build/iso \
    -o build/gila.iso

    limine bios-install build/gila.iso
'''

[tasks.run]
dependencies = ["iso"]
command = "${QEMUCOMMAND}"
args = ["-display", "none", "-drive", "file=build/gila.iso,format=raw,index=0,media=disk", "-serial", "stdio"]

[tasks.debug_run]
dependencies = ["iso"]
command = "${QEMUCOMMAND}"
args = ["-s", "-S", "-display", "none", "-drive", "file=build/gila.iso,format=raw,index=0,media=disk", "-serial", "stdio"]

[tasks.debug_view]
dependencies = ["iso"]
command = "gdb"
args = ["${ARTIFACTDIR}/kernel", "--eval-command=target remote localhost:1234"]

[tasks.debug]
run_task = { name = ["debug_run", "debug_view"], fork = true, parallel = true }
