# Template file for 'libreoffice'
pkgname=libreoffice
version=24.2.2.2
revision=2
build_style=meta
make_build_target="build"
hostmakedepends="automake flex gperf hyphen icu libtool openldap which gettext xz
 tar perl-Archive-Zip pkg-config qt5-qmake sane unzip zip python3-setuptools
 fontforge python3-lxml qt6-base gtk4-devel gobject-introspection gnupg"
makedepends="CoinMP-devel apr-devel avahi-libs-devel clucene-devel
 frameworkintegration-devel glyphy-devel gpgmepp-devel gst-plugins-base1-devel
 gtk+3-devel gtk4-devel hunspell-devel hyphen-devel libXt-devel libabw-devel glm
 libatomic_ops-devel libbluetooth-devel libcdr-devel libe-book-devel
 libepubgen-devel libetonyek-devel libexttextcat-devel libfbclient3-devel
 libfreehand-devel boost-devel zlib-devel bzip2-devel libxslt-devel frozen
 libgcrypt-devel libgltf-devel libldap-devel libmariadbclient-devel libmspub-devel
 libmwaw-devel libnumbertext-devel libodfgen-devel libopenjpeg2-devel
 liborcus-devel libargon2-devel zxcvbn-c-devel libzmf-devel
 libpagemaker-devel libqxp-devel librsvg-devel libvisio-devel libwpg-devel
 libwps-devel lpsolve-devel mdds mythes-devel neon-devel nss-devel nspr-devel
 postgresql-libs-devel python3-devel qt5-devel redland-devel sane-devel
 telepathy-glib-devel unixodbc-devel vigra-devel libcppunit-devel imath-devel
 qt6-base-devel libwebp-devel libgirepository-devel icu-devel twaindsm-devel
 libepoxy-devel libcurl-devel cairo-devel graphite-devel libjpeg-turbo-devel
 expat-devel fontconfig-devel freetype-devel harfbuzz-devel gpgmepp-devel
 lcms2-devel openssl-devel libpng-devel tiff-devel librevenge-devel
 xmlsec1-devel poppler-cpp-devel libxml2-devel zxing-cpp-devel"
# Add the previously installed default languages as well
depends="libreoffice-common>=${version}_${revision}
 libreoffice-base>=${version}_${revision}
 libreoffice-calc>=${version}_${revision}
 libreoffice-draw>=${version}_${revision}
 libreoffice-fonts>=${version}_${revision}
 libreoffice-gnome>=${version}_${revision}
 libreoffice-impress>=${version}_${revision}
 libreoffice-math>=${version}_${revision}
 libreoffice-postgresql>=${version}_${revision}
 libreoffice-writer>=${version}_${revision}
 libreoffice-xtensions>=${version}_${revision}
 libreoffice-i18n-en-US>=${version}_${revision}
 libreoffice-i18n-de>=${version}_${revision}
 libreoffice-i18n-es>=${version}_${revision}
 libreoffice-i18n-fr>=${version}_${revision}
 libreoffice-i18n-it>=${version}_${revision}
 libreoffice-i18n-pl>=${version}_${revision}
 libreoffice-i18n-pt>=${version}_${revision}"
checkdepends="gdb"
short_desc="Productivity suite"
maintainer="Érico Nogueira <ericonr@disroot.org>"
license="GPL-3.0-or-later"
homepage="https://www.libreoffice.org/"
nocross="Several dependencies are nocross=yes"

# Source, dictionary, help and translations
_baseurl="https://download.documentfoundation.org/libreoffice/src/${version%.*}"
# Please don't remove this mirror, it's faster for me
_baseurl="https://download.nus.edu.sg/mirror/tdf/libreoffice/src/${version%.*}"
# _baseurl="https://mirrors.nju.edu.cn/tdf/libreoffice/src/${version%.*}"
_addurl="https://dev-www.libreoffice.org/src"
_exturl="https://dev-www.libreoffice.org/extern"

