HELM_REPO_NAMES ?= stable incubator demo
HELM_REPO_PREFIX ?= cloudposse

REPO_URL?=https://charts.cloudposse.com

ifneq ($(TRAVIS_PULL_REQUEST_BRANCH),)
  BRANCH=pr-$(TRAVIS_PULL_REQUEST_BRANCH)
else ifneq ($(TRAVIS_BRANCH),)
  BRANCH=$(TRAVIS_BRANCH)
else ifneq ($(TRAVIS_TAG),)
  BRANCH=$(TRAVIS_TAG)
else ifneq ($(wildcard .git),)
  BRANCH=$(GIT_BRANCH)
else ifeq ($(DEBUG),true)
  $(warning unable to extrapolate current BRANCH)
endif

ifeq ($(BRANCH),master)
  CURRENT_REPO_URL?=$(REPO_URL)
else
  CURRENT_REPO_URL?=https://charts.dev.cloudposse.com/$(BRANCH)
endif

## Show repo info
helm/repo/info:
	@echo "REPO_URL: $(REPO_URL)"
	@echo "CURRENT_REPO_URL: $(CURRENT_REPO_URL)"

## Add helm remote repos
helm/repo/add-remote:
	@$(SELF) -s --no-print-directory helm/repo/add-all \
		HELM_REPO_URL=$(REPO_URL)

## Add helm remote dev repos
helm/repo/add-current:
	@$(SELF) -s --no-print-directory helm/repo/add-all \
		HELM_REPO_PREFIX=$(HELM_REPO_PREFIX)-dev \
		HELM_REPO_URL=$(CURRENT_REPO_URL)
	
helm/repo/add-all:
	@REPO_NAME=kubernetes-charts \
		REPO_ENDPOINT=http://storage.googleapis.com/kubernetes-charts \
		$(SELF) -s --no-print-directory helm/repo/add
	@REPO_NAME=kubernetes-charts-incubator \
		REPO_ENDPOINT=https://kubernetes-charts-incubator.storage.googleapis.com \
		$(SELF) -s --no-print-directory helm/repo/add
	@for repo in $(HELM_REPO_NAMES); do \
		if [ -d "$$repo" ]; then \
			curl --fail -s -q $(HELM_REPO_URL)/$$repo/index.yaml -o /dev/null; \
			if [ $$? -eq 0 ]; then \
				REPO_NAME=$(HELM_REPO_PREFIX)-$$repo REPO_ENDPOINT=$(HELM_REPO_URL)/$$repo $(SELF) -s --no-print-directory helm/repo/add; \
			else \
				echo "$(HELM_REPO_URL)/$$repo not available"; \
			fi; \
		else \
			echo "$$repo does not exist; skipping..."; \
		fi; \
	done
	@$(SELF) -s --no-print-directory helm/repo/update
	@$(HELM) repo list

## Add $REPO_NAME from $REPO_ENDPOINT
helm/repo/add:
	$(call assert-set,REPO_NAME)
	$(call assert-set,REPO_ENDPOINT)
	@$(HELM) repo add $(REPO_NAME) $(REPO_ENDPOINT)
	@echo "Added repo $(REPO_NAME)"

## Update repo info
helm/repo/update:
	@$(HELM) repo update

## Fix repo filesystem permissions
helm/repo/fix-perms:
	$(call assert-set,REPO_NAME)
	@find $(HELM_REPO_PATH) -type f -name '*.yaml' -exec chmod 644 {} \;
	@find $(HELM_REPO_PATH) -type f -name '*.txt' -exec chmod 644 {} \;
	@find $(HELM_REPO_PATH) -type f -name '*.tpl' -exec chmod 644 {} \;
	@find $(HELM_REPO_PATH) -type f -name '*.md' -exec chmod 644 {} \;
	@find $(HELM_REPO_PATH) -type f -name '*.tgz' -exec chmod 644 {} \;

helm/repo/deps:
	$(call assert-set,REPO_NAME)
	@echo "## Fetching deps for charts in $(shell basename $(CURDIR))"
	@find $(HELM_REPO_PATH) -maxdepth 1 -mindepth 1 -type d  | \
    	xargs -n 1 -I '{}' sh -c 'echo && echo "# Updating dependencies for {}" && $(HELM) dependency build --debug {}'

helm/repo/package:
	$(call assert-set,REPO_NAME)
	@echo "## Building packages in $(shell basename $(CURDIR))"
	@find $(HELM_REPO_PATH) -maxdepth 1 -mindepth 1 -type d  | \
    	xargs -n 1 -I '{}' sh -c 'echo && echo "# Building package for {}" && $(HELM) package --debug {}'
	@mkdir -p $(HELM_PACKAGE_PATH)
	@find . -maxdepth 1 -mindepth 1 -type f -name '*.tgz' | \
		xargs -I '{}' mv {} $(HELM_PACKAGE_PATH)

helm/repo/index:
	$(call assert-set,REPO_NAME)
	@echo "## Generating index for $(CURRENT_REPO_URL)/$(REPO_NAME)"
	@$(CURL) --fail -s -q $(CURRENT_REPO_URL)/$(REPO_NAME)/index.yaml -o /tmp/index.yaml; \
	if [ $$? -eq 0 ]; then \
		$(HELM) repo index $(HELM_PACKAGE_PATH) --merge /tmp/index.yaml --url $(CURRENT_REPO_URL)/$(REPO_NAME) --debug; \
	else \
		$(HELM) repo index $(HELM_PACKAGE_PATH) --url $(CURRENT_REPO_URL)/$(REPO_NAME) --debug; \
	fi; \

## Lint charts
helm/repo/lint:
	$(call assert-set,REPO_NAME)
	@echo "## Linting charts $(shell pwd)"
	@find $(HELM_REPO_PATH) -maxdepth 1 -mindepth 1 -type d | \
    	xargs -n 1 -I'{}' $(HELM) lint {}

## Clean helm repo
helm/repo/clean:
	$(call assert-set,REPO_NAME)
	@rm -f *.tgz
	@find $(HELM_REPO_PATH) -maxdepth 3 -mindepth 3 -type f -name '*.tgz' -delete
	@rm -f $(HELM_PACKAGE_PATH)/*

## Build repo
helm/repo/build: helm/repo/clean helm/repo/fix-perms helm/repo/deps helm/repo/lint helm/repo/package
	@exit 0
