HAS_HELM_DOCS := $(shell command -v helm-docs;)
HAS_HELM_UNITTEST := $(shell helm plugin list | grep unittest 2> /dev/null)

SCHEMA_MODS_JSON_FILES = $(shell find schema-mods -name "*.json")
SCHEMA_MODS_JQ_FILES = $(shell find schema-mods -name "*.jq")

INTEGRATION_VALUES_FILES = $(shell find integrations -name "*-values.yaml" | sort -u)
INTEGRATION_DOCS_FILES = $(INTEGRATION_VALUES_FILES:integrations/%-values.yaml=./docs/integrations/%.md)
INTEGRATION_SCHEMA_FILES = $(INTEGRATION_VALUES_FILES:integrations/%-values.yaml=./schema-mods/definitions/%-integration.schema.json)

templates/secrets/_helpers.tpl: ../../templates/secrets/_helpers.tpl
	cp $< $@

templates/secrets/_secret.alloy.tpl: ../../templates/secrets/_secret.alloy.tpl
	cp $< $@

Chart.lock: Chart.yaml
	helm dependency update .
	touch Chart.lock # Ensure the timestamp is updated

.SECONDEXPANSION:
docs/integrations/%.md: integrations/%-values.yaml $$(wildcard docs/integrations/.doc_templates/%.gotmpl)
	docker run --platform linux/amd64 --rm -v $(shell pwd):/src ghcr.io/grafana/helm-docs-and-schema-gen $(shell echo $< | sed 's/integrations\/\([-a-z]*\)-values.yaml/\1/') integration

schema-mods/definitions/%-integration.schema.json: integrations/%-values.yaml
	docker run --platform linux/amd64 --rm -v $(shell pwd):/src ghcr.io/grafana/helm-docs-and-schema-gen $(shell echo $< | sed 's/integrations\/\([-a-z]*\)-values.yaml/\1/') integration

NUMBER_OF_INTEGRATION_VALUES_FILES := $(words $(INTEGRATION_VALUES_FILES))
schema-mods/integration-list.json: $(INTEGRATION_VALUES_FILES)
	@echo '{' > $@
	@echo '  "definitions": {' >> $@
	@echo '    "integration-list": {' >> $@
	@echo '      "type": "array",' >> $@
	@echo '      "items": {' >> $@
	@echo '        "anyOf": [' >> $@
	@count=0; \
	for file in $(INTEGRATION_VALUES_FILES); do \
		count=$$((count + 1)); \
		if [ $$count -eq $(NUMBER_OF_INTEGRATION_VALUES_FILES) ]; then \
			echo '          { "$$ref": "#/definitions/'$$(echo $${file} | sed 's/integrations\/\([-a-z]*\)-values.yaml/\1/')'-integration"}' >> $@; \
		else \
			echo '          { "$$ref": "#/definitions/'$$(echo $${file} | sed 's/integrations\/\([-a-z]*\)-values.yaml/\1/')'-integration"},' >> $@; \
		fi; \
	done
	@echo '        ]' >> $@
	@echo '      }' >> $@
	@echo '    }' >> $@
	@echo '  }' >> $@
	@echo '}' >> $@

templates/_integration_types.tpl: $(INTEGRATION_VALUES_FILES)
	echo '{{/* Do not edit this file. It is generated by the Makefile */}}' > $@
	echo '{{- define "integrations.types" -}}' >> $@
	for file in $(INTEGRATION_VALUES_FILES); do \
		echo - $$(echo $${file} | sed 's/integrations\/\([-a-z]*\)-values.yaml/\1/') >> $@; \
	done
	echo '{{- end -}}' >> $@

values.schema.json: values.yaml $(INTEGRATION_SCHEMA_FILES) $(SCHEMA_MODS_JSON_FILES) $(SCHEMA_MODS_JQ_FILES) schema-mods/integration-list.json
	../../../../scripts/schema-gen.sh .

README.md: values.yaml Chart.yaml $$(wildcard README.md.gotmpl)
ifdef HAS_HELM_DOCS
	helm-docs
else
	docker run --rm --volume "$(shell pwd):/helm-docs" -u $(shell id -u) jnorwood/helm-docs:latest
endif

.PHONY: clean
clean:
	rm -f README.md values.schema.json schema-mods/integration-list.json templates/_integration_types.tpl templates/secrets/_helpers.tpl templates/secrets/_secret.alloy.tpl
	rm -f $(INTEGRATION_SCHEMA_FILES)
	rm -f $(INTEGRATION_DOCS_FILES)

.PHONY: build
build: README.md $(INTEGRATION_DOCS_FILES) Chart.lock values.schema.json templates/_integration_types.tpl templates/secrets/_helpers.tpl templates/secrets/_secret.alloy.tpl

.PHONY: test
test: build
	helm lint .
	ct lint --lint-conf ../../../../.configs/lintconf.yaml --helm-dependency-extra-args=--skip-refresh --charts .
ifdef HAS_HELM_UNITTEST
	helm unittest .
else
	docker run --rm --volume $(shell pwd):/apps helmunittest/helm-unittest:3.17.0-0.7.1 .
endif