# Additional distfiles and checksums can be found in
# the file 'download.lst' after configure was run.
distfiles="
 ${_baseurl}/${pkgname}-${version}.tar.xz
 ${_baseurl}/${pkgname}-help-${version}.tar.xz
 ${_baseurl}/${pkgname}-translations-${version}.tar.xz
 ${_addurl}/798b2ffdc8bcfe7bca2cf92b62caf685-rhino1_5R5.zip
 ${_addurl}/a7983f859eafb2677d7ff386a023bc40-xsltml_2.1.2.zip
 ${_addurl}/ace6ab49184e329db254e454a010f56d-libxml-1.1.7.zip
 ${_addurl}/language-subtag-registry-2022-08-08.tar.bz2
 ${_addurl}/17410483b5b5f267aa18b7e00b65e6e0-hsqldb_1_8_0.zip
 ${_addurl}/d8bd5eed178db6e2b18eeed243f85aa8-flute-1.1.6.zip
 ${_addurl}/ba2930200c9f019c2d93a8c88c651a0f-flow-engine-0.9.4.zip
 ${_addurl}/box2d-2.4.1.tar.gz
 ${_addurl}/libcmis-0.6.1.tar.xz
 ${_addurl}/libstaroffice-0.0.7.tar.xz
 ${_addurl}/pdfium-6179.tar.bz2
 ${_exturl}/8249374c274932a21846fa7629c2aa9b-officeotron-0.7.4-master.jar
 ${_exturl}/odfvalidator-0.9.0-RC2-SNAPSHOT-jar-with-dependencies-2726ab578664434a545f8379a01a9faffac0ae73.jar
 ${_addurl}/dtoa-20180411.tgz
 ${_addurl}/beeca87be45ec87d241ddd0e1bad80c1-bsh-2.0b6-src.zip
 ${_addurl}/35c94d2df8893241173de1d16b6034c0-swingExSrc.zip
 ${_addurl}/b7cae45ad2c23551fd6ccb8ae2c1f59e-numbertext_0.9.5.oxt
 ${_addurl}/eeb2c7ddf0d302fba4bfc6e97eac9624-libbase-1.1.6.zip
 ${_addurl}/3bdf40c0d199af31923e900d082ca2dd-libfonts-1.1.6.zip
 ${_addurl}/3404ab6b1792ae5f16bbd603bd1e1d03-libformula-1.1.7.zip
 ${_addurl}/db60e4fde8dd6d6807523deb71ee34dc-liblayout-0.2.10.zip
 ${_addurl}/97b2d4dba862397f446b217e2b623e71-libloader-1.1.6.zip
 ${_addurl}/8ce2fcd72becf06c41f7201d15373ed9-librepository-1.1.6.zip
 ${_addurl}/f94d9870737518e3b597f9265f4e9803-libserializer-1.1.6.zip
 ${_addurl}/39bb3fcea1514f1369fcfc87542390fd-sacjava-1.3.zip
 ${_addurl}/skia-m116-2ddcf183eb260f63698aa74d1bb380f247ad7ccd.tar.xz
 ${_addurl}/frozen-1.1.1.tar.gz
 ${_addurl}/dragonbox-1.1.3.tar.gz
 ${_addurl}/Java-WebSocket-1.5.4.tar.gz
 ${_addurl}/zxcvbn-c-2.5.tar.gz
