# hmanifest 1.0.0
CC      ?= cc
CFLAGS  ?= -O2 -Wall -Wextra -std=c99
PREFIX  ?= /usr/local

BIN      = hmanifest
BIN_STAT = hmanifest-static
SRC      = hmanifest.c

all: $(BIN)

$(BIN): $(SRC)
	$(CC) $(CFLAGS) -o $@ $(SRC)

# Fully static build: no libc version coupling, runs on any x86_64 Linux.
static: $(SRC)
	$(CC) $(CFLAGS) -static -o $(BIN_STAT) $(SRC)

# Binaries as shipped in bin/: stripped, still ABI-identical to `make` output.
release: all static
	strip -s $(BIN) $(BIN_STAT)

install: $(BIN)
	install -D -m 0755 $(BIN) $(DESTDIR)$(PREFIX)/bin/$(BIN)

# Round-trip self-test: generate, verify clean, then verify a tampered copy.
# The manifest lives outside the checked tree so it is not an "extra" file.
check: $(BIN)
	@rm -rf .hmanifest-check .hmanifest-check.manifest
	@mkdir -p .hmanifest-check/sub
	@printf 'alpha\n' > .hmanifest-check/a.txt
	@printf 'beta\n'  > .hmanifest-check/sub/b.txt
	@./$(BIN) -g .hmanifest-check > .hmanifest-check.manifest
	@./$(BIN) -c .hmanifest-check.manifest -d .hmanifest-check -q && echo "clean tree: PASS"
	@printf 'tampered\n' > .hmanifest-check/a.txt
	@if ./$(BIN) -c .hmanifest-check.manifest -d .hmanifest-check -q; then \
		echo "tamper detection: FAIL"; exit 1; else echo "tamper detection: PASS"; fi
	@rm -rf .hmanifest-check .hmanifest-check.manifest

clean:
	rm -f $(BIN) $(BIN_STAT)
	rm -rf .hmanifest-check .hmanifest-check.manifest

.PHONY: all static release install check clean
