#
# Main makefile for this auxiliary repo for EVA and ECC development
#

# ------------------------------------------------------------------------------------------
# Constants
# ------------------------------------------------------------------------------------------

TOPDIR=.
REALTOP=$(shell readlink -f .)

EMPIRIX_PIPELINE_FRAMEWORK_MAKEFILE_SUPPORT_DIR=$(REALTOP)/Makefiles
include ${TOPDIR}/Makefiles/Common-defines.mk
include ${TOPDIR}/Makefiles/Version-defines.mk
include $(TOPDIR)/Makefiles/Deploy-defines.mk


# ------------------------------------------------------------------------------------------
# Check for dependencies
# ------------------------------------------------------------------------------------------

check_deps:
	@echo "This repository has no dependencies"

# ------------------------------------------------------------------------------------------
# Build targets
# ------------------------------------------------------------------------------------------

docker_fulldist:
	@echo "This repository has nothing to build"

# ------------------------------------------------------------------------------------------
# Unit testing
# ------------------------------------------------------------------------------------------

# invoke below target with SKIP_STOP_TEST_DOCKER=1 to allow debugging unit tests
docker_unit_tests:
	@echo "Running tests inside a docker"
	docker run --rm --name test_docker \
		--volume $(REALTOP):/source:rw \
		--volume $(CLANG_FORMAT_BIN):/tmp/clang-format \
		--env TO_TEST_LIST='$(TO_TEST_LIST)' \
		--env SKIP_STOP_TEST_DOCKER='$(SKIP_STOP_TEST_DOCKER)' \
		--workdir /source \
		$(REMOTE_REPO_NEXUS_DOCKER_FOR_PULL)/library/oraclelinux:9 /source/UnitTests/run_tests.sh

docker_unit_tests_debug:
	@echo "Running tests inside a docker"
	docker run --rm --name test_docker -ti \
		--volume $(REALTOP):/source:rw \
		--volume $(CLANG_FORMAT_BIN):/tmp/clang-format \
		--workdir /source \
		$(REMOTE_REPO_NEXUS_DOCKER_FOR_PULL)/library/oraclelinux:9 \
		/source/UnitTests/prepare_docker.sh



# ------------------------------------------------------------------------------------------
# Code coverage targets
# ------------------------------------------------------------------------------------------

docker_gcov_finalize:
	@echo "This repository has no unit tests and hence no coverage"

# ------------------------------------------------------------------------------------------
# Packaging targets 
# ------------------------------------------------------------------------------------------

install:
ifndef DESTDIR
	@echo "*** ERROR: please call this makefile supplying explicitly the DESTDIR variable"
	@exit 1
