SUMMARY="A full office productivity suite"
DESCRIPTION="LibreOffice is a powerful office suite – its clean interface and \
feature-rich tools help you unleash your creativity and enhance your \
productivity.
It's compatible with a wide range of document formats such as MS Word (.doc, \
.docx), Excel (.xls, .xlsx), PowerPoint (.ppt, .pptx) and Publisher.

LibreOffice includes several applications that make it the most powerful Free \
and Open Source office suite on the market:

- Writer (word processing)
- Calc (spreadsheets)
- Impress (presentations)
- Draw (vector graphics and flowcharts)
- Base (databases)
- Math (formula editing)"
HOMEPAGE="https://www.libreoffice.org/"
COPYRIGHT="2000-2020 LibreOffice contributors"
LICENSE="MPL v2.0"
REVISION="8"

SOURCE_URI="https://github.com/LibreOffice/core/archive/libreoffice-$portVersion.tar.gz"
SOURCE_DIR="core-libreoffice-$portVersion"
CHECKSUM_SHA256="ade533e1a240f3263486d193aa24cf62a19f9a81457dc3bb680858bb10ae7648"

SOURCE_URI_2="https://github.com/LibreOffice/translations/archive/libreoffice-$portVersion.tar.gz"
CHECKSUM_SHA256_2="54c2d8b80e5f00577bc4299bd9f5e7c50fce8ab7eb8083599369993c721cd464"
SOURCE_DIR_2="translations-libreoffice-$portVersion"
SOURCE_FILENAME_2="translations-libreoffice-$portVersion.tar.gz"

srcGitRevision3="cd5a5ee2cc0e3b3a62a5827d22035752ec7813a3"
SOURCE_URI_3="https://github.com/threedeyes/LibreOfficeLauncher/archive/$srcGitRevision3.tar.gz"
CHECKSUM_SHA256_3="5314962ca770369398599e175dc2c35c685d3fc968c9894390490c737036e528"
SOURCE_DIR_3="LibreOfficeLauncher-$srcGitRevision3"
SOURCE_FILENAME_3="LibreOfficeLauncher-$srcGitRevision3.tar.gz"

PATCHES="
	libreoffice-${portVersion}.patchset
	libreoffice-${portVersion}_orcus-0.16.patchset
	"
ADDITIONAL_FILES="
	libreoffice.iom
	libreoffice.rdef.in
	create_buildid.sh
	"

POST_INSTALL_SCRIPTS="$relativePostInstallDir/create_buildid.sh"
# Build for secondary arch only
ARCHITECTURES="!all"
SECONDARY_ARCHITECTURES="x86"

boostMinimumVersion=1.69.0
libreofficeLanguages="af ar be bg bn br brx bs ca ca-valencia cs cy da de dsb el en-GB en-ZA eo es et eu fa fi fr fy ga gd gl he hi hr hu id is it ja ka kk kmr-Latn ko lb lt lv mk mn nb ne nl nn pl pt pt-BR ro ru sa-IN sd sk sl sq sr sr-Latn sv szl tg th tr tt ug uk uz vi zh-CN zh-TW"

PROVIDES="
	libreoffice$secondaryArchSuffix = $portVersion
	app:LibreOffice$secondaryArchSuffix = $portVersion
	"
