# Template file for 'libreoffice'
pkgname=libreoffice
version=5.1.0.3
revision=1
build_style=gnu-configure
configure_args="--disable-werror"
nopie=yes
nocross=yes
#
# XXX: libwps-0.4.x mit-krb5-devel nasm openjdk sane vlc
#      hamcrest hyphen iwyu libebook libfreehand libmwaw
#      lp_solve mdds ogl-math OpenCOLLADA orcus telepathy-glib-devel
# Missing, required libraries are downloaded and built internally
#
hostmakedepends="automake libtool pkg-config bison flex gperf icu
 perl-Archive-Zip unzip zip wget"
makedepends="apr-devel boost-devel cups-devel libcurl-devel
 dbus-glib-devel libjpeg-turbo-devel libpng-devel glu-devel graphite-devel
 gst-plugins-base1-devel gtk+-devel gtk+3-devel harfbuzz-devel icu-devel
 libatomic_ops-devel lcms2-devel librsvg-devel libxml2-devel libxslt-devel
 MesaLib-devel neon-devel nss-devel python3.4-devel libressl-devel libgcrypt-devel
 libgpg-error-devel poppler-devel postgresql-libs-devel redland-devel
 unixodbc-devel libXt-devel

 avahi-libs-devel hunspell-devel libabw-devel libcdr-devel libcmis-devel
 libcppunit-devel libexttextcat-devel liblangtag-devel libmspub-devel
 glew-devel glyphy-devel libodfgen-devel libpagemaker-devel librevenge-devel
 libvisio-devel libetonyek-devel libwpd-devel libwpg-devel mythes-devel
 vigra-devel"
short_desc="Productivity suite, formerly OpenOffice.org"
maintainer="Jürgen Buchmüller <pullmoll@t-online.de>"
license="LGPL-3"
homepage="http://www.libreoffice.org/"
_baseurl="http://download.documentfoundation.org/${pkgname}/src/${version%.*}"
distfiles="
 ${_baseurl}/${pkgname}-${version}.tar.xz
 ${_baseurl}/${pkgname}-dictionaries-${version}.tar.xz
 ${_baseurl}/${pkgname}-help-${version}.tar.xz
 ${_baseurl}/${pkgname}-translations-${version}.tar.xz"
checksum="
 b41b03f008dab3439bde31c1918f0477c035eab629166a79eeebb0bb6e8757b3
 718822a5ad5400e1026b18453bc38a118851df43e26255c3769078b444ab7a82
 c2e9dff6a930637ca9b203dc31b7f87b17a03419891201a386e81bfbbdc1b447
 fd0eeee993b8532288b77a43bfda9db6cdf781df3cb43f753ed01b620e760ee3"
skip_extraction="
 ${pkgname}-dictionaries-${version}.tar.xz
 ${pkgname}-help-${version}.tar.xz
 ${pkgname}-translations-${version}.tar.xz"
conflicts="libreoffice-bin>=0"

# Install location: either /usr or e.g. /opt/libreofficeX.Y.Z
_LO_PREFIX=/opt/${pkgname}${version%.*.*}

if [ -z "$CROSS_BUILD" ]; then
	# serf can't be cross compiled because of apr-util-devel
	makedepends+=" serf-devel"
	# mariadbclient can't currently be cross compiled
	makedepends+=" libmariadbclient-devel"
	configure_args+=" --with-system-mariadb"
fi

case "$XBPS_TARGET_MACHINE" in
	*-musl)	# use shipped clucene, because CLucene/analysis/cjk/CJKAnalyzer.h
		# is missing in the musl clucene-devel files
		;;
	*)	configure_args+=" --with-system-clucene"
		makedepends+=" clucene-devel"
		;;
esac

post_fetch() {
	local srcdistdir=${XBPS_SRCDISTDIR}/${pkgname}-${version}
	mkdir -p ${wrksrc}/external/tarballs
	ln -sf ${srcdistdir}/${pkgname}-dictionaries-${version}.tar.xz ${wrksrc}/external/tarballs/
	ln -sf ${srcdistdir}/${pkgname}-help-${version}.tar.xz ${wrksrc}/external/tarballs/
	ln -sf ${srcdistdir}/${pkgname}-translations-${version}.tar.xz ${wrksrc}/external/tarballs/
}

