# Contributor: Sören Tempel <soeren+alpine@soeren-tempel.net>
# Maintainer: Marian Buschsieweke <marian.buschsieweke@posteo.net>
pkgname=openocd-esp32
pkgver=0_git20250422
_gittag=0.12.0-esp32-20250422
pkgrel=0
pkgdesc="Fork of OpenOCD with ESP32 support"
url="https://github.com/espressif/openocd-esp32"
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
	linux-headers
	"
subpackages="$pkgname-dev $pkgname-doc $pkgname-udev-rules:udev_rules"
source="$pkgname-$pkgver.tar.gz::https://github.com/espressif/openocd-esp32/archive/v$_gittag.tar.gz"
builddir="$srcdir/openocd-esp32-$_gittag"

##
# XXX: Remove this aport when changes are upstreamed.
##

prepare() {
	default_prepare
	./bootstrap nosubmodule
}

build() {
	# --datadir:
	# avoid conflicts with upstream 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 \
		--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 upstream openocd
	mv "$pkgdir"/usr/bin/openocd "$pkgdir"/usr/bin/$pkgname
	mv "$pkgdir"/usr/share/man/man1/openocd.1 \
		"$pkgdir"/usr/share/man/man1/openocd-esp32.1
	sed -e 's/openocd/openocd-esp32/g' -i \
		"$pkgdir"/usr/share/man/man1/openocd-esp32.1
}

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

sha512sums="
c3e6cc95396fb89fb50d852815fa13d6f220eaa5769a3da1e6641b289fc7e72b3fe4ebaa276f85b82db0be3c56a6228794c0cc985eeade62d69ba9ae06f00dcd  openocd-esp32-0_git20250422.tar.gz
"
