SHELL := /usr/bin/env bash

export ARCH ?= arm64
export KERNEL_VERSION ?= 7.1
export BUSYBOX_VERSION ?= 1.36.1
export SYSTEMD_VERSION ?= 256
export NIX_VERSION ?= 2.34.0
export NIX_SYSTEM ?= aarch64-linux
export DESKTOP ?= console
export ROOT_LABEL ?= FORGE_ROOT
export EFI_LABEL ?= FORGE_EFI
export IMAGE_SIZE_MIB ?= 4096
export ESP_SIZE_MIB ?= 256
export JOBS ?= $(shell getconf _NPROCESSORS_ONLN 2>/dev/null || echo 1)
export SECURE_BOOT ?= 0
export SECURE_BOOT_KEY ?=
export SECURE_BOOT_CERT ?=

.PHONY: help deps image-deps fetch kernel rootfs image secure-boot-keys run run-image list-disks install clean distclean

help:
	@printf '%s\n' \
		'ForgeOS ARM64 targets:' \
		'  make deps       - install host deps for ARM64 systemd/Nix builds' \
		'  make image-deps - install ARM64 image + QEMU deps' \
		'  make fetch      - download Linux, BusyBox, systemd, and Nix payloads' \
		'  make kernel     - build the ARM64 Linux kernel and modules' \
		'  make rootfs     - build systemd rootfs, stage Nix, and make initramfs' \
		'  make image      - build a bootable ARM64 GPT/UEFI disk image' \
		'  make secure-boot-keys - generate a local Secure Boot signing keypair' \
		'  make run        - boot kernel + initramfs in qemu-system-aarch64' \
		'  make run-image  - boot the ARM64 UEFI disk image in QEMU' \
		'  make list-disks - list candidate installation disks' \
		'  sudo make install DISK=/dev/sdX - install ForgeOS ARM64 to a real disk' \
		'  make clean      - remove build, staging, and output artifacts' \
		'  make distclean  - also remove downloaded and extracted sources'

deps:
	@./scripts/install-deps.sh

image-deps:
	@./scripts/install-image-deps.sh

fetch:
	@./scripts/fetch-sources.sh

kernel: fetch
	@./scripts/build-kernel.sh

rootfs:
	@./scripts/build-rootfs.sh

image:
	@./scripts/build-image.sh

secure-boot-keys:
	@./scripts/generate-secure-boot-keys.sh

run:
	@./scripts/run-qemu.sh direct

run-image:
	@./scripts/run-qemu.sh image

list-disks:
	@./scripts/install-to-disk.sh --list

install:
	@./scripts/install-to-disk.sh --device "$(DISK)" $(INSTALL_ARGS)

clean:
	@./scripts/clean.sh

distclean: clean
	@rm -rf downloads sources