"
checksum="c205a65042f65c94b54ea310344b851043633c3eb5259f4e567d9341aae5e45e
 e4ca31ec501c3c59e6c7aa4cc1279185675b088b2d042f17a7e6b0a8304ce025
 925016b4172c1dcfb2774cdb0376df949241e369375399b9875b154e65f319b2
 1fb458d6aab06932693cc8a9b6e4e70944ee1ff052fa63606e3131df34e21753
 75823776fb51a9c526af904f1503a7afaaab900fba83eda64f8a41073724c870
 7d2797fe9f79a77009721e3f14fa4a1dec17a6d706bdc93f85f1f01d124fab66
 e2d9224e0e50fc8ad12a3cf47396bbcadf45b2515839d4770432653a88972c00
 d30b13f4ba2e3b6a2d4f020c0dee0a9fb9fc6fbcc2d561f36b78da4bf3802370
 1b5b24f7bc543c0362b667692f78db8bab4ed6dafc6172f104d0bd3757d8a133
 233f66e8d25c5dd971716d4200203a612a407649686ef3b52075d04b4c9df0dd
 d6b4650ff897ee1ead27cf77a5933ea197cbeef6705638dd181adc2e816b23c2
 d54d19d86153dbc88e2d468f7136269a2cfe71b73227e12fded01d29ac268074
 f94fb0ad8216f97127bedef163a45886b43c62deac5e5b0f5e628e234220c8db
 4d3f08fe0e2fda86246832085426616826dcca0912202874428bfbc24d13d95c
 f2443f27561af52324eee03a1892d9f569adc8db9e7bca55614898bc2a13a770
 d55495ab3a86544650587de2a72180ddf8bfc6376d14ddfa923992dbc86a06e0
 0082d0684f7db6f62361b76c4b7faba19e0c7ce5cb8e36c4b65fea8281e711b4
 9e93c73e23aff644b17dfff656444474c14150e7f3b38b19635e622235e01c96
 64585ac36a81291a58269ec5347e7e3e2e8596dbacb9221015c208191333c6e1
 1568ed1d2feb8210bb5de61d69574a165cded536cfa17c6953c9064076469de2
 75c80359c9ce343c20aab8a36a45cb3b9ee7c61cf92c13ae45399d854423a9ba
 e0531091787c0f16c83965fdcbc49162c059d7f0c64669e7f119699321549743
 5826d1551bf599b85742545f6e01a0079b93c1b2c8434bf409eddb3a29e4726b
 e1fb87f3f7b980d33414473279615c4644027e013012d156efa538bc2b031772
 3d853b19b1d94a6efa69e7af90f7f2b09ecf302913bee3da796c15ecfebcfac8
 abe2c57ac12ba45d83563b02e240fa95d973376de2f720aab8fe11f2e621c095
 05640a1f6805b2b2d7e2cb9c50db9a5cb084e3c52ab1a71ce015239b4a1d4343
 085f2112c51fa8c1783fac12fbd452650596415121348393bb51f0f7e85a9045
 2223ebce534458a37826e8fe4f24635b0712cde7ed1bd3208f089f6fdd796e01
 f7c7075750e8fceeac081e9ef01944f221b36d9725beac8681cbd2838d26be45
 09d63b05e9c594ec423778ab59b7a5aa1d76fdd71d25c7048b0258c4ec9c3384
 a6828b35d1f938fee2335945f3d3c563cbbfa58ce7eb0bf72778d0fa7a550720
 77d6c6ecb35952a8d8ce7f736b7a2bf466275c48210e309b73782d6b7e84dffd"
skip_extraction="
 798b2ffdc8bcfe7bca2cf92b62caf685-rhino1_5R5.zip
 a7983f859eafb2677d7ff386a023bc40-xsltml_2.1.2.zip
 ace6ab49184e329db254e454a010f56d-libxml-1.1.7.zip
 language-subtag-registry-2022-08-08.tar.bz2
 17410483b5b5f267aa18b7e00b65e6e0-hsqldb_1_8_0.zip
 d8bd5eed178db6e2b18eeed243f85aa8-flute-1.1.6.zip
 ba2930200c9f019c2d93a8c88c651a0f-flow-engine-0.9.4.zip
 box2d-2.4.1.tar.gz
 libcmis-0.6.1.tar.xz
 libstaroffice-0.0.7.tar.xz
 pdfium-6179.tar.bz2
 8249374c274932a21846fa7629c2aa9b-officeotron-0.7.4-master.jar
 odfvalidator-0.9.0-RC2-SNAPSHOT-jar-with-dependencies-2726ab578664434a545f8379a01a9faffac0ae73.jar
 dtoa-20180411.tgz
 beeca87be45ec87d241ddd0e1bad80c1-bsh-2.0b6-src.zip
 35c94d2df8893241173de1d16b6034c0-swingExSrc.zip
 b7cae45ad2c23551fd6ccb8ae2c1f59e-numbertext_0.9.5.oxt
 eeb2c7ddf0d302fba4bfc6e97eac9624-libbase-1.1.6.zip
 3bdf40c0d199af31923e900d082ca2dd-libfonts-1.1.6.zip
 3404ab6b1792ae5f16bbd603bd1e1d03-libformula-1.1.7.zip
 db60e4fde8dd6d6807523deb71ee34dc-liblayout-0.2.10.zip
 97b2d4dba862397f446b217e2b623e71-libloader-1.1.6.zip
 8ce2fcd72becf06c41f7201d15373ed9-librepository-1.1.6.zip
 f94d9870737518e3b597f9265f4e9803-libserializer-1.1.6.zip
 39bb3fcea1514f1369fcfc87542390fd-sacjava-1.3.zip
 skia-m116-2ddcf183eb260f63698aa74d1bb380f247ad7ccd.tar.xz
 frozen-1.1.1.tar.gz
 dragonbox-1.1.3.tar.gz
 Java-WebSocket-1.5.4.tar.gz
 zxcvbn-c-2.5.tar.gz
