# Contributor: Will Sinatra <wpsinatra@gmail.com>
# Contributor: Carlo Landmeter <clandmeter@gmail.com>
# Contributor: Francesco Colista <fcolista@alpinelinux.org>
# Maintainer: Leonardo Arena <rnalrd@alpinelinux.org>
pkgname=incus-feature
pkgver=6.4.0
pkgrel=1
pkgdesc="Powerful system container and virtual machine manager - Feature branch"
url="https://github.com/lxc/incus"
arch="all"
license="Apache-2.0"
depends="acl
	attr
	ca-certificates
	cgmanager
	dbus
	dnsmasq
	lxc
	iproute2
	iptables
	netcat-openbsd
	rsync
	squashfs-tools
	shadow-uidmap
	tar
	xz
	"
makedepends="acl-dev
	autoconf
	automake
	cowsql-dev
	raft-cowsql-dev
	eudev-dev
	gettext-dev
	go
	intltool
	libcap-dev
	libtool
	libuv-dev
	linux-headers
	lxc-dev
	lz4-dev
	sqlite-dev
	tcl-dev
	"
subpackages="$pkgname-client
	$pkgname-agent
	$pkgname-agent-openrc:agent_openrc:noarch
	$pkgname-user
	$pkgname-user-openrc:user_openrc:noarch
	$pkgname-openrc
	$pkgname-conversion
	$pkgname-utils
	$pkgname-bash-completion:bashcomp:noarch
	$pkgname-vm:vm:noarch
	$pkgname-oci:oci:noarch
	"
install="$pkgname.pre-install"
options="net"
source="$pkgname-$pkgver.tar.gz::https://github.com/lxc/incus/archive/refs/tags/v$pkgver.tar.gz
	incusd.confd
	incusd.initd
	$pkgname-agent.initd
	$pkgname-user.initd

	0001-statically-build-incus-agent-and-incus-migrate.patch
	"
builddir="$srcdir/incus-$pkgver"

_tools="fuidshift
	generate
	incus
	incus-benchmark
	incus-migrate
	incus-simplestreams
	lxc-to-incus
	lxd-to-incus
	sysinfo
	"

_project="github.com/lxc/incus"

export GOFLAGS="$GOFLAGS -tags=libsqlite3"
export GOCACHE="${GOCACHE:-"$srcdir/go-cache"}"
export GOTMPDIR="${GOTMPDIR:-"$srcdir"}"
export GOMODCACHE="${GOMODCACHE:-"$srcdir/go"}"
export GOPATH="$srcdir"

prepare() {
	default_prepare
	go mod download
}

build() {
	export CGO_CFLAGS="-I/usr/include/raft -I/usr/include/cowsql"
	export CGO_LDFLAGS="$LDFLAGS -lintl"
	export CGO_LDFLAGS_ALLOW="(-Wl,-wrap,pthread_create)|(-Wl,-z,now)"

	make
}

package() {
	# generate bash shell completion script
	chmod 755 "$srcdir"/bin/incus
	"$srcdir"/bin/incus completion bash > "$builddir"/scripts/bash-completion

	for daemon in incusd incus-agent incus-user; do
		install -Dm755 "$srcdir"/bin/$daemon "$pkgdir"/usr/sbin/$daemon
	done

	for tool in $_tools; do
		install -Dm755 "$srcdir"/bin/$tool "$pkgdir"/usr/bin/$tool
	done

	for init in incusd incus-feature-agent incus-feature-user; do
		install -Dm755 "$srcdir"/$init.initd \
		"$pkgdir"/etc/init.d/${init/-feature/}
	done
	install -Dm644 "$srcdir"/incusd.confd \
		"$pkgdir"/etc/conf.d/incusd

	install -Dm644 "$builddir"/scripts/bash-completion \
		"$pkgdir"/usr/share/bash-completion/completions/incus

	install -Dm755 "$builddir"/scripts/empty-incus.sh \
		"$pkgdir"/usr/bin/empty-incus.sh
}

agent() {
	pkgdesc="Incus agent"
	amove usr/sbin/incus-agent
}

agent_openrc() {
	pkgdesc="Incus agent OpenRC scripts"
	install_if="openrc ${subpkgname%-openrc}=$pkgver-r$pkgrel"
	amove etc/init.d/incus-agent
}

client() {
	pkgdesc="Incus CLI client"
	install -d "$subpkgdir"/usr/bin
	amove usr/bin/incus
}

user() {
	pkgdesc="Incus user project daemon"
	amove usr/sbin/incus-user
}

user_openrc() {
	pkgdesc="Incus user project daemon OpenRC scripts"
	install_if="openrc ${subpkgname%-openrc}=$pkgver-r$pkgrel"
	amove etc/init.d/incus-user
}

conversion() {
	pkgdesc="Tools to convert a prior lxc/lxd instance to incus"
	amove usr/bin/lxc-to-incus
	amove usr/bin/lxd-to-incus
}

utils() {
	pkgdesc="Incus utilities and scripts"
	depends="$pkgname py3-lxc jq"

	amove usr/bin
}

vm() {
	pkgdesc="Install packages required to run VMs under Incus"
	case "$CARCH" in
		x86_64)  depends=ovmf  ;;
		aarch64) depends=aavmf ;;
	esac
	depends="$depends
		qemu-system-x86_64
		qemu-chardev-spice
		qemu-hw-usb-redirect
		qemu-hw-display-virtio-vga
		qemu-img
		qemu-ui-spice-core
		incus-utils
		sgdisk
		util-linux-misc
		virtiofsd
		"
	install -d "$subpkgdir"
}

openrc() {
	provides=$pkgname-lts-openrc=$pkgver-r$pkgrel
	default_openrc
}

oci() {
	pkgdesc="Install packages required to run OCI images under Incus"
	depends="$depends skopeo umoci"
	install -d "$subpkgdir"
}

check() {
	# FIXME: disable failing tests
	go test -v -skip "TestCluster_*|TestIntegration_UnixSocket|TestContainerTestSuite_*|TestDotGo|TestParse|TestPackages|TestConvertNetworkConfig|TestSnapshotCommon|TestHttpRequest" ./...
}

sha512sums="
001ffbbc2eeb8b3d4d55a4bddaf5b62538c3f4edff43de2cb94d7891b5d67cc62d30706211d7691cfe17c46dcdbb48cc5bac96d66afe3497065594fa7b62bc23  incus-feature-6.4.0.tar.gz
0340f06589acc1509d6cf4c844320f23a40e8da9d6fd1aab59b7424b286bb109e81ded97e26b9a1467f64f378914f4d13f9726c48188468900ab8d88eb528d91  incusd.confd
ca8762648020b4c933879a7a01b3f6699e9efc1dc1141228342e6619d7e9b07b13d359121767d8cd1ce6d83752ced226964138040454eebec17a3479acdbcda3  incusd.initd
a495ca525f2d98771e076026e9046f5d120aa929ed114cae330829ad698c4dbc5d80ed815328f3558584520f6c8d44d7b037721eb977b70f0ad288f6ad19e9cf  incus-feature-agent.initd
ac40bdeb902a95c10d814a92ebf8f94b7d03858796278258b3bac43044329790d64fbd29a90db2491964d0228f894a098fee27699a37e635295b5c4386a50028  incus-feature-user.initd
2fb9f4b3e70eca29b023d17120eb4730efc9e4ad560ad94337ecbaf09992b800554a942847b1b992076712378c644f493a5ca3c58513760a26ba816f4c6dc8fc  0001-statically-build-incus-agent-and-incus-migrate.patch
"
