# Copyright 1999-2022 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

EAPI=8

inherit toolchain-funcs

DESCRIPTION="An application for working with the Microchip PicKit2/3 PIC programmers - updated version"
HOMEPAGE="http://www.microchip.com/pickit2 https://github.com/martonmiklos/pk2cmd"

LICENSE="MicroChip-PK2"
SLOT="0"

if [[ "${PV}" == "9999" ]] ; then
        EGIT_REPO_URI="https://github.com/martonmiklos/${PN}.git"
        EGIT_BRANCH="master"
        inherit git-r3
	S="${WORKDIR}/${P}/${PN}"
else
        SRC_URI="https://bitbucket.org/jpcgt/flatcam/downloads/FlatCAM-${PV}.zip -> ${P}.zip"
	KEYWORDS="~amd64 ppc ppc64 ~x86"
	S="${WORKDIR}/${PN}v${PV}LinuxMacSource"
fi

DEPEND="virtual/libusb:0"
RDEPEND="${DEPEND}"

PATCHES=(
	"${FILESDIR}"/${PN}-add-share-dir-for-dev-file-${PV}.patch
)

src_prepare() {
	default

	# Fix up the Makefile
	sed \
		-e 's:#TARGET=linux:TARGET=linux:' \
		-e 's:DBG=-O2:DBG=:' \
		-e 's:^CFLAGS=:CFLAGS+=:' \
		-e 's:^LDFLAGS=:LDFLAGS+=:' \
		-e 's:^LIBUSB=/usr/local:LIBUSB=/usr:' \
		-e 's:/lib -lusb:/'"$(get_libdir)"' -lusb:' \
		-e "s:^CC=g++::" \
		-i Makefile || die
}

src_compile() {
	emake CC="$(tc-getCXX)"
}

src_install() {
	# Copy the device files and PicKit2 OS
	insinto /usr/share/pk2
	doins PK2DeviceFile.dat PK2V023200.hex PK3BLV011405.hex PK3OSV020005.hex
	# Install the program
	dobin pk2cmd
	# Install the documentation
	dodoc Readme-libusb.txt ReadmeForPK2CMDLinux2-6.txt usbhotplug.txt
}