#!/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 option for PostgreSQL and mariadb.
# TODO: add optional dependencies for Qt modules (e.g. qt/qtwayland)

PKGNAME=qtbase
PKGVERSION=5.13.0
PKGREVISION=1
PKGLICENSE="GPLv3 LGPLv21"
PKGSUMMARY="cross platform SDK for developing applications"
PKGDOWNLOAD="http://download.qt-project.org/official_releases/qt/${PKGVERSION%.*}/$PKGVERSION/submodules/$PKGNAME-everywhere-src-$PKGVERSION.tar.xz"
PKGDEPENDS="lib/pcre2"
PKGOPTIONS="CUPS
            DATABASES
            DBUS
            EVDEV
            FONTCONFIG
            FREETYPE
            GLIB
            GTK
            GUI
            HARFBUZZ
            ICU
            JPEG
            KMS
            LIBINPUT
            MTDEV
            OPENGL
            PNG
            SSL
            SYSLOG
            UDEV
            X
            ZLIB"

: ${CC:=clang}
: ${CXX:=clang++}
: ${CUPS:=yes}
: ${DATABASES:=sqlite}  # (list) none, sqlite
: ${DBUS:=yes}
: ${EVDEV:=yes}
: ${FONTCONFIG:=yes}
: ${FREETYPE:=yes}
: ${GLIB:=yes}
: ${GTK:=yes}
: ${GUI:=yes}           # requires FREETYPE
: ${HARFBUZZ:=yes}
: ${ICU:=yes}
: ${JPEG:=yes}
: ${KMS:=yes}
: ${LIBINPUT:=yes}
: ${MTDEV:=yes}
: ${OPENGL:=yes}
: ${PNG:=yes}
: ${SSL:=yes}
: ${SYSLOG:=no}
: ${UDEV:=yes}
: ${X:=yes}
: ${ZLIB:=yes}

if [ "$CXX" = "clang++" ]; then
	platform="linux-clang"
else
	platform="linux-g++"
fi

if [ "$CUPS" = "yes" ]; then
	PKGDEPENDS="print/cups $PKGDEPENDS"
	with_cups="-cups"
else
	with_cups="-no-cups"
fi

#
# It looks like there is no option to disable all SQL drivers to create a
# "white" list of desired drivers so disable all by default unless it is listed
# in DATABASES option.
#
# First, create a list with unsupported databases in vanilla.
#
with_databases="-no-sql-db2
                -no-sql-ibase
                -no-sql-oci
                -no-sql-odbc
                -no-sql-sqlite2
                -no-sql-tds"

if echo $DATABASES | grep -q "sqlite"; then
	PKGDEPENDS="databases/sqlite $PKGDEPENDS"
	with_databases="-sql-sqlite -system-sqlite $with_databases"
else
	with_databases="-no-sql-sqlite $with_databases"
fi

if [ "$DBUS" = "yes" ]; then
	PKGDEPENDS="network/dbus $PKGDEPENDS"
	with_dbus="-dbus-linked"
else
	with_dbus="-no-dbus"
fi

if [ "$EVDEV" = "yes" ]; then
	with_evdev="-evdev"
else
	with_evdev="-no-evdev"
fi

if [ "$FONTCONFIG" = "yes" ]; then
	PKGDEPENDS="fonts/fontconfig $PKGDEPENDS"
	with_fontconfig="-fontconfig"
else
	with_fontconfig="-no-fontconfig"
fi

if [ "$FREETYPE" = "yes" ]; then
	PKGDEPENDS="fonts/freetype $PKGDEPENDS"
	with_freetype="-system-freetype"
else
	with_freetype="-no-freetype"
fi

if [ "$GLIB" = "yes" ]; then
	PKGDEPENDS="lib/glib $PKGDEPENDS"
	with_glib="-glib"
else
	with_glib="-no-glib"
fi

if [ "$GTK" = "yes" ]; then
	PKGDEPENDS="graphics/gtk $PKGDEPENDS"
	with_gtk="-gtk"
else
	with_gtk="-no-gtk"
fi