pre_configure() {
	sed -e "/gzip -f/d" \
		-e "s|.1.gz|.1|g" \
		-i bin/distro-install-desktop-integration
	sed -e "/distro-install-file-lists/d" -i Makefile.in
	chmod +x bin/unpack-sources
}

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

	# Add linux-musl* as host OS to lines containing linux-gnu*
	sed -i configure -e 's;linux-gnu\*;linux-gnu*|linux-musl*;g'

	# In theory we could use --with-lang=ALL but then
	# building the package then takes ages.

	./configure ${configure_args} \
		--prefix=${_LO_PREFIX} \
		--exec-prefix=${_LO_PREFIX} \
		--with-vendor='VoidLinux' \
		--with-lang='en-US de es fr it pl pt' \
		--with-help \
		--with-alloc=system \
		--without-java \
		--without-system-dicts \
		--disable-odk \
		--enable-python=system \
		--enable-gtk3 \
		--disable-dependency-tracking \
		--enable-release-build=yes \
		--disable-dconf \
		--with-tls=nss \
		--with-system-cairo \
		--with-myspell-dicts \
		--with-system-zlib \
		--with-system-jpeg \
		--with-system-expat \
		--with-system-icu \
		--with-system-icu-for-build=yes \
		--with-system-poppler \
		--with-system-curl \
		--with-system-boost \
		--with-system-glew \
		--with-system-glyphy \
		--with-system-nss \
		--with-system-graphite \
		--with-system-harfbuzz \
		--with-system-apr \
		--with-system-serf \
		--with-system-neon \
		--with-system-openssl \
		--with-system-redland \
		--with-system-libexttextcat \
		--with-system-libpng \
		--with-system-liblangtag \
		--with-system-lcms2 \
		--with-system-cppunit \
		--with-system-hunspell \
		--with-system-libabw \
		--with-system-libatomic_ops \
		--with-system-libcdr \
		--with-system-libcmis \
		--with-system-libetonyek \
		--with-system-libmspub \
		--with-system-libodfgen \
		--with-system-libpagemaker \
		--with-system-librevenge \
		--with-system-libvisio \
		--with-system-libwpd \
		--with-system-libwpg \
		--with-system-libxml \
		--with-system-mythes \
		--with-system-odbc \
		--with-system-openldap \
		--with-system-openssl \
		--with-system-postgresql \
		--with-system-vigra \
		--with-parallelism=${XBPS_MAKEJOBS}
}

do_build() {
	make build
}

do_install() {
	local f
	local INSTDIR=${DESTDIR}${_LO_PREFIX}

	make DESTDIR="${DESTDIR}" PREFIXDIR="${_LO_PREFIX}" distro-pack-install

	if [ "${_LO_PREFIX}" != "/usr" ]; then
		# HACK: for unknow reasons the i686 build ignores --prefix and installs in /usr
		if [ "$XBPS_TARGET_MACHINE" = "i686" ]; then
			# Copy from wrong install path
			mkdir -p ${INSTDIR}/bin
			if [ -d ${DESTDIR}/usr/bin ]; then
				cp -pR ${DESTDIR}/usr/bin/* ${INSTDIR}/bin/
				rm -rf ${DESTDIR}/usr/bin
			fi
			mkdir -p ${INSTDIR}/lib
			if [ -d ${DESTDIR}/usr/lib ]; then
				cp -pR ${DESTDIR}/usr/lib/* ${INSTDIR}/lib/
				rm -rf ${DESTDIR}/usr/lib
			fi
			if [ -d ${DESTDIR}/usr/lib32 ]; then
				cp -pR ${DESTDIR}/usr/lib32/* ${INSTDIR}/lib/
				rm -rf ${DESTDIR}/usr/lib32
			fi
			mkdir -p ${INSTDIR}/share
			if [ -d ${DESTDIR}/usr/lib32 ]; then
				cp -pR ${DESTDIR}/usr/share/* ${INSTDIR}/share/
				rm -rf ${DESTDIR}/usr/share
			fi
		fi

		# This symlink is necessary for the desktop menu entries
		vmkdir usr/bin
		ln -sf ${_LO_PREFIX}/lib/libreoffice/program/soffice \
			${DESTDIR}/usr/bin/libreoffice

		if [ -d ${INSTDIR}/share/icons/hicolor/32x32/apps ]; then
			# Icons
			vmkdir usr/share/pixmaps
			for f in $(find ${INSTDIR}/share/icons/hicolor/32x32/apps -type f); do
				f=${f#${DESTDIR}/}
 				ln -sf $f ${DESTDIR}/usr/share/pixmaps/
			done
		fi

		if [ -d ${INSTDIR}/lib/libreoffice/share/xdg ]; then
			# Desktop menu entries
			vmkdir usr/share/applications
			for f in $(find ${INSTDIR}/lib/libreoffice/share/xdg -type f); do
				f=${f#${DESTDIR}/}
				ln -sf $f ${DESTDIR}/usr/share/applications/libreoffice-$(basename $f)
			done
		fi

		if [ -d ${INSTDIR}/share/man/man1 ]; then
			# Manual pages
			vmkdir usr/share/man/man1
			for f in $(find ${INSTDIR}/share/man/man1 -type f); do
				f=${f#${DESTDIR}/}
 				ln -sf $f ${DESTDIR}/usr/share/man/man1/
			done
		fi
	fi

	mkdir -p ${INSTDIR}/share/appdata
	cp -p sysui/desktop/appstream-appdata/*.xml ${INSTDIR}/share/appdata/

	# Move bash completion to the right place
	vmkdir usr/share/bash-completion/completions
	mv ${DESTDIR}/etc/bash_completion.d/libreoffice.sh \
		${DESTDIR}/usr/share/bash-completion/completions/libreoffice

	# Remove unwanted libreoffice packaging files from the root directory
	rm -f ${DESTDIR}/gid_Module*
}
