#!/usr/bin/make -f

include /usr/share/dpkg/default.mk
include /usr/share/dpkg/pkg-info.mk

# Uncomment this to turn on verbose mode.
# export DH_VERBOSE = 1

export GOPATH := $(CURDIR)/_build
export DH_GOPKG := k8s.io/kubectl
export DH_GOLANG_BUILDPKG := "$(DH_GOPKG)/..."

# This information can be found consulting the GitHub tags
# https://github.com/kubernetes/kubernetes/tags
GIT_COMMIT := a78aa47129b8539636eb86a9d00e31b2720fe06b
GIT_TREE_STATE := clean

# Extract major, minor, and full version
DEB_UPSTREAM_MAIN_VERSION := $(shell echo ${DEB_VERSION_UPSTREAM} | sed -e 's/^\([0-9\.]*\).*/\1/')
GIT_MAJOR := $(shell echo $(DEB_UPSTREAM_MAIN_VERSION) | cut -d. -f1)
GIT_MINOR := $(shell echo $(DEB_UPSTREAM_MAIN_VERSION) | cut -d. -f2)
GIT_VERSION := v$(DEB_UPSTREAM_MAIN_VERSION)

DATE_FMT = +%Y-%m-%dT%H:%M:%SZ
ifdef SOURCE_DATE_EPOCH
    BUILD_DATE ?= $(shell date -u -d "@$(SOURCE_DATE_EPOCH)" "$(DATE_FMT)" 2>/dev/null || date -u -r "$(SOURCE_DATE_EPOCH)" "$(DATE_FMT)" 2>/dev/null || date -u "$(DATE_FMT)")
else
    BUILD_DATE ?= $(shell date "$(DATE_FMT)")
endif

# Upstream uses a helper function to generate LDFLAGS flags based on a set of variables.
# https://github.com/kubernetes/kubernetes/blob/9a9f10bc7b93ef13a83e39b262f4ea8bf2353e53/hack/lib/version.sh#L151
# There is a hack script to get those variables using the upstream repository.
# https://github.com/kubernetes/kubernetes/blob/9a9f10bc7b93ef13a83e39b262f4ea8bf2353e53/hack/print-workspace-status.sh
KUBECTL_LDFLAGS := \
    -X 'k8s.io/client-go/pkg/version.buildDate=$(BUILD_DATE)' \
    -X 'k8s.io/component-base/version.buildDate=$(BUILD_DATE)' \
	-X 'k8s.io/client-go/pkg/version.gitCommit=$(GIT_COMMIT)' \
	-X 'k8s.io/component-base/version.gitCommit=$(GIT_COMMIT)' \
	-X 'k8s.io/client-go/pkg/version.gitTreeState=$(GIT_TREE_STATE)' \
	-X 'k8s.io/component-base/version.gitTreeState=$(GIT_TREE_STATE)' \
	-X 'k8s.io/client-go/pkg/version.gitVersion=$(GIT_VERSION)' \
	-X 'k8s.io/component-base/version.gitVersion=$(GIT_VERSION)' \
	-X 'k8s.io/client-go/pkg/version.gitMajor=$(GIT_MAJOR)' \
	-X 'k8s.io/component-base/version.gitMajor=$(GIT_MAJOR)' \
	-X 'k8s.io/client-go/pkg/version.gitMinor=$(GIT_MINOR)' \
	-X 'k8s.io/component-base/version.gitMinor=$(GIT_MINOR)'


ifeq (,$(filter $(DEB_BUILD_ARCH), mipsel mips64el))
KUBECTL_BUILDMODE := -buildmode=pie
endif


%:
	dh $@ --builddirectory=_build --buildsystem=golang

override_dh_auto_configure:
	mkdir -pv $(GOPATH)/src/$(DH_GOPKG)

	# Prepare build directory but throw away source directory.
	DH_GOPKG="__IGNORE__" dh_auto_configure

	$(RM) -r $(GOPATH)/src/$(DH_GOPKG)
	cp -ra staging/src/k8s.io/kubectl $(GOPATH)/src/$(DH_GOPKG)

	mkdir -pv $(GOPATH)/src/$(DH_GOPKG)/cmd
	cp -ra cmd/kubectl $(GOPATH)/src/$(DH_GOPKG)/cmd

override_dh_auto_build:
	dh_auto_build -v -- -ldflags '$(KUBECTL_LDFLAGS)' $(KUBECTL_BUILDMODE)

override_dh_installchangelogs:
	dh_installchangelogs -X CHANGELOG.md CHANGELOG/CHANGELOG-*.md

ifeq (,$(filter nodoc,$(DEB_BUILD_PROFILES)))
execute_before_dh_installman-arch:
	help2man debian/kubectl/usr/bin/kubectl \
		--no-info --no-discard-stderr \
		--version-string="$(GIT_VERSION)" \
		-n "Command-line tool for interacting with a Kubernetes cluster's control plane" > debian/kubectl.1
endif

override_dh_auto_install:
	# Skip dh_auto_install to stop dh_golang trying to copy bits
	# for /usr/share/gocode (we use dh_install for that)

ifeq ($(DEB_BUILD_ARCH),$(DEB_HOST_ARCH))
	mkdir -v -p _build/completion/kubectl
	_build/bin/kubectl completion bash > _build/completion/kubectl/kubectl.bash
	_build/bin/kubectl completion fish > _build/completion/kubectl/kubectl.fish
	_build/bin/kubectl completion zsh > _build/completion/kubectl/kubectl.zsh
endif

	$(RM)  _build/src/k8s.io/kubectl/pkg/cmd/plugin/testdata/kubectl-create-foo
	$(RM) -r _build/src/k8s.io/kubectl/.github
	$(RM) -r _build/src/k8s.io/kubectl/LICENSE
	# Don't install docs in /usr/share/gocode, they are not useful to the users so just remove
	# them instead of moving to /usr/share/doc:
	$(RM) _build/src/k8s.io/kubectl/CONTRIBUTING.md
	$(RM) _build/src/k8s.io/kubectl/README.md
	$(RM) _build/src/k8s.io/kubectl/code-of-conduct.md
	$(RM) -r _build/src/k8s.io/kubectl/docs
	$(RM) _build/src/k8s.io/kubectl/pkg/util/i18n/translations/README.md
	$(RM) _build/src/k8s.io/kubectl/pkg/cmd/edit/testdata/README