REQUIRES="
	haiku$secondaryArchSuffix
	lib:libabw_0.1$secondaryArchSuffix
	lib:libboost_date_time$secondaryArchSuffix
	lib:libboost_filesystem$secondaryArchSuffix
	lib:libboost_iostreams$secondaryArchSuffix
	lib:libboost_locale$secondaryArchSuffix
	lib:libcairo$secondaryArchSuffix
	lib:libcdr_0.1$secondaryArchSuffix
	lib:libclucene_core$secondaryArchSuffix
	lib:libcmis_0.5$secondaryArchSuffix
	lib:libcrypto$secondaryArchSuffix
	lib:libcurl$secondaryArchSuffix
	lib:libe_book_0.1$secondaryArchSuffix
	lib:libepoxy$secondaryArchSuffix
	lib:libepubgen_0.1$secondaryArchSuffix
	lib:libetonyek_0.1$secondaryArchSuffix
	lib:libexpat$secondaryArchSuffix
	lib:libexslt$secondaryArchSuffix
	lib:libexttextcat_2.0$secondaryArchSuffix
	lib:libfontconfig$secondaryArchSuffix
	lib:libfreehand_0.1$secondaryArchSuffix
	lib:libfreetype$secondaryArchSuffix
	lib:libglib_2.0$secondaryArchSuffix
	lib:libgraphite2$secondaryArchSuffix
	lib:libharfbuzz_icu$secondaryArchSuffix
	lib:libharfbuzz$secondaryArchSuffix
	lib:libhunspell_1.7$secondaryArchSuffix
	lib:libhyphen$secondaryArchSuffix
	lib:libicui18n$secondaryArchSuffix >= 66
	lib:libicuuc$secondaryArchSuffix >= 66
	lib:libintl$secondaryArchSuffix
	lib:libjpeg$secondaryArchSuffix
	lib:libKF5ConfigCore$secondaryArchSuffix
	lib:libKF5CoreAddons$secondaryArchSuffix
	lib:libKF5I18n$secondaryArchSuffix
	lib:libKF5KIOCore$secondaryArchSuffix
	lib:libKF5KIOFileWidgets$secondaryArchSuffix
	lib:libKF5KIOWidgets$secondaryArchSuffix
	lib:libKF5WindowSystem$secondaryArchSuffix
	lib:liblangtag$secondaryArchSuffix
	lib:liblber_2.4$secondaryArchSuffix
	lib:liblcms2$secondaryArchSuffix
	lib:libldap_2.4$secondaryArchSuffix
	lib:liblpsolve55$secondaryArchSuffix
	lib:libmspub_0.1$secondaryArchSuffix
	lib:libmwaw_0.3$secondaryArchSuffix
	lib:libmythes_1.2$secondaryArchSuffix
	lib:libmysqlclient$secondaryArchSuffix
	lib:libneon$secondaryArchSuffix
	lib:libnspr4$secondaryArchSuffix
	lib:libnss3$secondaryArchSuffix
	lib:libnssutil3$secondaryArchSuffix
	lib:libnumbertext_1.0$secondaryArchSuffix
	lib:libodfgen_0.1$secondaryArchSuffix
	lib:liborcus_0.16$secondaryArchSuffix
	lib:libpagemaker_0.0$secondaryArchSuffix
	lib:libplc4$secondaryArchSuffix
	lib:libplds4$secondaryArchSuffix
	lib:libpng16$secondaryArchSuffix
	lib:libpoppler$secondaryArchSuffix
	lib:libQt5Core$secondaryArchSuffix
	lib:libQt5Gui$secondaryArchSuffix
	lib:libQt5Network$secondaryArchSuffix
	lib:libQt5Widgets$secondaryArchSuffix
	lib:libqxp_0.0$secondaryArchSuffix
	lib:libraptor2$secondaryArchSuffix
	lib:librdf$secondaryArchSuffix
	lib:librevenge_0.0$secondaryArchSuffix
	lib:librevenge_stream_0.0$secondaryArchSuffix
	lib:libsmime3$secondaryArchSuffix
	lib:libssl3$secondaryArchSuffix
	lib:libstaroffice_0.0$secondaryArchSuffix
	lib:libuuid$secondaryArchSuffix
	lib:libvisio_0.1$secondaryArchSuffix
	lib:libwpd_0.10$secondaryArchSuffix
	lib:libwpg_0.3$secondaryArchSuffix
	lib:libwps_0.4$secondaryArchSuffix
	lib:libxmlsec1_nss$secondaryArchSuffix
	lib:libxml2$secondaryArchSuffix
	lib:libxslt$secondaryArchSuffix
	lib:libz$secondaryArchSuffix
	lib:libzmf_0.0$secondaryArchSuffix
	"

ARCHITECTURES_galleries="any"
PROVIDES_galleries="
	libreoffice${secondaryArchSuffix}_galleries = $portVersion
	"
REQUIRES_galleries="
	libreoffice$secondaryArchSuffix == $portVersion base
	"

for lang in $libreofficeLanguages; do
typeset -l langcode
langcode=${lang/-/_}
eval "PROVIDES_${langcode}=\"\
	libreoffice${secondaryArchSuffix}_${langcode} = $portVersion\
	\"; \
REQUIRES_${langcode}=\"\
	libreoffice$secondaryArchSuffix == $portVersion base\
	\"; \
DESCRIPTION_${langcode}=\"Translations for the Libreoffice suite ($lang)\"; \
ARCHITECTURES_${langcode}=\"any\""
done

BUILD_REQUIRES="
	haiku${secondaryArchSuffix}_devel
	unixodbc${secondaryArchSuffix}_devel
	devel:glm
	devel:libabw_0.1$secondaryArchSuffix
	devel:libboost_date_time$secondaryArchSuffix >= $boostMinimumVersion
	devel:libboost_filesystem$secondaryArchSuffix >= $boostMinimumVersion
	devel:libboost_iostreams$secondaryArchSuffix >= $boostMinimumVersion
	devel:libboost_locale$secondaryArchSuffix >= $boostMinimumVersion
	devel:libcairo$secondaryArchSuffix
	devel:libcdr_0.1$secondaryArchSuffix
	devel:libclucene_contribs_lib$secondaryArchSuffix
	devel:libclucene_core$secondaryArchSuffix
	devel:libcmis_0.5$secondaryArchSuffix
	devel:libcppunit$secondaryArchSuffix
	devel:libcurl$secondaryArchSuffix
	devel:libe_book_0.1$secondaryArchSuffix
	devel:libepoxy$secondaryArchSuffix
	devel:libepubgen_0.1$secondaryArchSuffix
	devel:libetonyek_0.1$secondaryArchSuffix
	devel:libexpat$secondaryArchSuffix
	devel:libexttextcat_2.0$secondaryArchSuffix
	devel:libfontconfig$secondaryArchSuffix
	devel:libfreehand_0.1$secondaryArchSuffix
