#!/bin/sh
set -e

# CONTAINERD_VERSION specifies the version of the containerd runtime binary
# to install from the https://github.com/containerd/containerd repository.
#
# This version is used to build statically compiled containerd binaries, and
# used for the integration tests. The distributed docker .deb and .rpm packages
# depend on a separate (containerd.io) package, which may be a different version
# as is specified here.
#
# Generally, the commit specified here should match a tagged release.
#
# The containerd golang package is also pinned in vendor.mod. When updating
# the binary version you may also need to update the vendor version to pick up
# bug fixes or new APIs, however, usually the Go packages are built from a
# commit from the master branch.
: "${CONTAINERD_VERSION:=v1.7.27}"

install_containerd() (
	echo "Install containerd version $CONTAINERD_VERSION"
	git clone https://github.com/containerd/containerd.git "$GOPATH/src/github.com/containerd/containerd"
	cd "$GOPATH/src/github.com/containerd/containerd"
	git checkout -q "$CONTAINERD_VERSION"

	export BUILDTAGS='netgo osusergo static_build'
	export EXTRA_FLAGS=${GO_BUILDMODE}
	export EXTRA_LDFLAGS='-extldflags "-fno-PIC -static"'

	# Reset build flags to nothing if we want a dynbinary
	if [ "$1" = "dynamic" ]; then
		export BUILDTAGS=''
		export EXTRA_FLAGS=''
		export EXTRA_LDFLAGS=''
	fi
	make

	install -D bin/containerd "${PREFIX}/containerd"
	install -D bin/containerd-shim-runc-v2 "${PREFIX}/containerd-shim-runc-v2"
	install -D bin/ctr "${PREFIX}/ctr"
)
