#!/bin/busybox sh
#
# Copyright (c) 2019 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=gtk
PKGVERSION=3.24.0
PKGREVISION=1
PKGLICENSE="LGPLv20"
PKGSUMMARY="multiplatform toolki"
PKGDOWNLOAD="https://download.gnome.org/sources/gtk+/3.24/gtk+-$PKGVERSION.tar.xz"
PKGDEPENDS="cairo libepoxy libxkbcommon"
PKGOPTIONS="CUPS NLS WAYLAND X"

: ${CC:=clang}
: ${CFLAGS:=-O2}
: ${CXX:=clang++}
: ${CXXFLAGS:=-O2}
: ${LDFLAGS:=}
: ${CUPS:=yes}
: ${NLS:=yes}
: ${WAYLAND:=yes}
: ${X:=yes}

if [ "$CUPS" = "yes" ]; then
	PKGDEPENDS="cups $PKGDEPENDS"
	with_cups="--enable-cups"
else
	with_cups="--disable-cups"
fi

if [ "$NLS" = "yes" ]; then
	PKGDEPENDS="gettext $PKGDEPENDS"
	with_nls="--enable-nls"
else
	with_nls="--disable-nls"
fi

if [ "$WAYLAND" = "yes" ] || [ "$X" = "yes" ]; then
	PKGDEPENDS="pango $PKGDEPENDS"

	if [ "$WAYLAND" = "yes" ]; then
		PKGDEPENDS="wayland $PKGDEPENDS"
		with_wayland="--enable-wayland-backend"
	else
		with_wayland="--disable-wayland-backend"
	fi

	if [ "$X" = "yes" ]; then
		PKGDEPENDS="fontconfig $PKGDEPENDS"
		PKGDEPENDS="libx11 $PKGDEPENDS"
		PKGDEPENDS="libxcomposite $PKGDEPENDS"
		PKGDEPENDS="libxcursor $PKGDEPENDS"
		PKGDEPENDS="libxdamage $PKGDEPENDS"
		PKGDEPENDS="libxext $PKGDEPENDS"
		PKGDEPENDS="libxinerama $PKGDEPENDS"
		PKGDEPENDS="libxfixes $PKGDEPENDS"
		PKGDEPENDS="libxi $PKGDEPENDS"
		PKGDEPENDS="libxrandr $PKGDEPENDS"

		with_x="--enable-x11-backend"
	else
		with_x="--disable-x11-backend"
	fi
fi

build()
{
	rm -rf gtk+-$PKGVERSION
	tar xvf gtk+-$PKGVERSION.tar.xz
	cd gtk+-$PKGVERSION

	CC="$CC" \
	CFLAGS="$CFLAGS" \
	CXX="$CXX" \
	CXXFLAGS="$CXXFLAGS" \
	LDFLAGS="$LDFLAGS" \
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix= \
		$with_nls \
		$with_wayland \
		$with_x
	make
	make install DESTDIR=$DESTDIR
	find $DESTDIR -type f -name "*.la" -delete

	cd ..
	rm -rf gtk+-$PKGVERSION
}