#	devel:libgcrypt$secondaryArchSuffix
	devel:libgraphite2$secondaryArchSuffix
	devel:libharfbuzz_icu$secondaryArchSuffix
	devel:libhunspell_1.7$secondaryArchSuffix
	devel:libhyphen$secondaryArchSuffix
	devel:libicuuc$secondaryArchSuffix >= 66
	devel:libjpeg$secondaryArchSuffix
	devel:libKF5ConfigCore$secondaryArchSuffix
	devel:libKF5CoreAddons$secondaryArchSuffix
	devel:libKF5I18n$secondaryArchSuffix
	devel:libKF5KIOCore$secondaryArchSuffix
	devel:libKF5KIOFileWidgets$secondaryArchSuffix
	devel:libKF5KIOWidgets$secondaryArchSuffix
	devel:libKF5WindowSystem$secondaryArchSuffix
	devel:liblangtag$secondaryArchSuffix
	devel:liblcms2$secondaryArchSuffix
	devel:libldap$secondaryArchSuffix
	devel:liblpsolve55$secondaryArchSuffix
	devel:libmspub_0.1$secondaryArchSuffix
	devel:libmwaw_0.3$secondaryArchSuffix
	devel:libmysqlclient$secondaryArchSuffix
	devel:libmythes_1.2$secondaryArchSuffix
	devel:libneon$secondaryArchSuffix
	devel:libnss3$secondaryArchSuffix
	devel:libnumbertext_1.0$secondaryArchSuffix
	devel:libodfgen_0.1$secondaryArchSuffix
	devel:liborcus_0.16$secondaryArchSuffix
	devel:libpagemaker_0.0$secondaryArchSuffix
	devel:libpoppler$secondaryArchSuffix
	devel:libqrcodegen$secondaryArchSuffix
	devel:libqxp_0.0$secondaryArchSuffix
	devel:libraptor2$secondaryArchSuffix
	devel:librdf$secondaryArchSuffix
	devel:librevenge_0.0$secondaryArchSuffix
	devel:libsane$secondaryArchSuffix
	devel:libstaroffice_0.0$secondaryArchSuffix
	devel:libvisio_0.1$secondaryArchSuffix
	devel:libwpd_0.10$secondaryArchSuffix
	devel:libwpg_0.3$secondaryArchSuffix
	devel:libwps_0.4$secondaryArchSuffix
	devel:libuuid$secondaryArchSuffix
	devel:libxmlsec1_nss$secondaryArchSuffix
	devel:libxslt$secondaryArchSuffix
	devel:libz$secondaryArchSuffix
	devel:libzmf_0.0$secondaryArchSuffix
	devel:mdds >= 1.5
	"
BUILD_PREREQUIRES="
	cmd:aclocal
	cmd:autoconf
	cmd:bison
	cmd:find
	cmd:flex
	cmd:fontforge
	cmd:gcc$secondaryArchSuffix
	cmd:genbrk
	cmd:genccode
	cmd:gencmn
	cmd:git
	cmd:gperf
	cmd:grep
	cmd:kf5_config$secondaryArchSuffix
	cmd:ld$secondaryArchSuffix
	cmd:libtool$secondaryArchSuffix
	cmd:make
	cmd:patch
	cmd:perl
	cmd:pkg_config$secondaryArchSuffix
	cmd:python2.7
	cmd:python3.7
	cmd:ucpp
	cmd:which
	cmd:xz
	cmd:zip
	"

PATCH()
{
       # disable tooltips as they crash libreoffice currently
       cd officecfg/registry/schema/org/openoffice/Office
       sed -i '/Activates the Tip/{n;n;n;s/true/false/}' Common.xcs
}

