# Contributor: Sören Tempel <soeren+alpine@soeren-tempel.net>
# Contributor: Natanael Copa <ncopa@alpinelinux.org>
# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=graphviz
pkgver=12.1.1
pkgrel=0
pkgdesc="Graph Visualization Tools"
url="https://www.graphviz.org/"
arch="all"
license="EPL-1.0"
options="!check" # fixme: enable testsuite
_luaver=5.4
depends="font-cantarell"
depends_dev="
	cairo-dev
	expat-dev
	fontconfig-dev
	freetype-dev
	gd-dev
	gmp-dev
	libjpeg-turbo-dev
	libpng-dev
	libsm-dev
	libxext-dev
	pango-dev
	python3-dev
	zlib-dev
	"
makedepends="
	$depends_dev
	bison
	flex
	libltdl
	libxaw-dev
	lua$_luaver-dev
	m4
	swig
	tcl
	"
install="$pkgname.pre-deinstall"
triggers="$pkgname.trigger=/usr/lib/graphviz"
subpackages="
	$pkgname-dev
	$pkgname-libs
	$pkgname-doc
	py3-gv:_py3
	lua$_luaver-$pkgname:_lua
	$pkgname-graphs::noarch
	"
source="$pkgname-$pkgver.tar.xz::https://gitlab.com/api/v4/projects/4207231/packages/generic/graphviz-releases/$pkgver/graphviz-$pkgver.tar.xz"

# secfixes:
#   2.46.0-r0:
#     - CVE-2020-18032

prepare() {
 	default_prepare
	update_config_sub
}

build() {
	LIBPOSTFIX=/ \
	LUA=lua$_luaver \
	lua_suffix=$_luaver \
	CFLAGS="$CFLAGS -O2 -flto=auto" \
	CXXFLAGS="$CXXFLAGS -O2 -flto=auto" \
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr \
		--sysconfdir=/etc \
		--disable-python \
		--disable-silent-rules \
		--disable-static \
		--disable-dependency-tracking \
		--disable-ltdl-install \
		--enable-ltdl \
		--enable-sharp=no \
		--enable-go=no \
		--enable-guile=no \
		--enable-java=no \
		--enable-lua=yes \
		--enable-ocaml=no \
		--enable-perl=no \
		--enable-php=no \
		--enable-python3 \
		--enable-r=no \
		--enable-ruby=no \
		--enable-tcl=no \
		--without-included-ltdl \
		--with-gdk-pixbuf=yes \
		--with-ipsepcola=yes \
		--with-jpeg \
		--with-libgd=yes \
		--with-pangocairo=yes \
		--with-rsvg=yes \
		--with-x

	make
}

package() {
	# Install has race conditions...
	make DESTDIR="$pkgdir" \
		pkgconfigdir=/usr/lib/pkgconfig \
		install
}

_py3() {
	pkgdesc="Python3 extension for graphviz"
	depends="python3"

	amove usr/lib/graphviz/python3* \
		usr/lib/python3*
}

_lua() {
	pkgdesc="Lua$_luaver extension for graphviz"
	provides="lua-$pkgname=$pkgver-r$pkgrel"  # for backward compatibility
	replaces="lua-$pkgname"  # for backward compatibility

	mkdir -p "$subpkgdir"/usr/lib/lua
	mv "$pkgdir"/usr/lib/lua$_luaver/lua "$subpkgdir"/usr/lib/lua/$_luaver
	rmdir "$pkgdir"/usr/lib/lua$_luaver
	amove usr/lib/graphviz/lua
}

graphs() {
	pkgdesc="Demo graphs for graphviz"

	amove usr/share/graphviz/graphs
}

sha512sums="
644796fbb2a5b837564290bb90b43f193fc54998ba0d95311bf01404173c8c737165b9a53c37c4de7eff02b7362ead6cab4eff11d495180075322086569dabb7  graphviz-12.1.1.tar.xz
"
