# Template file for 'incus'
pkgname=incus
version=6.6.0
revision=2
build_style=go
build_helper=qemu
go_import_path=github.com/lxc/incus/v6
go_build_tags="libsqlite3"
go_package="${go_import_path}/cmd/..."
make_check_args="-skip TestConvertNetworkConfig"
hostmakedepends="pkg-config"
makedepends="lxc-devel acl-devel cowsql-devel raft-devel
 libcap-devel libuv-devel sqlite-devel eudev-libudev-devel"
depends="lxc acl acl-progs rsync squashfs-tools xz dnsmasq iptables attr-progs
 ${pkgname}-client-${version}_${revision}"
short_desc="Powerful system container and virtual machine manager"
maintainer="dkwo <npiazza@disroot.org>"
license="Apache-2.0"
homepage="https://linuxcontainers.org/incus"
distfiles="https://github.com/lxc/incus/archive/refs/tags/v${version}.tar.gz"
checksum=0274f6258591a3189737812228722d5c7b0cc57deb5edd0f65750d3323210394
system_groups="_incus-admin _incus"
make_dirs="
 /var/lib/incus 0755 root root
 /var/log/incus 0755 root root"

export CGO_LDFLAGS_ALLOW="(-Wl,-wrap,pthread_create)|(-Wl,-z,now)"
export GOFLAGS="-buildmode=pie"

post_build() {
	CGO_ENABLED=0 go install -p "${XBPS_MAKEJOBS}" -v -tags netgo -buildmode=default "${go_import_path}/cmd/incus-migrate"
	CGO_ENABLED=0 go install -p "${XBPS_MAKEJOBS}" -v -tags agent,netgo -buildmode=default "${go_import_path}/cmd/incus-agent"
	cd cmd/lxd-to-incus && go install -v ./
}

post_install() {
	vmkdir usr/libexec/incus
	for f in ${DESTDIR}/usr/bin/{incusd,incus-user}; do
		vinstall "${f}" 700 usr/libexec/incus && rm "${f}"
	done
	vsv incus
	vsv incus-user

	# avoid conflict with lxd, lxd-lts
	mv ${DESTDIR}/usr/bin/{fuidshift,fuidshift-incus}
	# upstream recommends these should be kept to root only
	for _tool in fuidshift-incus lxd-to-incus; do
		chmod 700 ${DESTDIR}/usr/bin/${_tool}
	done

	# generate shell completions
	local _incus=${DESTDIR}/usr/bin/incus
	for _shell in bash fish zsh; do
		vtargetrun ${_incus} completion ${_shell} > scripts/${_shell}-completion
	done

	vdoc "${FILESDIR}/README.voidlinux"
}

incus-client_package() {
	short_desc+=" - client"
	pkg_install() {
		vmove usr/bin/incus
		for shell in bash fish zsh; do
			vcompletion scripts/${shell}-completion ${shell} incus
		done
	}
}

incus-tools_package() {
	short_desc+=" - tools"
	pkg_install() {
		for _tool in fuidshift-incus lxc-to-incus lxd-to-incus \
		incus-benchmark incus-migrate incus-simplestreams; do
			vmove usr/bin/${_tool}
		done
	}
}