"
replaces="libreoffice-firebird<6.2.4.2_1"

build_options="java"
desc_option_java="Enable Java support"

CXXFLAGS="-DGLM_ENABLE_EXPERIMENTAL -DU_USING_ICU_NAMESPACE=1"

case "$XBPS_TARGET_MACHINE" in
	i686*)	# Broken unit tests
		CXXFLAGS+=" -DDISABLE_CVE_TESTS=1"
		;;
	x86_64*|ppc64*) build_options_default="java"
		makedepends+=" openjdk17 apache-ant"
		;;
esac

# Move files listed in a <name>_list.txt into $PKGDESTDIR
_split() {
	local list file dir destdir
	destdir="${DESTDIR}/all"
	list=${wrksrc}/file-lists/${1}_list.txt
	# skip non-existing file lists
	[ -f ${list} ] || msg_error "There is no ${1}_list.txt file!\n"
	grep -v '^%dir' "$list" | sort -u |
	while read file; do
		dir="${file%/*}"
		[ -d "${PKGDESTDIR}/${dir}" ] || vmkdir "${dir}"
		mv ${destdir}/${file} ${PKGDESTDIR}/${dir}/
	done
	# Move appdata file, if any
	file=usr/share/metainfo/libreoffice-${1}.appdata.xml
	if [ -f ${destdir}/${file} ]; then
		dir="${file%/*}"
		[ -d "${PKGDESTDIR}/${dir}" ] || vmkdir "${dir}"
		mv ${destdir}/${file} ${PKGDESTDIR}/${dir}/
	fi
}

unset _languages
_add_lang() {
	local pkglang tmp
	local deps=
	local pkg=$1
	local name=$2
	pkglang=$pkg
	shift 2
	while case "$1" in
		-h*)	tmp=${1#-h}; deps="$deps hunspell-${tmp:-$pkg}" ;;
		-H*)	tmp=${1#-H}; deps="$deps hyphen-${tmp:-$pkg}" ;;
		-m*)	tmp=${1#-m}; deps="$deps mythes-${tmp:-$pkg}" ;;
		-l*)	pkglang=${1#-l} ;;
		*)	false ;;
	esac; do shift; done
	_languages="$_languages ${pkglang//_/-}"
	eval "libreoffice-i18n-${pkg}_package() {
		short_desc+=\" - ${name} language pack\"
		depends='$deps'
		pkg_install() {
			_split lang_${pkglang}
		}
	}"
}

