# ==============================================================================
# OREBOLT OS  --  UNIFIED MASTER BUILD ORCHESTRATOR  (v1.5)
# ==============================================================================
#
# 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.5 CHANGES (see CHANGELOG.md for full diff):
#   * All 12 original module implementations restored from v1.3 upstream
#     (vault, nettap, deploy, studio, probe, vterm, radar, pauto, extract,
#     noise, reset, emulate).  Were stubs in v1.4.
#   * emulator_input_mapper fully integrated from addon-emulator-1.0
#     (no longer a separate addon).  Standalone uinput daemon with
#     hardened build flags, ROM dirs, and upgraded init script.
#   * h2_ui.h merges v1.3 graphics init (init_h2_graphics_runtime) with
#     v1.4 module registration API.
#   * log_manager.h merges v1.3 timed coalescer with v1.4 log levels.
#   * main.c restored to fork/exec launcher model (v1.3) with v1.4's
#     module metadata table for the menu.
#   * Module build count: 17 .mod binaries + 1 standalone mapper daemon.
#   * Missing overlay data restored: bt_config/, chat_config/.
#   * Added -lbluetooth linkage for radar (BLE) module.
#
# Sub-builds:
#
#   1. H2 Core Platform v6.2   (Makefile.h2-core-v6.2)
#      - LVGL v8.3.11 as liblvgl.so (shared library, ~200 KB)
#      - 19 module .mod binaries in overlay/apps/
#      - Master broker (main.c) with fork/exec launcher
#      - Integrated emulator_input_mapper daemon (NO LVGL, hardened build)
#      - ROM directory structure (11 systems in overlay/data/roms/)
#      - Output: overlay/usr/lib/liblvgl.so, overlay/usr/bin/h2_test,
#                overlay/apps/*.mod, overlay/usr/bin/emulator_input_mapper
#
#   2. OreBolt OS v1.5    (Makefile.orebolt-v1.5)
#      - src/modules/*.c -> liborebolt.a (static archive)
#      - 150-payload HID macro matrix via inject_payloads.sh
#      - bitchat mesh module compiled with -DAGPL_BITCHAT
#
# ==============================================================================

H2_CORE_MAKEFILE  := Makefile.h2-core-v6.2
OREBOLT_MAKEFILE  := Makefile.orebolt-v1.5

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.5 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 (fork/exec broker)"
        @echo "  Mapper:      overlay/usr/bin/emulator_input_mapper (integrated, hardened)"
        @echo "  ROM dirs:    overlay/data/roms/{nes,snes,gb,...,ngp} (11 systems)"
        @echo "  Modules:     overlay/apps/*.mod  (19 modules, real implementations)"
        @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

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):"; \
                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"; \
                exit 1; \
        fi
        @echo "[OK] kernel headers root: $(KERNEL_HEADERS)"

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

orebolt: validate_env
        @echo ">>> Building OreBolt OS v1.5 (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.5 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 + 19 modules + mapper"
        @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-)"