## To get GIT_* env variables reference https://github.com/cloudposse/build-harness/blob/master/modules/git/bootstrap.Makefile

# Extract ticket number from branch name (e.g. feature/ch37684/add-codefresh)
FEATURE ?= $(shell echo "$(GIT_BRANCH)" | cut -s -d'/' -f2 | tr '[:upper:]' '[:lower:]')


## Production build only released versions
ifeq ($(PIPELINE_ENV),production-build)
  ifeq ($(GIT_IS_TAG),1)
  	PIPELINE_ACTION = build
  endif
endif

## Production deploys only released versions
ifeq ($(PIPELINE_ENV),production)
  STAGE ?= prod
  ifeq ($(GIT_IS_TAG),1)
  	PIPELINE_ACTION = deploy
  endif
endif

## Pre-production deploys only released versions
ifeq ($(PIPELINE_ENV),pre-production)
  STAGE ?= pre-prod
  ifeq ($(GIT_IS_TAG),1)
  	PIPELINE_ACTION = deploy
  endif
endif

## Staging deploys all commits (working branch should be set on codefresh pipeline trigger config)
ifeq ($(PIPELINE_ENV),staging)
  STAGE ?= stage
  PIPELINE_ACTION = deploy
endif

## Integration deploys on each PR create/update. On PR close - cleanup
ifeq ($(PIPELINE_ENV),integration)
  STAGE ?= $(FEATURE)

  ## If triggered manually

  ifeq ($(CF_BUILD_TRIGGER),build)
    PIPELINE_ACTION ?= deploy
  endif

  ## GitHub integration support
  ifeq ($(CF_PULL_REQUEST_ACTION),opened)
    PIPELINE_ACTION = deploy
  endif

  ifeq ($(CF_PULL_REQUEST_ACTION),reopened)
    PIPELINE_ACTION = deploy
  endif

  ifeq ($(CF_PULL_REQUEST_ACTION),synchronize)
    PIPELINE_ACTION = deploy
  endif

  ifeq ($(CF_PULL_REQUEST_ACTION),closed)
    PIPELINE_ACTION = destroy
  endif

  ## GitLab integration support
  ifeq ($(CF_PULL_REQUEST_ACTION),open)
    PIPELINE_ACTION = deploy
  endif

  ifeq ($(CF_PULL_REQUEST_ACTION),reopen)
    PIPELINE_ACTION = deploy
  endif

  ifeq ($(CF_PULL_REQUEST_ACTION),update)
    PIPELINE_ACTION = deploy
  endif

  ifeq ($(CF_PULL_REQUEST_ACTION),close)
    PIPELINE_ACTION = destroy
  endif

  ifeq ($(CF_PULL_REQUEST_ACTION),merge)
    PIPELINE_ACTION = destroy
  endif

endif

## Diff codefresh sync - display diff
ifeq ($(PIPELINE_ENV),codefresh-diff)
  PIPELINE_ACTION ?= diff
endif

## Apply codefresh sync - apply changes
ifeq ($(PIPELINE_ENV),codefresh-apply)
  PIPELINE_ACTION ?= apply
endif

PIPELINE_ACTION ?= pass

ifneq ($(PIPELINE_ENV),)
  ifneq ($(PIPELINE_ENV),production-build)
    NAMESPACE ?= $(PROJECT)-$(STAGE)
    APP_HOST ?= $(APP_NAME).$(STAGE).$(BASE_HOST)
    RELEASE_NAME ?= $(NAMESPACE)-$(APP_NAME)
  endif
endif

## Export pipeline vars
codefresh/pipeline/export:
	$(call assert-set,PROJECT)
	$(call assert-set,PIPELINE_ACTION)
	@echo "PROJECT=$(PROJECT)"
	@echo "PIPELINE_ACTION=$(PIPELINE_ACTION)"

ifneq ($(PIPELINE_ENV),)
  ifneq ($(PIPELINE_ENV),production-build)
	$(call assert-set,NAMESPACE)
	$(call assert-set,APP_HOST)
	$(call assert-set,RELEASE_NAME)
	$(call assert-set,BASE_HOST)
	$(call assert-set,APP_NAME)
	@echo "NAMESPACE=$(NAMESPACE)"
	@echo "APP_HOST=$(APP_HOST)"
	@echo "RELEASE_NAME=$(RELEASE_NAME)"
  endif
endif

## Export sync pipeline vars
codefresh/sync/pipeline/export:
	$(call assert-set,PIPELINE_ACTION)
	@echo "PIPELINE_ACTION=$(PIPELINE_ACTION)"