# _add_lang ab		"Abkhazian"
_add_lang af		"Afrikaans" -h -H
_add_lang am		"Amharic"
# _add_lang an		"Aragonese"
_add_lang ar		"Arabic" -h -m
_add_lang as		"Assamese"
_add_lang ast		"Asturian"
# _add_lang az		"Azerbaijani"
_add_lang be		"Belarusian" -h -H
_add_lang bg		"Bulgarian" -h -H -m
_add_lang bn		"Bengali" -h
_add_lang bn-IN		"Bengali (India)" -lbn_IN -hbn
_add_lang bo		"Tibetan" -h
_add_lang br		"Breton" -h
_add_lang brx		"Bodo"
_add_lang bs		"Bosnian" -h
_add_lang ca		"Catalan" -hca_ES -H -m
_add_lang ca-valencia	"Catalan (Valencian)" -lca_valencia -hca_ES -Hca -mca
_add_lang ckb		"Central Kurdish" -h
_add_lang cs		"Czech" -h -H -m
_add_lang cy		"Welsh (Cymraeg)"
_add_lang da		"Danish" -hda_DK -H -m
_add_lang de		"German" -h -H -m
_add_lang dgo		"Dogri proper"
_add_lang dsb		"Lower Sorbian"
_add_lang dz		"Dzongkha"
_add_lang el		"Greek" -hel_GR -H
_add_lang en-GB		"English (UK)" -len_GB -hen -hen_GB -Hen -men
_add_lang en-US		"English (US)" -len_US -hen -Hen -men
_add_lang en-ZA		"English (South Africa)" -len_ZA -hen -hen_GB -Hen -men
_add_lang eo		"Esperanto" -h -H -m
_add_lang es		"Spanish" -h -H -m
_add_lang et		"Estonian" -h -H
_add_lang eu		"Basque"
_add_lang fa		"Persian (Farsi)" -h
_add_lang fi		"Finnish"
_add_lang fr		"French" -hfr_FR -H -m
_add_lang fur		"Friulian"
_add_lang fy		"Frisian"
_add_lang ga		"Irish"
_add_lang gd		"Scottish Gaelic" -h
_add_lang gl		"Galician" -h -H -m
_add_lang gu		"Gujarati" -h
_add_lang gug		"Guaraní (Paraguay)" -h -m
_add_lang he		"Hebrew" -h
_add_lang hi		"Hindi" -h
_add_lang hr		"Croatian" -h -H
_add_lang hsb		"Upper Sorbian"
_add_lang hu		"Hungarian" -h -H -m
_add_lang id		"Indonesian" -h -H -m
_add_lang is		"Icelandic" -h -H -m
_add_lang it		"Italian" -hit_IT -Hit_IT -mit_IT
_add_lang ja		"Japanese"
# _add_lang jv		"Bedjond"
_add_lang ka		"Georgian"
_add_lang kab		"Kabyle"
_add_lang kk		"Kazakh"
# _add_lang kl		"Aklanon"
_add_lang km		"Khmer"
_add_lang kmr-Latn	"Kurmanji Kurdish (Latin)" -lkmr_Latn -hkmr
_add_lang kn		"Kannada"
_add_lang ko		"Korean" -h
_add_lang kok		"Konkani"
_add_lang ks		"Kashmiri"
# _add_lang ky		"Aka-Kol"
_add_lang lb		"Luxembourgish"
_add_lang lo		"Lao" -h
_add_lang lt		"Lithuanian" -h -H
_add_lang lv		"Latvian" -h -H -m
_add_lang mai		"Maithili"
_add_lang mk		"Macedonian"
_add_lang ml		"Malayalam"
_add_lang mn		"Mongolian" -h -H
_add_lang mni		"Meithei (Manipuri)"
_add_lang mr		"Marathi"
_add_lang my		"Burmese"
_add_lang nb		"Norwegian (Bokmal)" -hno -Hno -mno
_add_lang ne		"Nepali" -h -m
_add_lang nl		"Dutch" -hnl_NL -Hnl_NL
_add_lang nn		"Nynorsk" -hno -Hno -mno
_add_lang nr		"Ndebele (South)"
_add_lang nso		"Northern Sotho"
_add_lang oc		"Occitan" -h
_add_lang om		"Oromo"
_add_lang or		"Oriya"
_add_lang pa-IN		"Punjabi (India)" -lpa_IN
_add_lang pl		"Polish" -h -H -m
_add_lang pt		"Portuguese" -hpt_PT -Hpt_PT -mpt_PT
_add_lang pt-BR		"Portuguese (Brazil)" -lpt_BR -hpt_BR -Hpt_BR -mpt_BR
_add_lang ro		"Romanian" -hro_RO -H -m
_add_lang ru		"Russian" -hru_RU -H -m
_add_lang rw		"Kinyarwanda"
_add_lang sa-IN		"Sanskrit (India)" -lsa_IN
# _add_lang sah		"Yakut"
_add_lang sat		"Santali"
_add_lang sd		"Sindhi"
_add_lang si		"Sinhala" -h
_add_lang sid		"Sidamo"
_add_lang sk		"Slovak" -h -H -m
_add_lang sl		"Slovenian" -h -H -m
_add_lang sq		"Albanian" -h -H
_add_lang sr		"Serbian" -h -H
_add_lang sr-Latn	"Serbian (Latin)" -lsr_Latn -hsr -Hsr
_add_lang ss		"Swati"
_add_lang st		"Southern Sotho"
_add_lang sv		"Swedish" -h -H -m
_add_lang sw-TZ		"Swahili (Tanzania)" -lsw_TZ
_add_lang szl		"Silesian"
_add_lang ta		"Tamil"
_add_lang te		"Telugu" -h -H
_add_lang tg		"Tajik"
_add_lang th		"Thai" -h -H
# _add_lang ti		"Tigrinya/Tigrigna"
_add_lang tn		"Tswana"
_add_lang tr		"Turkish" -h
_add_lang ts		"Tsonga"
_add_lang tt		"Tatar"
_add_lang ug		"Uyghur"
_add_lang uk		"Ukrainian" -h -H -m
# _add_lang ur		"Urdu"
_add_lang uz		"Uzbek"
_add_lang ve		"Venda"
_add_lang vec		"Venetian"
_add_lang vi		"Vietnamese" -h
_add_lang xh		"Xhosa"
_add_lang zh-CN		"Simplified Chinese (People's Republic of China)" -lzh_CN
_add_lang zh-TW		"Traditional Chinese (Taiwan)" -lzh_TW
_add_lang zu		"Zulu" -H