if [ "$GUI" = "yes" ]; then
        if [ "$FREETYPE" != "yes" ]; then
                echo "warning: GUI requires FREETYPE" 1>&2
        fi

	with_gui="-gui"
else
	with_gui="-no-gui"
fi

if [ "$HARFBUZZ" = "yes" ]; then
	PKGDEPENDS="fonts/harfbuzz $PKGDEPENDS"
	with_harfbuzz="-system-harfbuzz"
else
	with_harfbuzz="-no-harfbuzz"
fi

if [ "$ICU" = "yes" ]; then
	PKGDEPENDS="lib/icu $PKGDEPENDS"
	with_icu="-icu"
else
	with_icu="-no-icu"
fi

if [ "$KMS" = "yes" ]; then
	PKGDEPENDS="graphics/libdrm $PKGDEPENDS"
	with_kms="-kms"
else
	with_kms="-no-kms"
fi

if [ "$LIBINPUT" = "yes" ]; then
	PKGDEPENDS="lib/libinput $PKGDEPENDS"
	with_libinput="-libinput"
else
	with_libinput="-no-libinput"
fi

if [ "$MTDEV" = "yes" ]; then
	PKGDEPENDS="lib/mtdev $PKGDEPENDS"
	with_mtdev="-mtdev"
else
	with_mtdev="-no-mtdev"
fi

if [ "$JPEG" = "yes" ]; then
	PKGDEPENDS="lib/libjpeg-turbo $PKGDEPENDS"
	with_jpeg="-system-libjpeg"
else
	with_jpeb="-no-libjpeg"
fi

if [ "$OPENGL" = "yes" ]; then
	with_opengl="-opengl"
else
	with_opengl="-no-opengl"
fi

if [ "$PNG" = "yes" ]; then
	PKGDEPENDS="lib/libpng $PKGDEPENDS"
	with_png="-system-libpng"
else
	with_png="-no-libpng"
fi

if [ "$SSL" = "yes" ]; then
	PKGDEPENDS="crypto/libressl $PKGDEPENDS"
	with_ssl="-ssl -openssl-linked"
else
	with_ssl="-no-openssl"
fi

if [ "$SYSLOG" = "yes" ]; then
	with_syslog="-syslog"
else
	with_syslog="-no-syslog"
fi

if [ "$UDEV" = "yes" ]; then
	PKGDEPENDS="core/eudev $PKGDEPENDS"
	with_udev="-libudev"
else
	with_udev="-no-libudev"
fi

if [ "$X" = "yes" ]; then
	with_x="-xcb -xcb-xlib -xcb-xinput"
else
	with_x="-no-xcb -no-xcb-xlib -no-xcb-xinput"
fi

if [ "$ZLIB" = "yes" ]; then
	PKGDEPENDS="compression/zlib $PKGDEPENDS"
	with_zlib="-system-zlib"
else
	with_zlib="-no-zlib"
fi

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

	# https://github.com/void-linux/void-packages/tree/master/srcpkgs/qt5/patches
	patch -p0 < ../patch-libressl.patch
	patch -p0 < ../patch-musl.patch
	PKG_CONFIG_PATH=/lib/pkgconfig \
	./configure \
		-opensource \
		-prefix / \
		-archdatadir /lib/qt \
		-datadir /share/qt \
		-docdir /share/doc/qt \
		-libexecdir /libexec \
		-confirm-license \
		-release \
		-platform $platform \
		-nomake examples \
		-nomake tests \
		-system-pcre \
		$with_cups \
		$with_databases \
		$with_dbus \
		$with_evdev \
		$with_fontconfig \
		$with_freetype \
		$with_glib \
		$with_gtk \
		$with_gui \
		$with_harfbuzz \
		$with_icu \
		$with_kms \
		$with_libinput \
		$with_opengl \
		$with_png \
		$with_ssl \
		$with_syslog \
		$with_udev \
		$with_x \
		$with_zlib
	make
	make install INSTALL_ROOT=$DESTDIR
	rm -f $DESTDIR/lib/libQt*.la

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