#!/bin/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.
#

# TODO: add LUA, PYTHON option

PKGNAME=libpeas
PKGVERSION=1.23.90
PKGREVISION=1
PKGLICENSE="LGPL-2.1-only"
PKGSUMMARY="gobject-based plugins engine"
PKGDOWNLOAD="https://download.gnome.org/sources/$PKGNAME/${PKGVERSION%.*}/$PKGNAME-$PKGVERSION.tar.xz"
PKGDEPENDS="meson:build"
PKGOPTIONS="GTK INTROSPECTION VALA"

: ${CC:=clang}
: ${CFLAGS:=-O2}
: ${CXX:=clang++}
: ${CXXFLAGS:=-O2}
: ${LDFLAGS:=}
: ${GTK:=yes}
: ${INTROSPECTION:=yes}
: ${VALA:=no}

if [ "$GTK" = "yes" ]; then
	PKGDEPENDS="gtk $PKGDEPENDS"
	with_gtk="-D widgetry=true"
else
	with_gtk="-D widgetry=false"
fi

if [ "$INTROSPECTION" = "yes" ]; then
	PKGDEPENDS="gobject-introspection:build $PKGDEPENDS"
	with_introspection="-D introspection=true"
else
	with_introspection="-D introspection=false"
fi

if [ "$VALA" = "yes" ]; then
	PKGDEPENDS="vala $PKGDEPENDS"
	with_vala="-D vapi=true"
else
	with_vala="-D vapi=false"
fi

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

	# Tests do not build with llvm-objcopy.
	sed -i -e "/subdir('tests')/d" meson.build
	CC="$CC" \
	CFLAGS="$CFLAGS" \
	CXX="$CXX" \
	CXXFLAGS="$CXXFLAGS" \
	LDFLAGS="$LDFLAGS" \
	meson . build \
		--prefix / \
		--buildtype release \
		-D lua51=false \
		$with_gtk \
		$with_introspection \
		$with_vala
	CC="$CC" ninja -C build
	DESTDIR=$DESTDIR ninja -C build install

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