post_extract() {
	local pkg f dir fname
	local srcdistdir=${XBPS_SRCDISTDIR}/${pkgname}-${version} pkg
	mkdir -p ${wrksrc}/external/tarballs
	for pkg in ${skip_extraction} libreoffice-{dictionaries,help,translations}-${version}.tar.xz ; do
		if [ "${pkg%.dll}" != "${pkg}" ]; then
			# The dll needs to be copied instead of symlinked
			cp -v ${srcdistdir}/${pkg} ${wrksrc}/external/tarballs/
		else
			# ... the other files can be symlinked
			ln -svf ${srcdistdir}/${pkg} ${wrksrc}/external/tarballs/
		fi
	done
	for f in ${FILESDIR}/*.patch; do
		fname=${f##*/}
		dir=${fname%%-*}
		if [ -d "${wrksrc}/external/${dir}" ]; then
			cp "$f" "${wrksrc}/external/${dir}/${fname}.1"
		fi
	done
}

post_patch() {
	vsed -i bin/distro-install-desktop-integration \
		-e "/gzip -f/d" \
		-e "s|.1.gz|.1|g"
	chmod +x bin/unpack-sources

	case "$XBPS_TARGET_MACHINE" in
		i686*)	# Don't run broken tests
			vsed -i "/CppunitTest_sw_ooxmlexport9/d" sw/Module_sw.mk
			vsed -i "/CPPUNIT_TEST(testTdf111798);/d" sd/qa/unit/export-tests-ooxml2.cxx
			;;
	esac

	if [ "$XBPS_BUILD_ENVIRONMENT" = void-packages-ci ]; then
		vsed -i "s/bootstrap: check-if-root compilerplugins/bootstrap: compilerplugins/" Makefile.in
	fi
}

