# Maintainer: Marian Buschsieweke <marian.buschsieweke@posteo.net>
pkgname=ngspice
pkgver=42
pkgrel=0
pkgdesc="open source spice simulator for electric and electronic circuits"
url="https://ngspice.sourceforge.net/"
arch="all"
license="BSD-3-Clause"
makedepends="
	libedit-dev
	libxaw-dev
	readline-dev
	"
depends="python3"
subpackages="
	$pkgname-doc
	$pkgname-dev
	"
source="
	https://downloads.sourceforge.net/project/ngspice/ng-spice-rework/$pkgver/ngspice-$pkgver.tar.gz

	fix-unit-tests.patch
	fix-token-parsing-musl.patch
	"

build() {
	local config_flags="
		--prefix=/usr
		--mandir=/usr/share/man
		--infodir=/usr/share/info
		--with-readline=yes
		--enable-xspice
		--enable-cider
		--enable-openmp
		"

	# first build the lib:
	(
		mkdir -p libs
		cd libs

		# word splitting is intended for $config_flags
		# shellcheck disable=SC2086
		../configure $config_flags --with-ngshared
		make
	)

	(
		mkdir -p bin
		# build the binary ngspice, which is only built without --with-ngshared
		# TODO: this also means usr/bin/ngspice is not linked to its own library.
		# Just patching src/Makefile.am is not a quick fix, as the resulting
		# binary will segfault on startup. This is more a fundamental design
		# issue than a minor build system bug.
		cd bin
		# shellcheck disable=SC2086
		../configure $config_flags
		make
	)

	# copy the binary where the unit tests expect it
	cp bin/src/ngspice libs/src/
}

check() {
	cd libs
	make check
}

package() {
	make -C bin DESTDIR="$pkgdir" install
	make -C libs DESTDIR="$pkgdir" install
}

sha512sums="
37fbabd8e537ac8efe02731e54a0c586588e48010a8022daeeb53442b8ebdf3f1ea866e8d6eb180c130cabba2ae6675bc8741208ad2a20091d2c753eec9b4d55  ngspice-42.tar.gz
a9a8998a165c30545996c873954ea845351ce5e7d4e30a2695db794681867e9c1ddea9eb8aa22a98b31779a4ce28cb07efab23f13bfb0c59a0470b25f5558649  fix-unit-tests.patch
82ddc415b83700154a66c132362845981d890d1050766d8e5a6b4ee0746267b06f45ff5afb4f9cd194f40e6374410e8baa8bae0745969796cdccefe9a576cf82  fix-token-parsing-musl.patch
"
