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

EAPI=7

inherit udev

DESCRIPTION="Micronucleus is a bootloader designed for AVR ATtiny microcontrollers"
HOMEPAGE="https://github.com/micronucleus/micronucleus"
SRC_URI="https://github.com/micronucleus/micronucleus/archive/refs/tags/v${V}.tar.gz -> ${P}.tar.gz"

LICENSE="GPL-2"
SLOT="0"
KEYWORDS="~amd64 ~arm ~x86"
IUSE=""

DEPEND="virtual/libusb:1"


src_compile() {
	cd ${S}/commandline
	sed -i '/STATIC =/s:^:#:' Makefile #no static linking as long as we use libusb for e.g. udev
	sed -i '/micronucleus-cli version:/s:2\.5:2.6:' library/micronucleus_lib.h #the CLI still spits out an old version
	emake -j1 || die "emake micronucleus commandline failed."
}

src_install() {
	# DESTDIR not honored by install step, and it's only a few files; do it manually
	cd ${S}/commandline || die "Error changing directory to ${S}/commandline!"
	dobin micronucleus
	insinto /lib/udev/rules.d/
	doins 49-micronucleus.rules
	cd ${S}/firmware/releases || die "Error changing directory to ${S}/firmware/releases!"
	insinto /var/lib/${PN}
	doins *.hex
	cd ${S}/firmware/upgrades || die "Error changing directory to ${S}/firmware/upgrades!"
	insinto /var/lib/${PN}
	doins *.hex
}

pkg_postinst() {
	udev_reload
}

pkg_postrm() {
	udev_reload
}