endif
	@mkdir -p $(DESTDIR)/makefiles $(DESTDIR)/scripts $(DESTDIR)/static-analysis/ $(DESTDIR)/config-file-generator/ $(DESTDIR)/yaml-to-json-schema/ $(DESTDIR)/systemhealth-kpi-generator/
	# install MAKEFILE SUPPORT files for BUILDING:
	@cp -avr Makefiles/*                      $(DESTDIR)/makefiles/
	@cp -avr Scripts/*                        $(DESTDIR)/scripts/
	@cp -avr StaticAnalysis/*                 $(DESTDIR)/static-analysis/
	@cp -avr ConfigFileGenerator/*            $(DESTDIR)/config-file-generator/
	@cp -avr YamlToJsonSchema/*               $(DESTDIR)/yaml-to-json-schema/
	@cp -avr SystemHealthKpiGenerator/*       $(DESTDIR)/systemhealth-kpi-generator/

docker_fullpackaging::
	conan create . empirix/$(CONAN_CHANNEL)

clean::
	$(MAKE) packaging_clean

# ------------------------------------------------------------------------------------------
#  # Automated tests targets
# ------------------------------------------------------------------------------------------

automated_tests_stop:
automated_tests:
ifeq ($(ARCH),docker)
	@echo "##teamcity[blockOpened name='$(ARCH) AutomatedTests']"
	@echo "======================================================================================================================"
	@echo "== $(ARCH) Automated tests"
	@echo "======================================================================================================================"
	@echo "No automated tests are supported by this repo"
	@echo "##teamcity[blockClosed name='$(ARCH) AutomatedTests']"
endif

# ------------------------------------------------------------------------------------------
# Documentation targets
# ------------------------------------------------------------------------------------------

docker_documentation:
	@echo "No documentation auto-generated by this repo"

# ------------------------------------------------------------------------------------------
# Deploy targets
# ------------------------------------------------------------------------------------------

CONAN_PACKAGES=pipeline-framework/$(CONAN_VERSION)@empirix/$(CONAN_CHANNEL)

EMPIRIX_PIPELINE_FRAMEWORK_MAKEFILE_SUPPORT_DIR=$(REALTOP)/Makefiles
EMPIRIX_PIPELINE_FRAMEWORK_SCRIPT_SUPPORT_DIR=$(REALTOP)/Scripts
include ${TOPDIR}/Makefiles/Deploy-targets.mk

docker_deploy: 
	$(MAKE) deploy_conan

run_redeploy:
	@echo "##teamcity[blockOpened name='Re-deploying artifacts to Artifactory']"
	@echo "=================================================================================="
	@echo " Re-deploying artifacts to Artifactory"
	@echo "=================================================================================="
	$(REDEPLOY_WRAPPER_SCRIPT) "$(CSV_URL)" "$(EXCLUDE_REGEX)" "$(ARTIFACTORY_USER)" "$(ARTIFACTORY_PW)" \
		"$(REMOTE_REPO_ARTIFACTORY_RPM_FOR_PUSH)" "$(REMOTE_REPO_ARTIFACTORY_DOCKER_FOR_PUSH)" \
		"$(REMOTE_REPO_ARTIFACTORY_HELM_FOR_PUSH)" "$(DEPLOY_FILELIST_LOG)"
	@echo "##teamcity[blockClosed name='Re-deploying artifacts to Artifactory']"

# ------------------------------------------------------------------------------------------
# Root-level targets
# ------------------------------------------------------------------------------------------

include $(TOPDIR)/Makefiles/RootMakefile-targets.mk


# ------------------------------------------------------------------------------------------
# Maintainance targets for Gitlab projects
# ------------------------------------------------------------------------------------------

include ${TOPDIR}/Makefiles/GitHelpers-targets.mk

LOCAL_PATH_TO_REPOS=/root/git

# upstreams
CPP_UPSTREAMS = third-party-cpp-libs base-libs common-libs networking-libs protocol-events events-networking ixengine-protocol-bundle 
OTHER_UPSTREAMS = automated-tests-framework third-party pipeline-framework docker-base

# downstreams
DOWNSTREAMS += capture statemachine correlator tcap-loadbalancer netutil
#tls-decipher

REPOS_TO_CLEANUP = $(CPP_UPSTREAMS) $(OTHER_UPSTREAMS) $(DOWNSTREAMS)

# note that the "git_delete_local_feature_branches FORCE=1" target is avail only for Makefiles including GitHelpers-targets.mk
# of version 3.9.10-master or higher

run_local_git_cleanup:
	@for repo in $(REPOS_TO_CLEANUP); do \
		if [ -d $(LOCAL_PATH_TO_REPOS)/$$repo/.git ]; then \
			echo "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"; \
			echo "** Cleaning up stale branches from the remote [origin] of [$$repo]" ; \
			echo "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"; \
			pushd . ; \
			cd $(LOCAL_PATH_TO_REPOS)/$$repo ; \
			make git_delete_local_feature_branches FORCE=1 ; \
			popd ; \
		fi ; \
	done
	@echo
	@echo "Cleanup of local repos completed"
	@echo


# ------------------------------------------------------------------------------------------
# Maintainance targets for Empirix Artifactory
# ------------------------------------------------------------------------------------------

run_artifactory_cleanup:
	pip3 install virtualenv
	python3 -m venv /opt/empirix/artifactory_cleanup_venv
	# the Teamcity pipeline is setting 2 env vars: GITLAB_USER and GITLAB_ACCESS_TOKEN
	source /opt/empirix/artifactory_cleanup_venv/bin/activate && \
		pip3 install -r Scripts/clean-docker-helm-artifacts-requirements.txt && \
		Scripts/clean-docker-helm-artifacts.py --exclude_modified_in_last_days=3

help::
	@echo "GIT Maintenance Targets:"
	@echo " LIST OF GITLAB PROJECTS TO CLEAN: $(REPOS_TO_CLEANUP)"
	@echo " - run_eva_ecc_gitlab_cleanup         delete from Gitlab remote all stale branches for all GIT projects under $(LOCAL_PATH_TO_REPOS)"
	@echo " - run_local_git_cleanup              delete all feature/bugfix branches from all GIT projects under $(LOCAL_PATH_TO_REPOS)"
	@echo 
