# Copyright 2026 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

EAPI=8

PYTHON_COMPAT=(python3_{12..15})

inherit python-single-r1

DESCRIPTION="A collection of user-contributed scripts for Sway"
HOMEPAGE="https://github.com/OctopusET/sway-contrib"
SRC_URI="https://github.com/OctopusET/${PN}/archive/refs/tags/${PV}.tar.gz -> ${P}.tar.gz"

LICENSE="MIT"
SLOT="0"
KEYWORDS="amd64"
IUSE="+grimshot +grimpicker"
REQUIRED_USE="${PYTHON_REQUIRED_USE}"

RDEPEND="
  ${PYTHON_DEPS}
  $(python_gen_cond_dep ' 
    dev-python/i3ipc[${PYTHON_USEDEP}]
  ')
  grimshot? (
    app-misc/jq
    gui-apps/grim
    gui-apps/wl-clipboard
    x11-libs/libnotify
  )
  grimpicker? (
    gui-apps/grim
    gui-apps/slurp
    media-gfx/imagemagick
  )
"
BDEPEND="
  grimpicker? ( app-text/scdoc )
"

src_prepare() {
	default

	local file
	for file in *.py; do
		if [[ $(head -n1 "${file}") != "#!"* ]]; then
			sed -i '1i #!/usr/bin/env python3' "${file}" || die "Failed to do sed"
		fi
	done
}

src_compile() {
	if use grimpicker; then
		emake -C grimpicker
	fi
}

src_install() {
	python_doscript *.py

	if use grimshot; then
		dobin grimshot/grimshot
		doman grimshot/grimshot.1
		insinto /usr/share/bash-completion/completions
		newins grimshot/grimshot-completion.bash grimshot
	fi

	if use grimpicker; then
		emake -C grimpicker DESTDIR="${D}" PREFIX=/usr install
		insinto /usr/share/zsh/site-functions
		newins grimpicker/completion.zsh _grimpicker
	fi

	dodoc README.md
}