BUILD()
{
	# rename $sourceDir to something shorter, otherwise build fails with
	# execv: Argument too big /sources/core/sw/Library_sw.mk:20
	cd .. ; mv core-libreoffice-$portVersion core
	ln -s core core-libreoffice-$portVersion; cd core

	rm -rf translations
	ln -s $sourceDir2 translations

	pkgconfig_libdir="`finddir B_SYSTEM_DIRECTORY`/$relativeDevelopLibDir/pkgconfig"
	pkgconfig_datadir="`finddir B_SYSTEM_DIRECTORY`/$relativeDataDir/pkgconfig"
	export PKG_CONFIG_LIBDIR="$pkgconfig_libdir:$pkgconfig_datadir"

	./autogen.sh \
		--with-distro=LibreOfficeHaiku \
		--enable-kf5 \
		--enable-qt5 \
		--enable-release-build \
		--enable-readonly-installset \
		--enable-python=no \
		--enable-build-opensymbol \
		--enable-sal-log \
		\
		--disable-ccache \
		--disable-firebird-sdbc \
		--disable-postgresql-sdbc \
		--disable-fetch-external \
		--disable-dependency-tracking \
		\
		--with-fonts=no \
		--with-vendor="HaikuPorts" \
		--with-system-libs \
		--with-boost-libdir=`finddir B_SYSTEM_DIRECTORY`/$relativeDevelopLibDir \
		--with-system-ucpp \
		\
		--without-doxygen \
		--without-helppack-integration \
		--without-java \
		--without-system-jars \
		--with-lang="$libreofficeLanguages"

	make $jobArgs build-nocheck

	pushd $sourceDir3/src
	make
	popd
}

INSTALL()
{
	appDir=$appsDir/LibreOffice
	mkdir -p $appDir
	cp -r ../core/instdir/* $appDir
	mv $appDir/program/soffice.bin $appDir/program/LibreOffice
	mv $sourceDir3/src/LibreOffice\ {Calc,Database,Draw,Impress,Math,Writer} $appDir

	# make sure we do not copy default profile
	rm -rf $appDir/user $appDir/share/xdg

	mkdir -p "$dataDir/deskbar/menu/Applications/LibreOffice"

	packageEntries galleries \
		$appDir/share/gallery $appDir/presets/gallery

	declare -a files
	declare -a filesToPackage
	typeset -l langcode

	for lang in $libreofficeLanguages; do
		unset files
		unset filesToPackage
		langcode=${lang/-/_}

		files=("${files[@]}" "$appDir/program/resource/${lang/-/_}")
		files=("${files[@]}" "$appDir/share/autotext/$lang")
		files=("${files[@]}" "$appDir/share/autocorr/$lang")
		files=("${files[@]}" "$appDir/share/registry/res/registry_$lang.xcd")
		files=("${files[@]}" "$appDir/share/registry/res/fcfg_langpack_$lang.xcd")
		files=("${files[@]}" "$appDir/share/registry/Langpack_$lang.xcd")
		files=("${files[@]}" "$appDir/readmes/README_$lang")

		for f in ${files[@]}; do
			if [ -f $f ] || [ -d $f ]; then
				filesToPackage=("${filesToPackage[@]}" "$f")
			fi
		done

		packageEntries ${langcode} ${filesToPackage[@]}
	done

	mkdir -p $postInstallDir
	cp $portDir/additional-files/create_buildid.sh $postInstallDir

	local APP_SIGNATURE="application/x-vnd.LibreOffice"
	local MAJOR="`echo "$portVersion" | cut -d. -f1`"
	local MIDDLE="`echo "$portVersion" | cut -d. -f2`"
	local MINOR="`echo "$portVersion" | cut -d. -f3`"
	local LONG_INFO="$SUMMARY"
	sed \
		-e "s|@APP_SIGNATURE@|$APP_SIGNATURE|" \
		-e "s|@MAJOR@|$MAJOR|" \
		-e "s|@MIDDLE@|$MIDDLE|" \
		-e "s|@MINOR@|$MINOR|" \
		-e "s|@LONG_INFO@|$LONG_INFO|" \
		$portDir/additional-files/libreoffice.rdef.in > \
			$sourceDir/../core/libreoffice.rdef

	addResourcesToBinaries $sourceDir/../core/libreoffice.rdef \
		$appDir/program/LibreOffice
	mimeset -f $appDir/program/LibreOffice

	addAppDeskbarSymlink $appDir/program/LibreOffice "LibreOffice/LibreOffice"
	addAppDeskbarSymlink "$appDir/LibreOffice Calc" "LibreOffice/LibreOffice Calc"
	addAppDeskbarSymlink "$appDir/LibreOffice Database" "LibreOffice/LibreOffice Database"
	addAppDeskbarSymlink "$appDir/LibreOffice Draw" "LibreOffice/LibreOffice Draw"
	addAppDeskbarSymlink "$appDir/LibreOffice Impress" "LibreOffice/LibreOffice Impress"
	addAppDeskbarSymlink "$appDir/LibreOffice Math" "LibreOffice/LibreOffice Math"
	addAppDeskbarSymlink "$appDir/LibreOffice Writer" "LibreOffice/LibreOffice Writer"
}
