GO_BIN?=$(shell pwd)/.bin
BINARY_DIR=bin
BINARY_NAME=doco-cd
.PHONY: test test-verbose test-coverage test-run build fmt lint update update-all download tools compose-up compose-down wiki-tools wiki-build wiki-serve wiki-version-publish

ifneq (,$(wildcard ./.env))
    include .env
    export
endif

BUILD_FLAGS:=
ifeq ($(shell uname), Linux)
    BUILD_FLAGS:=-linkmode external -extldflags '-static -Wl,-unresolved-symbols=ignore-all'
    COMPILER=CGO_ENABLED=1 CC=musl-gcc
else ifeq ($(shell uname), Darwin)
		BUILD_FLAGS:=""
		COMPILER=CGO_ENABLED=1 CC=clang CXX=clang++
endif

BUILD_FLAGS:=-ldflags="-X main.Version=dev $(BUILD_FLAGS)"

test:
	@echo "Running tests..."
	@WEBHOOK_SECRET="test_Secret1" API_SECRET="test_apiSecret1" ${COMPILER} go test ${BUILD_FLAGS} -cover ./... -timeout 10m

# The nobitwarden build tag excludes the Bitwarden SDK, which is the only
# dependency that requires cgo, so this target builds without a C compiler.
test-nobitwarden:
	@echo "Running tests without bitwarden integration..."
	@WEBHOOK_SECRET="test_Secret1" API_SECRET="test_apiSecret1" CGO_ENABLED=0 go test -ldflags="-X main.Version=dev" -tags nobitwarden -cover ./... -timeout 10m

test-verbose:
	@echo "Running tests..."
	@WEBHOOK_SECRET="test_Secret1" API_SECRET="test_apiSecret1" ${COMPILER} go test ${BUILD_FLAGS} -v -cover ./... -timeout 10m

test-coverage:
	@echo "Running tests with coverage..."
	@WEBHOOK_SECRET="test_Secret1" API_SECRET="test_apiSecret1" ${COMPILER} go test ${BUILD_FLAGS} -v -coverprofile cover.out ./...
	@go tool cover -html cover.out -o cover.html

# Run specified tests from arguments
test-run:
	@echo "Running tests: $(filter-out $@,$(MAKECMDGOALS))"
	@WEBHOOK_SECRET="test_Secret1" API_SECRET="test_apiSecret1" ${COMPILER} go test ${BUILD_FLAGS} -cover ./... -timeout 10m -run $(filter-out $@,$(MAKECMDGOALS))

build:
	mkdir -p $(BINARY_DIR)
	${COMPILER} go build ${BUILD_FLAGS} -o $(BINARY_DIR) ./...

lint fmt:
	${GO_BIN}/golangci-lint run --fix ./...
	@go fix ./...

update:
	git pull origin main
	git submodule update --recursive --remote

update-all: update
	git submodule foreach git pull origin master
	git submodule foreach git checkout master

download:
	@echo Download go.mod dependencies
	@go mod download

tools:
	mkdir -p ${GO_BIN}
	curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/HEAD/install.sh | sh -s -- -b ${GO_BIN} latest
	GOBIN=${GO_BIN} go install tool

compose-up:
	@echo "Starting dev docker-compose..."
	@docker compose -f dev.compose.yaml up --build

compose-down:
	@echo "Stopping dev docker-compose..."
	@docker compose -f dev.compose.yaml down --volumes

cleanup:
	@CONTAINERS=$$(docker container ls -a --format "{{.ID}}" --filter "label=cd.doco.metadata.manager"); \
	if [ -n "$$CONTAINERS" ]; then \
		for PROJECT in $$(for ID in $$CONTAINERS; do docker container inspect --format '{{ index .Config.Labels "com.docker.compose.project" }}' $$ID; done | sort | uniq); do \
			docker compose -p $$PROJECT down -v; \
		done; \
	else \
		echo "No containers to clean up."; \
	fi

clean-testcache:
	go clean -testcache

webhook:
	@SIGNATURE=$$(openssl dgst -sha256 -hmac "test_Secret1" < cmd/doco-cd/testdata/github_payload.json | sed 's/^.* //') && \
  	curl -X POST -H "X-Hub-Signature-256: sha256=$$SIGNATURE" \
  		-H "Content-Type: application/json" \
  		-H "X-GitHub-Event: push" \
  		--data @cmd/doco-cd/testdata/github_payload.json \
  		http://localhost/v1/webhook

wiki-tools:
	python3 -m venv .venv-wiki
	.venv-wiki/bin/python -m pip install --upgrade pip
	.venv-wiki/bin/python -m pip install -r wiki/requirements.txt

wiki-serve:
	.venv-wiki/bin/zensical serve --config-file wiki/zensical.toml
