# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
setvar pkgname = 'qt5-qtbase'
setvar pkgver = '5.9.1'
setvar _ver = ${pkgver/_p/-}
setvar _ver = ${_ver/_/-}
setvar _ver = ${_ver/beta0/beta}
setvar _ver = ${_ver/rc0/rc}
setvar _V = ${_ver/rc/RC}
setvar pkgrel = '1'
setvar pkgdesc = ""Qt5 - QtBase components""
setvar url = ""http://qt-project.org/""
setvar arch = ""all""
setvar license = ""LGPLv2 with exceptions or GPLv3 with exceptions""
setvar _sub = ""$pkgname-sqlite $pkgname-odbc $pkgname-postgresql $pkgname-mysql
	$pkgname-tds $pkgname-x11""
setvar depends = """"
setvar depends_dev = ""mesa-dev libice-dev libsm-dev libx11-dev libxext-dev
	libressl-dev fontconfig-dev freetype-dev glib-dev libpng-dev zlib-dev
	sqlite-dev dbus-dev perl $_sub""
setvar makedepends = ""$depends_dev
	bison
	cups-dev
	eudev-dev
	flex
	freetds-dev
	gawk
	gperf
	gtk+2.0-dev
	hicolor-icon-theme
	icu-dev
	libinput-dev
	libjpeg-turbo-dev
	libxkbcommon-dev
	libxi-dev
	libxrandr-dev
	libxrender-dev
	libxslt-dev
	libxv-dev
	mariadb-dev
	mtdev-dev
	pcre2-dev
	postgresql-dev
	unixodbc-dev
	xcb-util-dev
	xcb-util-image-dev
	xcb-util-keysyms-dev
	xcb-util-wm-dev
	xcb-util-renderutil-dev
	""
setvar install = """"
setvar subpackages = ""$pkgname-dev $pkgname-doc $_sub""
case (pkgver) {
*_beta*|*_rc* { setvar _rel = 'development_releases'}
* { setvar _rel = 'official_releases'}
}
setvar source = ""http://download.qt-project.org/$_rel/qt/${pkgver%.*}/$_ver/submodules/qtbase-opensource-src-$_V.tar.xz
	qt-musl-iconv-no-bom.patch
	libressl-compat.patch
	hack-openssl_test.patch
	""

setvar _qt5_prefix = "/usr/lib/qt5"
setvar _qt5_datadir = "/usr/share/qt5"

setvar builddir = ""$srcdir"/qtbase-opensource-src-${_V%-*}"
proc prepare {
	cd $builddir
	for i in $source {
		case (i) {
		*.patch {
			msg "Applying $i"
			patch -p1 -i "$srcdir"/$i || return 1
			}
		}
	}
	sed -i -e "s|-O2|$CXXFLAGS|" \
		-e "/^QMAKE_RPATH/s| -Wl,-rpath,||g" \
		-e "/^QMAKE_LFLAGS\s/s|+=|+= $LDFLAGS|g" \
		mkspecs/common/*.conf || return 1
}

proc build {
	local _arch

	cd $builddir

	./configure -confirm-license -opensource \
		-archdatadir $_qt5_prefix \
		-bindir  "$_qt5_prefix"/bin \
		-datadir $_qt5_datadir \
		-dbus-linked \
		-docdir /usr/share/doc/qt5 \
		-examplesdir /usr/share/doc/qt5/examples \
		-glib \
		-icu \
		-importdir "$_qt5_prefix"/imports \
		-libexecdir "$_qt5_prefix"/libexec \
		-no-rpath \
		-no-separate-debug-info \
		-no-pch \
		-nomake examples \
		-opengl \
		-openssl-linked \
		-optimized-qmake \
		-plugin-sql-mysql \
		-plugin-sql-odbc \
		-plugin-sql-psql \
		-plugin-sql-sqlite \
		-plugin-sql-tds \
		-plugindir "$_qt5_prefix"/plugins \
		-prefix /usr \
		-silent \
		-sysconfdir /etc/xdg \
		-system-libjpeg \
		-system-libpng \
		-system-pcre \
		-system-sqlite \
		-system-xcb \
		-system-zlib \
		-translationdir "$_qt5_datadir"/translations \
		-no-reduce-relocations \
		|| return 1
	make || return 1
}

proc package {
	cd $builddir
	make INSTALL_ROOT="$pkgdir" install || return 1
	mkdir -p "$pkgdir"/usr/bin/
	for i in "$pkgdir"/$_qt5_prefix/bin/* {
		setvar _name = ${i##*/}
		case (_name) {
		*.* {	setvar _dest = ""$pkgdir"/usr/bin/${_name%.*}-qt5.${_name##*.}"}
		* {	setvar _dest = ""$pkgdir"/usr/bin/${_name%.*}-qt5"}
		}
		ln -s ../lib/qt5/bin/"${_name}" $_dest
	}
}

