# Maintainer: Marian Buschsieweke <marian.buschsieweke@posteo.net>
pkgname=ngspice
pkgver=44.2
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
	"
checkdepends="
	xvfb-run
	"
depends="python3"
subpackages="
	$pkgname-doc
	$pkgname-dev
	"
source="
	https://sourceforge.net/projects/ngspice/files/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
	xvfb-run make check
}

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

sha512sums="
b6cccf38d460f9aace5379db3b75c247b3aed04b902a2355e27b4ee3a64206b0ac8828c8685da3bf8e57c0746c10f39418849a4f31563e9f5ff7c05de0eb3ae2  ngspice-44.2.tar.gz
a9a8998a165c30545996c873954ea845351ce5e7d4e30a2695db794681867e9c1ddea9eb8aa22a98b31779a4ce28cb07efab23f13bfb0c59a0470b25f5558649  fix-unit-tests.patch
82ddc415b83700154a66c132362845981d890d1050766d8e5a6b4ee0746267b06f45ff5afb4f9cd194f40e6374410e8baa8bae0745969796cdccefe9a576cf82  fix-token-parsing-musl.patch
"
