# ==============================================================================
# OREBOLT OS  --  UNIFIED MASTER BUILD ORCHESTRATOR  (v1.4)
# ==============================================================================
#
# Target:  HiFiWalker H2 (Ingenic X1000E / JZ4760 family, MIPS32r2,
#          little-endian, hard-float, mipsel-linux-musl)
# Host:    Arch Linux (x86_64) with mipsel-linux-musl cross-compiler
#
# v1.4 CHANGES (see CHANGELOG.md for full diff):
#   * Corrected target SoC: T31 -> X1000E (matches the Aigo Eros Q / H2 / F20
#     hardware family documented in /upload/h2-and-related-devices.text).
#   * Kernel/uapi headers now sourced from the FiiO M3K GPL kernel tree
#     (Linux 3.10.14, Ingenic BSP) -- same X1000E SoC as the H2.  See
#     HEADERS.md for the full sourcing strategy and the fallback to the
#     raw Ingenic XBurst BSP for any header FiiO stripped.
#   * Rockbox tree demoted from "primary header reference" to OPTIONAL
#     bare-metal reference only.  Rockbox is NOT used for the Linux build
#     path -- it is the OS, not a header set on top of Linux.
#   * Module count reconciled: 16 modules in Makefile, PREREQUISITES,
#     SHA256SUMS, and ARCHITECTURE.
#   * bitchat mesh module added under AGPL-3.0-only.  See LICENSE.md and
#     licenses/AGPL-3.0.txt for the multi-tier licensing strategy.
#
# Sub-builds:
#
#   1. H2 Core Platform v6.1   (Makefile.h2-core-v6.1)
#      - LVGL v8.3.11 as liblvgl.so (shared library, ~200 KB)
#      - 16 module binaries in modules/orebolt-*/ link dynamically
#      - Master broker (main.c) + emulator input mapper daemon
#      - Output: overlay/usr/lib/liblvgl.so, overlay/usr/bin/h2_test,
#                overlay/apps/*.mod, overlay/usr/bin/emulator_input_mapper
#
#   2. OreBolt OS v1.4    (Makefile.orebolt-v1.4)
#      - src/modules/*.c -> liborebolt.a (static archive)
#      - 150-payload HID macro matrix via inject_payloads.sh
#      - bitchat mesh module compiled with -DAGPL_BITCHAT
#
# Module layout (16 modules):
#   modules/orebolt-vault/     vault.c
#   modules/orebolt-nettap/    scalpel.c
#   modules/orebolt-deploy/    deploy.c
#   modules/orebolt-studio/    studio.c
#   modules/orebolt-probe/     probe.c
#   modules/orebolt-vterm/     vterm.c
#   modules/orebolt-radar/     noise_radar.c
#   modules/orebolt-ducky/     ducky.c
#   modules/orebolt-extract/   extract.c
#   modules/orebolt-noise/     noise.c
#   modules/orebolt-reset/     reset.c
#   modules/orebolt-emulate/   emulator.c, emulator_input_mapper.c
#   modules/orebolt-rfid/      rfid.c          [v1.4: restored]
#   modules/orebolt-wifi/      wifi.c          [v1.4: restored]
#   modules/orebolt-glitch/    glitch.c        [v1.4: restored]
#   modules/orebolt-pwdb/      pwdb.c          [v1.4: restored]
#   modules/orebolt-bitchat/   bitchat.c       [v1.4: NEW, AGPL-3.0]
#
# To add a new module:
#   1. mkdir modules/orebolt-<name>/
#   2. Write modules/orebolt-<name>/<name>.c (include h2_ui.h)
#   3. Add "<name>:modules/orebolt-<name>/<name>.c \" to MODULES
#      in Makefile.h2-core-v6.1
#   4. Add "<name>.mod" to modules[] in main.c
#   5. make core
#
# License headers: every .c and .h file MUST carry an SPDX-License-Identifier
# line in its leading comment block.  bitchat uses AGPL-3.0-only; everything
# else uses GPL-2.0-or-later unless LICENSE.md says otherwise.
# ==============================================================================

H2_CORE_MAKEFILE  := Makefile.h2-core-v6.1
OREBOLT_MAKEFILE  := Makefile.orebolt-v1.4

# v1.4: kernel headers root, sourced from the FiiO M3K GPL tree.
# Override with KERNEL_HEADERS=/path/to/fiio-m3k-linux if you cloned it
# somewhere other than the default.  See HEADERS.md.
KERNEL_HEADERS    ?= /opt/fiio-m3k-linux

.PHONY: all core orebolt payloads clean validate_env help headers-check

