# Copyright 1999-2017 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2

EAPI=7

inherit eutils git-r3

DESCRIPTION="Library and podman tool for running OCI-based containers in Pods"

LICENSE="Apache-2.0"
SLOT="0"
KEYWORDS="amd64"

EGIT_REPO_URI=https://github.com/containers/podman.git
EGIT_BRANCH=v2.2.1-rhel
EGIT_COMMIT=a0d478edea7f775b7ce32f8eb1a01e75374486cb

IUSE="apparmor btrfs +fuse selinux systemd seccomp +rootless"

COMMON_DEPEND="
	app-crypt/gpgme:=
	>=app-emulation/conmon-2.0.0
	|| ( >=app-emulation/runc-1.0.0_rc6 app-emulation/crun )
	dev-libs/libassuan:=
	dev-libs/libgpg-error:=
	>=net-misc/cni-plugins-0.8.6
	seccomp? ( sys-libs/libseccomp:= )
	rootless? ( app-emulation/slirp4netns )
	apparmor? ( sys-libs/libapparmor )
	btrfs? ( sys-fs/btrfs-progs )
	selinux? ( sys-libs/libselinux:= )
	systemd? ( sys-apps/systemd )"
DEPEND="
	${COMMON_DEPEND}
	dev-go/go-md2man"
RDEPEND="${COMMON_DEPEND}
	fuse? ( sys-fs/fuse-overlayfs )"

RESTRICT="network-sandbox"

PATCHES=(
	"${FILESDIR}"/0001-Disable-Makefile-selinux-install-options.patch
)

src_compile() {
	local myargs=(
		$(usex btrfs "" exclude_graphdriver_btrfs)
		$(usev apparmor)
		$(usev seccomp)
		$(usev selinux)
		$(usev systemd)
		exclude_graphdriver_devicemapper
	)
	local tags="${myargs[@]}"

	emake PREFIX=/usr BUILDTAGS="${tags}"
}

src_install() {
	emake install PREFIX=/usr DESTDIR="${D}"

	insinto /etc/containers
	newins test/registries.conf registries.conf.example
	newins test/policy.json policy.json.example

	insinto /usr/share/containers
	doins vendor/github.com/containers/common/pkg/seccomp/seccomp.json

	newinitd "${FILESDIR}"/podman.initd podman

	insinto /etc/logrotate.d
	newins "${FILESDIR}/podman.logrotated" podman

	dobashcomp completions/bash/*

	insinto /usr/share/zsh/site-functions
	doins completions/zsh/*

	insinto /usr/share/fish/vendor_completions.d
	doins completions/fish/*

	keepdir /var/lib/containers
}

pkg_preinst() {
	LIBPOD_ROOTLESS_UPGRADE=false
	if use rootless; then
		has_version 'app-emulation/libpod[rootless]' || LIBPOD_ROOTLESS_UPGRADE=true
	fi
}

pkg_postinst() {
	local want_newline=false
	if [[ ! ( -e ${EROOT%/*}/etc/containers/policy.json && -e ${EROOT%/*}/etc/containers/registries.conf ) ]]; then
		elog "You need to create the following config files:"
		elog "/etc/containers/registries.conf"
		elog "/etc/containers/policy.json"
		elog "To copy over default examples, use:"
		elog "cp /etc/containers/registries.conf{.example,}"
		elog "cp /etc/containers/policy.json{.example,}"
		want_newline=true
	fi
	if [[ ${LIBPOD_ROOTLESS_UPGRADE} == true ]] ; then
		${want_newline} && elog ""
		elog "For rootless operation, you need to configure subuid/subgid"
		elog "for user running podman. In case subuid/subgid has only been"
		elog "configured for root, run:"
		elog "usermod --add-subuids 1065536-1131071 <user>"
		elog "usermod --add-subgids 1065536-1131071 <user>"
		want_newline=true
	fi
}
