#!/usr/bin/make -f

#   Copyright 2018-2020 Docker Inc.

#   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.
export HOME=/root
export GOPATH=$(shell pwd)/
PREFIX="$(shell pwd)/build"

PACKAGE=${PACKAGE:-containerd.io}
GO_SRC_PATH="$(shell pwd)/src/github.com/containerd/containerd"
#git clone https://gitee.com/wanghuaiqing/v20.10.3-containerd $GO_SRC_PATH
#git clone https://gitee.com/wanghuaiqing/runc.git /go/src/github.com/opencontainers/runc
#VERSION="$(shell git --git-dir "${GO_SRC_PATH}/.git" describe --tags | sed 's/^v//')"
VERSION="1.4.3-1-g6fbe2f9"

#REF=$(shell git --git-dir "${GO_SRC_PATH}/.git" rev-parse HEAD)
REF="6fbe2f9a8e085ffeb72c00027c4f9c889261602c"


%:
	dh $@ --with systemd


# GO_SRC_PATH PACKAGE VERSION and REF are defined in build-deb
binaries: ## Create containerd binaries
	@set -x; make -C $(GO_SRC_PATH) --no-print-directory \
		DESTDIR="$$(pwd)" \
		VERSION=$(VERSION) \
		REVISION=$(REF) \
		PACKAGE=$(PACKAGE) \
		binaries install

	# Remove containerd-stress, as we're not shipping it as part of the packages
	rm -f bin/containerd-stress

bin/runc:
	@set -x; make -C $(shell pwd)/src/github.com/opencontainers/runc  --no-print-directory \
		BINDIR="$$(pwd)/bin" \
		BUILDTAGS='seccomp apparmor selinux' \
		runc install

man: ## Create containerd man pages
	@echo "+ make -C $(GO_SRC_PATH) --no-print-directory man"
	@make -C $(GO_SRC_PATH) --no-print-directory man

	# copy the generated man pages instead of using "make install-man" to allow
	# dh_installman doing its magic
	install -d man
	install -D -m 0644 $(GO_SRC_PATH)/man/* man

#override_dh_auto_build: binaries bin/runc man
override_dh_auto_build: binaries bin/runc

override_dh_systemd_start:
	dh_systemd_start --restart-after-upgrade
	sed -i 's/_dh_action=try-restart/_dh_action=restart/g' ./debian/containerd.io.postinst.debhelper

#override_dh_auto_install: binaries bin/runc man
override_dh_auto_install: binaries bin/runc
	mkdir -p debian/containerd.io/usr/bin
	install -D -m 0755 bin/* debian/containerd.io/usr/bin
	install -D -m 0644 common/containerd.service debian/containerd.io/lib/systemd/system/containerd.service
	install -D -m 0644 common/containerd.toml    debian/containerd.io/etc/containerd/config.toml
