#!/bin/sh
#
# Copyright (c) 2019-2021 David Demelier <markand@malikania.fr>
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#

PKGNAME=graphviz
PKGVERSION=2.40.1
PKGREVISION=1
PKGLICENSE="CUSTOM"
PKGSUMMARY="graph drawing programs"
PKGDOWNLOAD="http://files.malikania.fr/distfiles/$PKGNAME-$PKGVERSION.tar.gz"
PKGDEPENDS="libtool bash:build"
PKGOPTIONS="FONTCONFIG GD GTK PANGO TTF WEBP X XML"

: ${CHOST:=$(uname -m)-linux-musl}
: ${CBUILD:=$(uname -m)-linux-musl}
: ${CC:=clang}
: ${CFLAGS:=-O2}
: ${CXX:=clang++}
: ${CXXFLAGS:=-O2}
: ${LDFLAGS:=}
: ${LIBS:=}
: ${FONTCONFIG:=yes}
: ${GD:=yes}
: ${GTK:=yes}
: ${PANGO:=yes}
: ${TTF:=yes}
: ${WEBP:=no}
: ${X:=yes}
: ${XML:=yes}

if [ "$FONTCONFIG" = "yes" ]; then
	PKGDEPENDS="fontconfig $PKGDEPENDS"
	with_fontconfig="--with-fontconfig"
else
	with_fontconfig="--without-fontconfig"
fi

if [ "$GD" = "yes" ]; then
	PKGDEPENDS="libgd $PKGDEPENDS"
	with_gd="--with-libgd"
else
	with_gd="--without-libgd"
fi

if [ "$GTK" = "yes" ]; then
	PKGDEPENDS="gtk $PKGDEPENDS"
	with_gtk="--with-gdk --with-gtk --with-gdk-pixbuf"
else
	with_gtk="--without-gdk --without-gtk --without-gdk-pixbuf"
fi

if [ "$PANGO" = "yes" ]; then
	PKGDEPENDS="pango $PKGDEPENDS"
	with_pango="--with-pangocairo"
else
	with_pango="--without-pangocairo"
fi

if [ "$TTF" = "yes" ]; then
	PKGDEPENDS="freetype $PKGDEPENDS"
	with_ttf="--with-freetype2"
else
	with_ttf="--without-freetype2"
fi

if [ "$WEBP" = "yes" ]; then
	PKGDEPENDS="graphics/webp $PKGDEPENDS"
	with_webp="--with-webp"
else
	with_webp="--without-webp"
fi

if [ "$X" = "yes" ]; then
	PKGDEPENDS="libx11 $PKGDEPENDS"
	PKGDEPENDS="libxaw $PKGDEPENDS"
	PKGDEPENDS="libxmu $PKGDEPENDS"
	PKGDEPENDS="libxpm $PKGDEPENDS"
	PKGDEPENDS="libxt $PKGDEPENDS"
	with_x="--with-x"
else
	with_x="--without-x"
fi

if [ "$XML" = "yes" ]; then
	PKGDEPENDS="expat $PKGDEPENDS"
	with_expat="--with-expat"
else
	with_expat="--without-expat"
fi

build()
{
	rm -rf $PKGNAME-$PKGVERSION
	tar xvf $PKGNAME-$PKGVERSION.tar.gz
	cd $PKGNAME-$PKGVERSION

	# TODO: enable features as they become available.
	# --disable-tcl because it installs to /lib64.
	CONFIG_SHELL=/bin/bash \
	CC="$CC" \
	CFLAGS="$CFLAGS" \
	CXX="$CXX" \
	CXXFLAGS="$CXXFLAGS" \
	LDFLAGS="$LDFLAGS" \
	LIBS="$LIBS" \
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix= \
		--libdir=/lib \
		--disable-tcl \
		--disable-ltdl-install \
		--without-included-ltdl \
		--without-qt \
		--without-devil \
		--without-poppler \
		--without-rsvg \
		--without-ghostscript \
		--without-visio \
		--without-lasi \
		--without-glitz \
		--without-gtkgl \
		--without-gtkglext \
		--without-gts \
		--without-ann \
		--without-glade \
		--without-ming \
		--without-qt\
		--without-quartz \
		--without-gdiplus \
		--without-glut \
		--without-smyrna \
		--without-ortho \
		--without-digcola \
		--without-ipsepcola \
		--with-sfdp \
		$with_fontconfig \
		$with_gd \
		$with_gtk \
		$with_pango \
		$with_ttf \
		$with_webp \
		$with_x \
		$with_xml
	make
	make install DESTDIR=$DESTDIR
	find $DESTDIR -type f -name "*.la" -delete

	cd ..
	rm -rf $PKGNAME-$PKGVERSION
}