do_configure() {
	local opts

	# the build system tries to call curl or wget, we can catch any invocations here and add them to distfiles
	ln -sf /bin/false ${XBPS_WRAPPERDIR}/curl
	ln -sf /bin/false ${XBPS_WRAPPERDIR}/wget
	ln -sf /bin/false ${XBPS_WRAPPERDIR}/git

	export QT6DIR=/usr/lib/qt6

	# Separate autogen.sh and configure because we need
	# to patch configure for musl support
	NOCONFIGURE=1 ./autogen.sh

	# standard options
	opts="--prefix=/usr"
	opts+=" --sysconfdir=/etc"
	opts+=" --sbindir=/usr/bin"
	opts+=" --bindir=/usr/bin"
	opts+=" --mandir=/usr/share/man"
	opts+=" --infodir=/usr/share/info"
	opts+=" --localstatedir=/var"
	# libreoffice options
	opts+=" --with-vendor='VoidLinux'"
	opts+=" --with-help"
	opts+=" --disable-odk"
	opts+=" --disable-online-update"
	opts+=" --enable-python=system"
	opts+=" --enable-gtk3"
	opts+=" --enable-introspection"
	opts+=" --enable-gtk4"
	opts+=" --enable-qt5"
	opts+=" --enable-qt6"
	opts+=" --enable-kf5"
	opts+=" --enable-gtk3-kde5"
	opts+=" --disable-dependency-tracking"
	opts+=" --enable-release-build"
	opts+=" --enable-split-app-modules"
	opts+=" --disable-dconf"
	opts+=" --disable-epm"
	# New strategy: enable all libs and headers, then disable what's missing
	opts+=" --with-system-libs"
	opts+=" --with-system-headers"
	case "$XBPS_TARGET_MACHINE" in
	x86_64)	# opts+=" --enable-ext-mariadb-connector"
		# opts+=" --enable-ext-ct2n"
		opts+=" --enable-ext-numbertext"
		opts+=" --enable-ext-nlpsolver"
		# opts+=" --enable-ext-languagetool"
		;;
	esac
	opts+=" --without-fonts"
	opts+=" --enable-build-opensymbol"
	# opts+=" --disable-fetch-external"

	# skia sucks
	if [ "$XBPS_TARGET_ENDIAN" = "be" ]; then
		opts+=" --disable-skia"
	fi

	if [ "$build_option_java" ]; then
		opts+=" --with-java"
		opts+=" --with-jdk-home=/usr/lib/jvm/openjdk17"
		# also disables hamcrest
		opts+=" --without-junit"
	else
		opts+=" --without-java"
	fi
	opts+=" --with-tls=nss"
	opts+=" --without-system-abseil"
	opts+=" --without-system-box2d"
	# https://github.com/tdf/libcmis/pull/43 changes ABI
	opts+=" --without-system-libcmis"
	opts+=" --without-system-libstaroffice"
	opts+=" --without-system-libtommath"
	opts+=" --without-system-jfreereport"
	opts+=" --without-system-beanshell"
	# opts+=" --without-system-cuckoo"
	# opts+=" --without-system-glm"
	opts+=" --without-system-dragonbox"
	opts+=" --without-system-libfixmath"
	opts+=" --without-myspell-dicts"
	# opts+=" --with-system-icu-for-build=yes"
	# use system utilities
	opts+=" --enable-build-opensymbol"
	# finish configuring build
	opts+=" --with-external-dict-dir=${XBPS_CROSS_BASE}/usr/share/hunspell"
	opts+=" --with-external-hyph-dir=${XBPS_CROSS_BASE}/usr/share/hyphen"
	opts+=" --with-external-thes-dir=${XBPS_CROSS_BASE}/usr/share/mythes"
	opts+=" --with-parallelism=${XBPS_MAKEJOBS}"

	./configure ${opts} --with-lang="${_languages}"
}

do_build() {
	make ${makejobs} ${make_build_target}
	make ${makejobs} -C libreofficekit
}

do_check() {
	: "TODO test can not find hsqldb.jar" \
	LO_JAVA_DIR="${wrksrc}/instdir/program/classes" \
	LD_LIBRARY_PATH=/usr/lib/jvm/openjdk17/lib/server \
	make ${makejobs} check
}

do_install() {
	make DESTDIR="${DESTDIR}/all" PREFIXDIR=/usr distro-pack-install

	# Remove useless directory usr/lib32
	rm -rf ${DESTDIR}/usr/lib32
}

libreoffice-common_package() {
	short_desc+=" - Common files"
	depends="libreoffice-i18n-en-US>=${version}_${revision}"
	skiprdeps=/usr/lib/libreoffice/program/libofficebean.so
	replaces="libreoffice-epub>=0"
	pkg_install() {
		_split common
	}
}

libreoffice-base_package() {
	short_desc+=" - Database frontend"
	depends="libreoffice-writer>=${version}_${revision}"
	pkg_install() {
		_split base
	}
}

libreoffice-calc_package() {
	short_desc+=" - Spreadsheet"
	depends="libreoffice-writer>=${version}_${revision}"
	pkg_install() {
		_split calc
	}
}

