# =============================================================================
# Knowledge Genome - Makefile v. 0.2.0
# Orchestrates the setup and management of the knowledge base.
# =============================================================================

include globals.env
export $(shell grep -v '^[#[:space:]]' globals.env | sed 's/=.*//')

.PHONY: setup add-genome status lint lock doctor sync help

help:
	@echo "Available commands:"
	@echo "  make setup         - Full system initialization"
	@echo "  make add-genome    - Register and scaffold a new genome"
	@echo "  make status        - Check submodule and encryption status"
	@echo "  make lint          - Verify schema, privacy flags, and metadata"
	@echo "  make lock          - Lock all encrypted files across all genomes"
	@echo "  make doctor        - Verify all required tools are installed"
	@echo "  make sync          - Sync submodules and report unpushed commits"

lint:
	@bash scripts/lint-genomes.sh

setup:
	@bash scripts/setup.sh

add-genome:
	@if [ -z "$(NAME)" ] || [ -z "$(DESC)" ]; then \
		echo "Error: NAME and DESC are required."; \
		echo "Usage: make add-genome NAME=my-genome DESC='My description'"; \
		exit 1; \
	fi
	@bash scripts/add-genome.sh "$(NAME)" "$(DESC)"

status:
	@echo "--- Master Status ---"
	@git submodule status
	@echo "--- Encryption Status (First 10 files) ---"
	@git-crypt status | head -n 10

doctor:
	@echo "Checking required tools..."
	@command -v git        >/dev/null 2>&1 || { echo "  MISSING: git";        exit 1; }
	@command -v git-crypt  >/dev/null 2>&1 || { echo "  MISSING: git-crypt";  exit 1; }
	@command -v curl       >/dev/null 2>&1 || { echo "  MISSING: curl";       exit 1; }
	@command -v jq         >/dev/null 2>&1 || { echo "  MISSING: jq";         exit 1; }
	@command -v bw         >/dev/null 2>&1 || echo "  OPTIONAL: bw (Bitwarden CLI) not found — key injection will be manual."
	@echo "System ready."

sync:
	@echo "Syncing submodules..."
	@git submodule update --init --recursive
	@echo "--- Unpushed commits per genome ---"
	@git submodule foreach 'git log --oneline @{u}.. 2>/dev/null | head -5 || true'

lock:
	@echo "Locking master repository..."
	@git-crypt lock 2>/dev/null || true
	@echo "Locking all submodules..."
	@git submodule foreach 'git-crypt lock 2>/dev/null || true'
	@echo "All genomes securely locked."