proc _mv_files {
	local ifor i in @ARGV {
		mkdir -p "$subpkgdir"/${i%/*}
		mv "$pkgdir"/$i "$subpkgdir"/$i || return 1
	}
}

proc dev {
	cd $pkgdir
	_mv_files usr/lib/qt5/mkspecs \
		usr/lib/cmake \
		usr/lib/qt5/bin \
		usr/bin \
		$(find usr/ -name '*.prl') \
		|| return 1
	default_dev
}

proc sqlite {
	setvar pkgdesc = ""SQLite driver for Qt5's SQL classes""
	cd $pkgdir
	_mv_files usr/lib/qt5/plugins/sqldrivers/libqsqlite*
}

proc odbc {
	setvar pkgdesc = ""ODBC driver for Qt5's SQL classes""
	cd $pkgdir
	_mv_files usr/lib/qt5/plugins/sqldrivers/libqsqlodbc*
}

proc postgresql {
	setvar pkgdesc = ""PostgreSQL driver for Qt5's SQL classes""
	cd $pkgdir
	_mv_files usr/lib/qt5/plugins/sqldrivers/libqsqlpsql*
}

proc mysql {
	setvar pkgdesc = ""MySQL driver for Qt5's SQL classes""
	cd $pkgdir
	_mv_files usr/lib/qt5/plugins/sqldrivers/libqsqlmysql*
}

proc tds {
	setvar pkgdesc = ""TDS driver for Qt5's SQL classes""
	cd $pkgdir
	_mv_files usr/lib/qt5/plugins/sqldrivers/libqsqltds*
}

proc x11 {
	setvar pkgdesc = ""Qt5 GUI-related libraries""
	setvar depends = ""hicolor-icon-theme""
	cd $pkgdir
	_mv_files \
		usr/lib/libQt5EglFSDeviceIntegration.so.* \
		usr/lib/libQt5EglFsKmsSupport.so.* \
		usr/lib/libQt5Gui.so.* \
		usr/lib/libQt5OpenGL.so.* \
		usr/lib/libQt5PrintSupport.so.* \
		usr/lib/libQt5Widgets.so.* \
		usr/lib/libQt5XcbQpa.so.* \
		usr/lib/qt5/plugins/egldeviceintegrations \
		usr/lib/qt5/plugins/generic \
		usr/lib/qt5/plugins/image* \
		usr/lib/qt5/plugins/platform* \
		usr/lib/qt5/plugins/printsupport* \
		usr/lib/qt5/plugins/xcbglintegrations \
		|| return 1

	scanelf -Rn usr/ | egrep '(libX|libQt5Gui|libGL)' && return 1
	return 0
}

setvar sha512sums = ""b384e91b3fd88b2f32e826e3dd1c930213683a0fdbfd284a319204fa8d27c796b54324cf4a715f6bebd92fca6426e37cf0be5866fc1f6053b8758570ddb2fa45  qtbase-opensource-src-5.9.1.tar.xz
7d68421a14f0259535c977d8a521c98918193c107b76ac664571b12f5b0d7588a0d0e1297af412a26753a393b21f3f44c3274fa8ab5bc87f03705a3a03acb444  qt-musl-iconv-no-bom.patch
af284ebb51e3d903275f0f43901755fee0d0071a2fa4f5d6e8a4b00d9185d9d9fffba8e5cbda8c4aa2b3acde69fd26a449b23ad104f509fd4156f6908e0b43da  libressl-compat.patch
3bc1380268f077587114cfa535c58403f59c4c931902d0599633e1365f075d2e253b6f461e22ed3fe3adb12ef45ea97e1dbcbc1bbe0d151435703986f25c44c6  hack-openssl_test.patch""