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

PKGNAME=webkitgtk
PKGVERSION=2.24.3
PKGREVISION=1
PKGLICENSE="CUSTOM"
PKGSUMMARY="web content engine for Gtk"
PKGDOWNLOAD="https://webkitgtk.org/releases/$PKGNAME-$PKGVERSION.tar.xz"
PKGDEPENDS="accessibility/atk
            dev/cmake:build
            dev/ninja:build
            fonts/fontconfig
            fonts/freetype
            fonts/harfbuzz
            graphics/cairo
            graphics/gtk
            graphics/libpng
            graphics/libwebp
            lib/glib
            lib/icu
            lib/libtasn1
            network/libsoup
            perl/perl:build
            python/python:build
            ruby/ruby:build
            text/libxslt"
PKGOPTIONS="GEOLOCATION
            GSTREAMER
            HYPHEN
            INTROSPECTION
            JPEG
            NOTIFICATIONS
            LIBSECRET
            OPENGL
            SPELL
            WAYLAND
            WOFF2
            X"

: ${CC:=clang}
: ${CFLAGS:=-O2}
: ${CXX:=clang++}
: ${CXXFLAGS:=-O2}
: ${GEOLOCATION:=no}    # TODO: set to on once geoclue is there.
: ${GSTREAMER:=yes}
: ${HYPHEN:=no}         # TODO: set to on once hyphen is there.
: ${INTROSPECTION:=yes}
: ${JPEG:=no}           # TODO: set to on once openjpeg is there.
: ${NOTIFICATIONS:=yes}
: ${LIBSECRET:=yes}
: ${OPENGL:=yes}
: ${SPELL:=no}          # TODO: set to on once enchant is there.
: ${WAYLAND:=yes}
: ${WOFF2:=no}          # TODO: set to on once woff2 is there.
: ${X:=yes}

if [ "$GEOLOCATION" = "yes" ]; then
	PKGDEPENDS="network/geoclue $PKGDEPENDS"
	with_geolocation="-DENABLE_GEOLOCATION=On"
else
	with_geolocation="-DENABLE_GEOLOCATION=Off"
fi

if [ "$GSTREAMER" = "yes" ]; then
	PKGDEPENDS="multimedia/gstreamer multimedia/gst-plugins-base $PKGDEPENDS"
	with_gstreamer="-DENABLE_VIDEO=On -DENABLE_WEB_AUDIO=On"
else
	with_gstreamer="-DENABLE_VIDEO=Off -DENABLE_WEB_AUDIO=Off"
fi

if [ "$HYPHEN" = "yes" ]; then
	with_hyphen="-DUSE_LIBHYPHEN=On"
else
	with_hyphen="-DUSE_LIBHYPHEN=Off"
fi

if [ "$INTROSPECTION" = "yes" ]; then
	PKGDEPENDS="dev/gobject-introspection:build $PKGDEPENDS"
	with_introspection="-DENABLE_INTROSPECTION=On"
else
	with_introspection="-DENABLE_INTROSPECTION=Off"
fi

if [ "$JPEG" = "yes" ]; then
	PKGDEPENDS="graphics/openjpeg $PKGDEPENDS"
	with_jpeg="-DUSE_OPENJPEG=On"
else
	with_jpeg="-DUSE_OPENJPEG=Off"
fi

if [ "$NOTIFICATIONS" = "yes" ]; then
	PKGDEPENDS="desktop/notifications $PKGDEPENDS"
	with_notifications="-DUSE_LIBNOTIFY=On"
else
	with_notifications="-DUSE_LIBNOTIFY=Off"
fi

if [ "$LIBSECRET" = "yes" ]; then
	PKGDEPENDS="security/libsecret $PKGDEPENDS"
	with_libsecret="-DUSE_LIBSECRET=On"
else
	with_libsecret="-DUSE_LIBSECRET=Off"
fi

if [ "$OPENGL" = "yes" ]; then
	PKGDEPENDS="graphics/mesa $PKGDEPENDS"
	with_opengl="-DENABLE_OPENGL=On"
else
	with_opengl="-DENABLE_OPENGL=Off"
fi

if [ "$SPELL" = "yes" ]; then
	with_spell="-DENABLE_SPELLCHECK=On"
else
	with_spell="-DENABLE_SPELLCHECK=Off"
fi

if [ "$WAYLAND" = "yes" ]; then
	with_wayland="-DENABLE_WAYLAND_TARGET=On"
else
	with_wayland="-DENABLE_WAYLAND_TARGET=Off"
fi

if [ "$WOFF2" = "yes" ]; then
	PKGDEPENDS="fonts/woff2 $PKGDEPENDS"
	with_woff2="-DUSE_WOFF2=On"
else
	with_woff2="-DUSE_WOFF2=Off"
fi

if [ "$X" = "yes" ]; then
	PKGDEPENDS="x11/libxcomposite
		    x11/libxdamage
		    x11/libxrender
		    x11/libxt
		    $PKGDEPENDS"
	with_x="-DENABLE_X11_TARGET=On"
else
	with_x="-DENABLE_X11_TARGET=Off"
fi

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

	# -DENABLE_PLUGIN_PROCESS_GTK2=Off: in Vanilla Gtk 2 is deprecated.
	patch -p0 < ../patch-clang.patch
	patch -p1 < ../patch-musl.patch
	patch -p1 < ../patch-isystem.patch
	mkdir build && cd build
	cmake .. \
		-GNinja \
		-DBUILD_SHARED_LIBS=On \
		-DCMAKE_BUILD_TYPE=Release \
		-DCMAKE_CXX_COMPILER="$CXX" \
		-DCMAKE_CXX_FLAGS="$CXXFLAGS" \
		-DCMAKE_C_COMPILER="$CC" \
		-DCMAKE_C_FLAGS="$CFLAGS" \
		-DCMAKE_INSTALL_PREFIX= \
		-DLIB_INSTALL_DIR=/lib \
		-DPORT=GTK \
		-DENABLE_PLUGIN_PROCESS_GTK2=Off \
		$with_geolocation \
		$with_gstreamer \
		$with_hyphen \
		$with_introspection \
		$with_jpeg \
		$with_notifications \
		$with_libsecret \
		$with_opengl \
		$with_spell \
		$with_wayland \
		$with_woff2 \
		$with_x
	CC="$CC" CXX="$CXX" ninja
	DESTDIR=$DESTDIR ninja install
	cd ..

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