# Maintainer: Thomas Dreibholz <dreibh@simula.no>
pkgname=bibtexconv
pkgver=2.2.3
pkgrel=1
pkgdesc="BibTeX file converter to export entries to other custom formats"
url="https://www.nntb.no/~dreibh/bibtexconv/"
arch="all"
license="GPL-3.0-or-later"
depends="file poppler-utils zip"
makedepends="
	bison
	cmake
	curl-dev
	flex
	gcc
	ninja
	openssl-dev
"
subpackages="
	$pkgname-ietf2bibtex:_ietf2bibtex:noarch
	$pkgname-bash-completion
	$pkgname-doc
"
source="https://www.nntb.no/~dreibh/bibtexconv/download/$pkgname-$pkgver.tar.xz"
builddir="$srcdir/$pkgname-$pkgver"

build() {
	cd "$builddir"
	cmake -B build -G Ninja \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_BUILD_TYPE=None .
	cmake --build build
}

package() {
	cd "$builddir"
	DESTDIR="$pkgdir" cmake --install build

	# Apply shebang fix for scripts matching Alpine's environment paths
	for directory in usr/bin usr/share/doc/bibtexconv/examples ; do
		if [ -d "$pkgdir/$directory" ]; then
			find "$pkgdir/$directory" -type f -exec sed -i \
				-e 's|^#!/usr/bin/env bash|#!/bin/bash|' \
				-e 's|^#!/usr/bin/env python3|#!/bin/python3|' \
				-e 's|^#!/usr/bin/env Rscript|#!/usr/bin/Rscript|' \
				{} +
		fi
	done
}

_ietf2bibtex() {
	pkgdesc="Create BibTeX entry for IETF documents (RFC or Internet Draft)"
	depends="$pkgname=$pkgver-r$pkgrel python3 py3-urllib3"

	mkdir -p "$subpkgdir/usr/bin"
	mv "$pkgdir/usr/bin/ietf2bibtex" "$subpkgdir/usr/bin/"

	# Captures the subpackage-specific text asset before the global 'doc' sweep
	mkdir -p "$subpkgdir/usr/share/doc/bibtexconv/examples"
	mv "$pkgdir/usr/share/doc/bibtexconv/examples/authors-fix.list" "$subpkgdir/usr/share/doc/bibtexconv/examples/"
}

sha512sums="
4e99dd6ea686af160e8cdcd73c3360b738de0dcbd8a0e3db28e2fbe2ec976a0594a02dd52fa86f2562d3a1ccf26aa78937deef499ba13dfb29bd02ac061eb3b6  bibtexconv-2.2.3.tar.xz
"
