# Copyright 2026 Nadeŭka <me+oss@nadevko.cc>
# Distributed under the terms of the GNU General Public License v2
EAPI=8

inherit optfeature shell-completion systemd unpacker

DESCRIPTION='Standalone Unity command-line interface for managing editors, modules, projects'
HOMEPAGE=https://docs.unity.com/unity-cli
MY_PV=${PV/_beta/~beta.}
BASE_URI=https://hub-dist.unity3d.com/artifactory/hub-debian-prod-local/pool/main/u/unity-cli
SRC_URI="
	amd64? ( ${BASE_URI}/unity-cli_amd64/unity-cli_${MY_PV}_amd64.deb -> ${P}-amd64.deb )
	arm64? ( ${BASE_URI}/unity-cli_arm64/unity-cli_${MY_PV}_arm64.deb -> ${P}-arm64.deb )
"
S="${WORKDIR}"

LICENSE='Apache-2.0 MIT Unity-Editor-Software Unity-ToS'
SLOT=0
KEYWORDS='~amd64'

IUSE='+broker bwrap +telemetry'
RESTRICT='bindist mirror strip'

QA_PREBUILT='
	usr/bin/*
	usr/share/unity-cli/*
'

RDEPEND='
	app-arch/unzip
	broker? (
		res-group/unity-broker
		res-user/unity-broker
		sys-auth/polkit
	)
	bwrap? ( sys-apps/bubblewrap )
'
BDEPEND=$(unpacker_src_uri_depends)

src_unpack() {
	unpack_deb ${A}
}

src_prepare() {
	default

	if use broker; then
		if use bwrap; then
			sed -i -e "s|^ExecStart=/usr/bin/unity |ExecStart=${EPREFIX}/usr/share/unity-cli/unity |" \
				usr/lib/systemd/system/unity-auth-broker.service || die
		fi

		if ! use telemetry; then
			sed -i -e '/^\[Service\]/a Environment=UNITY_NO_CLI_INVOKED_TELEMETRY=1' \
				usr/lib/systemd/system/unity-auth-broker.service || die
		fi
	fi
}

src_compile() {
	local -x HOME="${T}"
	local -x XDG_CONFIG_HOME="${T}"/.config DOTNET_CLI_HOME="${T}"/.dotnet
	local -x UNITY_NO_CLI_INVOKED_TELEMETRY=1

	usr/bin/unity completion bash > unity || die
	usr/bin/unity completion zsh > _unity || die
	usr/bin/unity completion fish > unity.fish || die

	if use bwrap; then
		local exe
		for exe in unity $(usev telemetry cli-crash-reporter); do
			sed -e "s|@BIN_NAME@|${exe}|g" \
				"${FILESDIR}"/bwrap.sh > "${T}/${exe}" || die
		done
	fi
}

src_install() {
	# bins & wrappers
	if use bwrap; then
		exeinto /usr/share/unity-cli
		doexe usr/bin/unity
		dobin "${T}"/unity

		if use telemetry; then
			doexe usr/bin/cli-crash-reporter
			dobin "${T}"/cli-crash-reporter
		fi
	else
		dobin usr/bin/unity
		use telemetry && dobin usr/bin/cli-crash-reporter
	fi

	# services
	if use broker; then
		newinitd "${FILESDIR}"/unity-auth-broker.initd unity-auth-broker
		systemd_dounit usr/lib/systemd/system/unity-auth-broker.{service,socket}

		insinto /usr/share/polkit-1/actions
		doins usr/share/polkit-1/actions/com.unity3d.authbroker.policy
	fi

	# configs
	use telemetry || newenvd - 90${PN} <<<'UNITY_NO_CLI_INVOKED_TELEMETRY=1'

	# shell completion
	dobashcomp unity
	dozshcomp _unity
	dofishcomp unity.fish
}

pkg_postinst() {
	optfeature 'Azure DevOps CLI integration' app-admin/azure-cli
	optfeature 'CLI subprocess command caching' sys-apps/bkt
	optfeature 'Git Large File Storage' dev-vcs/git-lfs
	optfeature 'Gitea CLI integration' dev-vcs/tea
	optfeature 'GitHub CLI integration' dev-util/github-cli
	optfeature 'GitLab CLI integration' dev-vcs/glab
	optfeature 'official graphical management hub' gui-dcc/unityhub-bin
	optfeature 'Secret Service credential helper' app-crypt/libsecret
	optfeature 'version control integration' dev-vcs/git

	if use broker; then
		elog 'To enable the hardened system-wide auth broker:'
		elog '  rc-update add unity-auth-broker default && rc-service unity-auth-broker start'
		elog '  systemctl enable --now unity-auth-broker.socket'
		elog
		elog 'Check the active tier with: unity doctor'
	fi

	if ! use telemetry; then
		elog 'Telemetry and crash reports have been disabled via USE="-telemetry".'
		elog 'UNITY_NO_CLI_INVOKED_TELEMETRY=1 was set in /etc/env.d/90unity-cli-bin.'
	fi

	elog 'Note: Do not use "unity self-update" or "unity self-uninstall";'
	elog 'updates and removals are managed by Portage.'
}