CUE = ../scripts/cue.sh
VDEV ?= cargo vdev

export CI ?= false

# Override to specify which network address to use in Hugo/HTTP servers for binding
export SERVER_BIND ?= 127.0.0.1
# Override to specify which port to use in Hugo/HTTP servers for listening
export SERVER_PORT ?= 1313

clean:
	rm -rf public resources data/cargo-lock.toml data/docs.json

.PHONY: setup-vdev
setup-vdev:
	cd .. && scripts/environment/prepare.sh --modules=vdev,prettier

.PHONY: setup
setup: setup-vdev
	yarn

# Copy Cargo data such as the dependency information file
cargo-data:
	cp -f ../Cargo.lock data/cargo-lock.toml

# Build JSON from CUE sources with `make cue-build`. Run other commands with `make cue-*`
cue-build:
	$(VDEV) build docs-json

cue-%:
	${CUE} $*

.PHONY: component-examples
component-examples: generate-vrl-docs generate-deprecations-json
	$(VDEV) build component-examples website/generated/example-configs

.PHONY: config-examples
config-examples: component-examples

generate-vrl-docs: setup-vdev
	$(VDEV) build vrl-docs --output-dir cue/reference/remap/functions \
		$(if $(findstring true,$(CI)),--vector-sha $(GITHUB_SHA),)

generate-deprecations-json: setup-vdev
	$(VDEV) deprecation generate

structured-data: generate-vrl-docs generate-deprecations-json component-examples

serve: clean setup cargo-data structured-data
	$(MAKE) serve-development

.PHONY: serve-development
serve-development:
	hugo \
	--buildDrafts \
	--buildFuture \
	--environment "development"
	$(MAKE) pagefind
	yarn concurrently --kill-others --success first --names hugo,pagefind \
		"hugo server --bind $(SERVER_BIND) --port $(SERVER_PORT) --buildDrafts --buildFuture --environment development" \
		"yarn pagefind:watch"

# Live site
production-build:
	hugo \
	--environment "production" \
	--minify
	$(MAKE) pagefind

ci-production-build: setup cargo-data structured-data production-build

# Preview site
preview-build:
	hugo \
	$(if $(DEPLOY_PRIME_URL),--baseURL $(DEPLOY_PRIME_URL),) \
	--buildFuture \
	--environment "preview" \
	--minify
	cp ./custom-headers/_preview ./public/_headers
	$(MAKE) pagefind

ci-preview-build: setup cargo-data structured-data preview-build

run-link-checker:
	htmltest

run-external-link-checker:
	htmltest --conf ./.htmltest.external.yml

# Check links without rebuilding the CUE sources. Optimal for fixing links locally.
check-links: production-build run-link-checker

# Search
pagefind:
	yarn pagefind --site public

# Useful for locally debugging issues that arise only on the deployed production site
run-production-site-locally:
	make setup cargo-data structured-data production-build
	python3 -m http.server $(SERVER_PORT) --bind $(SERVER_BIND) --directory ./public

# Local dev build with no link checking and no Yarn dependency fetching
quick-build: clean cargo-data structured-data production-build

# Full local builds without Algolia updates (for debugging, link checking, etc.)
local-production-build: clean setup cargo-data structured-data production-build run-link-checker

local-preview-build: clean setup cargo-data structured-data preview-build run-link-checker

# Generate Lighthouse scores locally
lighthouse-report:
	yarn lighthouse

# Browser tests use a static server over the same minified assets as deployment.
.PHONY: test
test: cargo-data structured-data
	$(MAKE) production-build
	yarn test
