# Copyright 2019, 2020 IBM Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Copyright (c) 2020 Red Hat, Inc.


# Image URL to use all building/pushing image targets;
# Use your own docker registry and image name for dev/test by overridding the IMG and REGISTRY environment variable.
IMG ?= $(shell cat COMPONENT_NAME 2> /dev/null)
REGISTRY ?= quay.io/open-cluster-management

# Github host to use for checking the source tree;
# Override this variable ue with your own value if you're working on forked repo.
GIT_HOST ?= github.com/open-cluster-management

PWD := $(shell pwd)
BASE_DIR := $(shell basename $(PWD))

VERSION ?= $(shell cat COMPONENT_VERSION 2> /dev/null)

GIT_REMOTE_URL = $(shell git config --get remote.origin.url)
WORKING_CHANGES = $(shell git status --porcelain)
BUILD_DATE = $(shell date +%m/%d@%H:%M:%S)
GIT_COMMIT = $(shell git rev-parse --short HEAD)
VCS_REF = $(if $(WORKING_CHANGES),$(GIT_COMMIT)-$(BUILD_DATE),$(GIT_COMMIT))

IMAGE_VERSION = $(VERSION)
IMAGE_DISPLAY_NAME = ${IMG}
IMAGE_DESCRIPTION = Configures MongoDB replicas
IMAGE_MAINTAINER = ssli@redhat.com
IMAGE_VENDOR = Red Hat
IMAGE_SUMMARY = $(IMAGE_DESCRIPTION)
IMAGE_OPENSHIFT_TAGS = ${IMG}

ARCH := $(shell uname -m)
ifeq ($(ARCH), x86_64)
    ARCH = amd64
endif


DOCKER_BUILD_OPTS = --build-arg "VCS_REF=$(VCS_REF)" \
                    --build-arg "VCS_URL=$(GIT_REMOTE_URL)" \
                    --build-arg "IMAGE_NAME=$(DOCKER_IMAGE_ARCH)" \
                    --build-arg "IMAGE_DISPLAY_NAME=$(IMAGE_DISPLAY_NAME)" \
                    --build-arg "IMAGE_MAINTAINER=$(IMAGE_MAINTAINER)" \
                    --build-arg "IMAGE_VENDOR=$(IMAGE_VENDOR)" \
                    --build-arg "IMAGE_VERSION=$(IMAGE_VERSION)" \
                    --build-arg "IMAGE_DESCRIPTION=$(IMAGE_DESCRIPTION)" \
                    --build-arg "IMAGE_SUMMARY=$(IMAGE_SUMMARY)" \
                    --build-arg "IMAGE_OPENSHIFT_TAGS=$(IMAGE_OPENSHIFT_TAGS)" \
                    --build-arg "ARCH=$(ARCH)"


LOCAL_OS := $(shell uname)
ifeq ($(LOCAL_OS),Linux)
    TARGET_OS ?= linux
    XARGS_FLAGS="-r"
else ifeq ($(LOCAL_OS),Darwin)
    TARGET_OS ?= darwin
    XARGS_FLAGS=
else
    $(error "This system's OS $(LOCAL_OS) isn't recognized/supported")
endif

# Use podman if available, otherwise use docker
ifeq ($(CONTAINER_ENGINE),)
        CONTAINER_ENGINE = $(shell podman version > /dev/null 2>&1 && echo podman || echo docker)
endif

# GITHUB_USER containing '@' char must be escaped with '%40'
GITHUB_USER := $(shell echo $(GITHUB_USER) | sed 's/@/%40/g')
GITHUB_TOKEN ?=


USE_VENDORIZED_BUILD_HARNESS ?=

ifndef USE_VENDORIZED_BUILD_HARNESS
-include $(shell curl -s -H 'Authorization: token ${GITHUB_TOKEN}' -H 'Accept: application/vnd.github.v4.raw' -L https://api.github.com/repos/open-cluster-management/build-harness-extensions/contents/templates/Makefile.build-harness-bootstrap -o .build-harness-bootstrap; echo .build-harness-bootstrap)
else
-include vbh/.build-harness-vendorized
endif

copyright-check:
	./build/copyright-check.sh $(TRAVIS_BRANCH)

default::
	@echo "Build Harness Bootstrapped"

############################################################
# images section
############################################################

build-images:
	@$(CONTAINER_ENGINE) build ${DOCKER_BUILD_OPTS} . -f Dockerfile -t ${IMAGE_NAME_AND_VERSION}
	@$(CONTAINER_ENGINE) tag ${IMAGE_NAME_AND_VERSION} $(REGISTRY)/$(IMG):latest

############################################################
# clean section
############################################################
clean::
	rm -rf $(BINDIR)
