# Contributor: Sören Tempel <soeren+alpine@soeren-tempel.net>
# Maintainer: Marian Buschsieweke <marian.buschsieweke@posteo.net>
_pkgname=openocd
_commit=ea2e26f7d521f5755b4bfda7bf12d99650277421
pkgname=$_pkgname-git
pkgver=0_git20240113
pkgrel=1
pkgdesc="Free and Open On-Chip Debugging, In-System Programming and Boundary-Scan Testing"
url="http://openocd.org/"
arch="all"
license="GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-only"
makedepends="
	autoconf
	automake
	hidapi-dev
	jimtcl-dev
	libftdi1-dev
	libjaylink-dev
	libtool
	libusb-dev
	"
subpackages="
	$pkgname-dbg
	$pkgname-dev
	$pkgname-doc
	$pkgname-udev-rules:udev_rules:noarch
	$pkgname-cmd-openocd:symlink:noarch
	"
# Note: Using a github mirror here as sourceforge git snapshots have changing checksums
source="
	$_pkgname-$_commit.tar.gz::https://github.com/openocd-org/openocd/archive/$_commit.tar.gz

	fix-jimtcl-link.patch
	"
builddir="$srcdir/$_pkgname-$_commit"

prepare() {
	default_prepare
	./bootstrap nosubmodule
}

build() {
	# --datadir:
	# avoid conflicts with release version of openocd to allow parallel
	# installations
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr \
		--sysconfdir=/etc \
		--mandir=/usr/share/man \
		--datadir=/usr/share/$pkgname \
		--localstatedir=/var \
		--disable-werror \
		--enable-cmsis-dap \
		--enable-sysfsgpio \
		--enable-bcm2835gpio \
		--disable-internal-jimtcl \
		--disable-internal-libjaylink
	make
}

check() {
	make check
}

package() {
	make DESTDIR="$pkgdir" install
	install -Dm644 contrib/60-openocd.rules \
		"$pkgdir"/usr/lib/udev/rules.d/60-$pkgname.rules

	# allow parallel install with release openocd
	mv "$pkgdir"/usr/bin/openocd "$pkgdir"/usr/bin/$pkgname
	mv "$pkgdir"/usr/share/man/man1/openocd.1 \
		"$pkgdir"/usr/share/man/man1/openocd-git.1
	sed -e 's/openocd/openocd-git/g' -i \
		"$pkgdir"/usr/share/man/man1/openocd-git.1
}

udev_rules() {
	pkgdesc="udev rules for $pkgname"
	depends="!openocd-udev-rules"
	amove usr/lib/udev/rules.d/60-$pkgname.rules
}

symlink() {
	pkgdesc="provide the 'openocd' cmd as symlink to 'openocd-git'"
	depends="$pkgname=$pkgver-r$pkgrel"
	mkdir -p "$subpkgdir"/usr/bin/
	ln -s openocd-git "$subpkgdir"/usr/bin/openocd
}

sha512sums="
82ecf9ca084adef91f88425c0f8d35ba5939ccee4086e11559b98e21a0287c1fca7c8f20b25a7d650458319a10c106fd484616ffef6f9dd6dd3d3bc847bc8317  openocd-ea2e26f7d521f5755b4bfda7bf12d99650277421.tar.gz
6fd2fbe11917bb7f181ba67812f63e75d51bcd19e4df6a7e22e1abb8f36a4f4bdc9202e815f560476ec184d54d84df8ae4dceb17f2429b9dc1d9503804dacb03  fix-jimtcl-link.patch
"
