# drivestage — top-level Makefile
#
# Common developer entry points: build, test, lint, dist, install.
# Run `make help` for a summary.
#
# Author: Jeremy Anderson <info@dcos.net> — https://dcos.net
# License: MIT

VERSION := 0.4.0
PROJECT := drivestage
TARBALL := $(PROJECT)-$(VERSION).tar.gz
SHA256  := $(TARBALL).sha256

# Paths
GUI_DIR        := gui
SCRIPT_DIR     := scripts
ENGINE_SRC     := $(SCRIPT_DIR)/blkstage.sh
ENGINE_ASSET   := $(GUI_DIR)/assets/blkstage.sh
# Tarball staging directory. Deliberately named `dist-staging` (not `dist/` or
# `release/`) so it doesn't collide with the phony targets `dist` and
# `release` — Make would otherwise see a circular dependency between the
# phony target and the directory of the same name.
DIST_DIR       := dist-staging

# Install destinations (override with make PREFIX=/opt/drivestage install)
PREFIX          ?= /usr/local
BINDIR          ?= $(PREFIX)/bin
LIBDIR          ?= $(PREFIX)/lib/$(PROJECT)

# Tooling
CARGO    ?= cargo
SHELLCHECK ?= shellcheck

# Default to the first target (informational).
.DEFAULT_GOAL := all

# Phony targets — none of these produce a file with the same name.
.PHONY: all gui test check fmt clippy dist release clean install \
        sync-assets help

##@ Help

help: ## Show this help.
        @awk 'BEGIN {FS = ":.*##"; printf "Usage: make [target]\n\nTargets:\n"} \
              /^[a-zA-Z_-]+:.*##/ { printf "  %-14s %s\n", $$1, $$2 }' $(MAKEFILE_LIST)

##@ Build

all: gui ## Build everything (currently just the GUI).

gui: ## Build the Rust GUI in release mode.
        $(CARGO) build --release --manifest-path $(GUI_DIR)/Cargo.toml

##@ Testing

test: ## Run bash distro-detection tests + cargo test.
        bash $(SCRIPT_DIR)/test_distro_detection.sh
        $(CARGO) test --manifest-path $(GUI_DIR)/Cargo.toml

##@ Lint & format

check: ## cargo check + shellcheck.
        $(CARGO) check --manifest-path $(GUI_DIR)/Cargo.toml
        $(SHELLCHECK) -x $(ENGINE_SRC)

fmt: ## Format Rust code (writes).
        $(CARGO) fmt --manifest-path $(GUI_DIR)/Cargo.toml

clippy: ## Run clippy with -D warnings.
        $(CARGO) clippy --manifest-path $(GUI_DIR)/Cargo.toml --all-targets -- -D warnings

##@ Packaging

dist: $(DIST_DIR)/$(TARBALL) $(DIST_DIR)/$(SHA256) ## Build release tarball + sha256.

release: dist ## Alias for `dist` (cut a release artifact).
        @echo "Release artifact ready in $(DIST_DIR)/"

$(DIST_DIR)/:
        mkdir -p $(DIST_DIR)

$(DIST_DIR)/$(TARBALL): $(ENGINE_SRC) $(GUI_DIR)/Cargo.toml $(GUI_DIR)/Cargo.lock | $(DIST_DIR)/
        @# Stage a clean export of the source tree (excluding build artefacts).
        git archive --format=tar.gz --prefix=$(PROJECT)-$(VERSION)/ HEAD -o $(DIST_DIR)/$(TARBALL)
        @echo "Created $(DIST_DIR)/$(TARBALL)"

$(DIST_DIR)/$(SHA256): $(DIST_DIR)/$(TARBALL) | $(DIST_DIR)/
        cd $(DIST_DIR) && sha256sum $(TARBALL) > $(SHA256)
        @echo "Wrote $(DIST_DIR)/$(SHA256)"

##@ Install

install: gui sync-assets ## Install the engine script + GUI binary to PREFIX.
        install -d $(DESTDIR)$(BINDIR)
        install -d $(DESTDIR)$(LIBDIR)
        install -m 0755 $(GUI_DIR)/target/release/drivestage-gui $(DESTDIR)$(BINDIR)/drivestage-gui
        install -m 0755 $(ENGINE_SRC) $(DESTDIR)$(BINDIR)/drivestage
        install -m 0644 $(ENGINE_SRC) $(DESTDIR)$(LIBDIR)/blkstage.sh
        @echo "Installed drivestage + drivestage-gui to $(DESTDIR)$(PREFIX)"

##@ Repo hygiene

sync-assets: ## Copy scripts/blkstage.sh → gui/assets/blkstage.sh.
        install -m 0644 $(ENGINE_SRC) $(ENGINE_ASSET)
        @echo "Synced $(ENGINE_SRC) → $(ENGINE_ASSET)"

clean: ## Remove cargo build artefacts.
        $(CARGO) clean --manifest-path $(GUI_DIR)/Cargo.toml
        rm -rf $(DIST_DIR)