all: validate_env headers-check core orebolt
	@echo "======================================================================"
	@echo "[OK] OREBOLT OS v1.4 FULLY BUILT"
	@echo "  Target SoC:  Ingenic X1000E (JZ4760 family, MIPS32r2 mipsel)"
	@echo "  Headers:     $(KERNEL_HEADERS)"
	@echo "  LVGL shared: overlay/usr/lib/liblvgl.so"
	@echo "  Launcher:    overlay/usr/bin/h2_test"
	@echo "  Mapper:      overlay/usr/bin/emulator_input_mapper"
	@echo "  Modules:     overlay/apps/*.mod  (16 modules)"
	@echo "  HW archive:  liborebolt.a"
	@echo "  Payloads:    overlay/data/payloads/{linux,macos,windows}/"
	@echo "  Licensing:   see LICENSE.md (bitchat = AGPL-3.0-only)"
	@echo "======================================================================"

validate_env:
	@if [ "$$(basename $$(pwd))" != "h2-workspace" ]; then \
		echo "[-] CRITICAL: build must run inside h2-workspace/"; \
		exit 1; \
	fi

# v1.4 NEW: verify the FiiO M3K kernel headers tree exists and exposes the
# X1000E SoC headers we expect.  Falls back to the Ingenic XBurst BSP if
# KERNEL_HEADERS points there instead.  See HEADERS.md §3.
headers-check: validate_env
	@if [ ! -d "$(KERNEL_HEADERS)" ]; then \
		echo "[-] CRITICAL: KERNEL_HEADERS not found at $(KERNEL_HEADERS)"; \
		echo "    Clone the FiiO M3K GPL kernel tree (see HEADERS.md §2):"; \
		echo "      git clone --depth 1 https://github.com/...",
		echo "          $(KERNEL_HEADERS)"; \
		exit 1; \
	fi
	@if [ ! -f "$(KERNEL_HEADERS)/include/uapi/linux/kernel.h" ]; then \
		echo "[-] $(KERNEL_HEADERS) does not look like a Linux kernel tree"; \
		echo "    (missing include/uapi/linux/kernel.h)"; \
		exit 1; \
	fi
	@if [ ! -f "$(KERNEL_HEADERS)/arch/mips/include/asm/mach-jz4760/jz4760.h" ]; then \
		echo "[!] Warning: JZ4760 SoC header not found at expected path."; \
		echo "    If this is the XBurst BSP layout, set KERNEL_HEADERS accordingly."; \
		echo "    Continuing -- build will fail later if a needed header is missing."; \
	fi
	@echo "[OK] kernel headers root: $(KERNEL_HEADERS)"

core: validate_env
	@echo ">>> Building H2 Core Platform v6.1 (shared LVGL + 16 modules)..."
	$(MAKE) -f $(H2_CORE_MAKEFILE) all KERNEL_HEADERS=$(KERNEL_HEADERS)

orebolt: validate_env
	@echo ">>> Building OreBolt OS v1.4 (liborebolt.a + payloads)..."
	$(MAKE) -f $(OREBOLT_MAKEFILE) all HARDWARE_GPIO=$(HARDWARE_GPIO) \
		KERNEL_HEADERS=$(KERNEL_HEADERS)

payloads: validate_env
	@echo ">>> Regenerating 150-payload HID matrix only..."
	@chmod +x inject_payloads.sh
	@./inject_payloads.sh

clean:
	-$(MAKE) -f $(H2_CORE_MAKEFILE) clean
	-$(MAKE) -f $(OREBOLT_MAKEFILE) clean HARDWARE_GPIO=$(HARDWARE_GPIO)

help:
	@echo "OREBOLT OS v1.4 MASTER BUILD"
	@echo ""
	@echo "Target SoC: Ingenic X1000E (JZ4760 family, MIPS32r2 mipsel-linux-musl)"
	@echo "Headers:    FiiO M3K GPL kernel tree ($(KERNEL_HEADERS))"
	@echo ""
	@echo "Targets:"
	@echo "  all           Build everything (default)"
	@echo "  headers-check Verify kernel headers tree before build"
	@echo "  core          Build LVGL shared lib + launcher + 16 modules"
	@echo "  orebolt       Build liborebolt.a + payload matrix"
	@echo "  payloads      Regenerate 150-payload HID macro matrix only"
	@echo "  clean         Remove all build artifacts"
	@echo "  help          This message"
	@echo ""
	@echo "Environment:"
	@echo "  KERNEL_HEADERS  Path to FiiO M3K GPL kernel tree (default: /opt/fiio-m3k-linux)"
	@echo "  HARDWARE_GPIO   Set to 1 to compile real GPIO panic-purge path"
	@echo "  CROSS_COMPILE   Toolchain prefix (default: mipsel-linux-musl-)"
	@echo ""
	@echo "Adding a new module:"
	@echo "  1. mkdir modules/orebolt-<name>/"
	@echo "  2. Write modules/orebolt-<name>/<name>.c (include h2_ui.h)"
	@echo "  3. Add '<name>:modules/orebolt-<name>/<name>.c' to MODULES"
	@echo "  4. Add '<name>.mod' to modules[] in main.c"
	@echo "  5. make core"