libreoffice-draw_package() {
	short_desc+=" - Drawing application"
	depends="sane libreoffice-writer>=${version}_${revision}"
	pkg_install() {
		_split draw
	}
}

libreoffice-fonts_package() {
	short_desc+=" - Fonts"
	# libreoffice-fonts used to bundle those fonts
	depends="
	 amiri-font
	 culmus
	 dejavu-fonts-ttf
	 font-adobe-source-code-pro
	 font-adobe-source-sans-pro-v2
	 font-adobe-source-serif-pro
	 font-alef
	 font-crosextra-caladea-ttf
	 font-crosextra-carlito-ttf
	 font-emoji-one-color
	 font-kacst
	 font-liberation-narrow-ttf
	 liberation-fonts-ttf
	 font-libertine-graphite-ttf
	 font-reem-kufi-ttf
	 font-sil-gentium-basic
	 font-sil-scheherazade
	 noto-fonts-ttf
	 noto-fonts-ttf-extra
	"
	build_style=meta
	pkg_install() {
		vmkdir usr/lib/libreoffice/share
		mv ${DESTDIR}/all/usr/lib/libreoffice/share/fonts \
			${PKGDESTDIR}/usr/lib/libreoffice/share/
	}
}

libreoffice-gnome_package() {
	short_desc+=" - GNOME integration"
	pkg_install() {
		_split gnome
	}
}

libreoffice-impress_package() {
	short_desc+=" - Presentation application"
	depends="libreoffice-writer>=${version}_${revision}"
	pkg_install() {
		_split impress
	}
}

libreoffice-kde_package() {
	short_desc+=" - KDE integration"
	pkg_install() {
		cat > ${wrksrc}/file-lists/kde5_list.txt <<-EOF
		%dir /usr/lib/libreoffice/program
		%dir /usr/share/metainfo
		/usr/lib/libreoffice/program/libkf5be1lo.so
		/usr/lib/libreoffice/program/libvclplug_gtk3_kde5lo.so
		/usr/lib/libreoffice/program/libvclplug_kf5lo.so
		/usr/lib/libreoffice/program/libvclplug_qt5lo.so
		/usr/lib/libreoffice/program/lo_kde5filepicker
		/usr/share/metainfo/org.libreoffice.kde.metainfo.xml
		EOF
		_split kde5
	}
}

libreoffice-qt6_package() {
	short_desc+=" - Qt6 integration"
	pkg_install() {
		cat > ${wrksrc}/file-lists/qt6_list.txt <<-EOF
		%dir /usr/lib/libreoffice/program
		/usr/lib/libreoffice/program/libvclplug_qt6lo.so
		EOF
		_split qt6
	}
}

libreoffice-kit_package() {
	short_desc+=" - development files"
	pkg_install() {
		vmkdir usr/include
		vcopy ${wrksrc}/include/LibreOfficeKit usr/include
	}
}

libreoffice-math_package() {
	short_desc+=" - Equation editor"
	depends="libreoffice-writer>=${version}_${revision}"
	pkg_install() {
		_split math
	}
}

libreoffice-postgresql_package() {
	short_desc+=" - Connector for PostgreSQL"
	depends="libreoffice-base>=${version}_${revision}"
	pkg_install() {
		_split postgresql
	}
}

libreoffice-writer_package() {
	short_desc+=" - Word processor"
	pkg_install() {
		_split writer
	}
}

# Use a name which makes this catch-all subpackage the last one
libreoffice-xtensions_package() {
	short_desc+=" - Extensions"
	depends="libreoffice-common>=${version}_${revision}"
	pkg_install() {
		# Remove empty files
		find ${DESTDIR}/all -size 0 -delete
		# Remove useless files
		for f in segments.gen segments_3 _0.cfs help.tree help.key_ help.db_; do
			find ${DESTDIR}/all -name "${f}" -delete
		done
		# Move the remaining files to this subpackage
		vmkdir usr
		mv -v ${DESTDIR}/all/usr/* ${PKGDESTDIR}/usr/
		rm -rvf ${DESTDIR}/all
	}
}
