#### this file is sourced not run
PKGVER=7.5.2.2
PKGBUILD=1
PKGARCH=x86_64

# source: kirk updated to post BLFS 11.3 2023-04-29
TARBALL=libreoffice-$PKGVER.tar.xz
MD5SUM=636a4fa94c7361d9d0fd0bf840783c27
SRC_URL=https://download.documentfoundation.org/libreoffice/src/7.5.2/$TARBALL
BUNDLE=

DICT_TARBALL=libreoffice-dictionaries-$PKGVER.tar.xz
DICT_MD5SUM=3a07567b1f23b8a68f79dff659de81d6
DICT_URL=https://download.documentfoundation.org/libreoffice/src/7.5.2/$DICT_TARBALL

HELP_TARBALL=libreoffice-help-$PKGVER.tar.xz
HELP_MD5SUM=4ed012102d394870279f6532f86d2e50
HELP_URL=https://download.documentfoundation.org/libreoffice/src/7.5.2/$HELP_TARBALL

TRAN_TARBALL=libreoffice-translations-$PKGVER.tar.xz
TRAN_MD5SUM=d1cb0b5a68927eb066cc3e1b66de8a1a
TRAN_URL=https://download.documentfoundation.org/libreoffice/src/7.5.2/$TRAN_TARBALL

DEPS_TARBALL=libreoffice-$PKGVER-external-deps.tar # see download.lst from main tarball
DEPS_MD5SUM=3ba6b16a191efa3b598e1fed0d0fdd6e
DEPS_URL=http://distro.ibiblio.org/fatdog/source/900/$DEPS_TARBALL


SLACKREQ='libglew libboost-common gtk3 openssl poppler cairo
expat curl libboost-common libicu libnspr libnss libatomic_ops
gstreamer2 gst2-plugins-base gst2-libav gst2-plugins-good' # or vlc-qt, when built with vlc
SLACKDESC="libreoffice: libreoffice $PKGVER (Office suite)
libreoffice: 
libreoffice: LibreOffice is a full-featured office suite. It comes with word 
libreoffice: processor, spreadsheet, presentation, vector drawing and database 
libreoffice: management programs. It is largely compatible with Microsoft Office.
libreoffice: 
libreoffice: http://www.documentfoundation.org
libreoffice: 
libreoffice: Note: this build contains limited locales, no help files, no dicts.
libreoffice: They are available in the repo. If you need the full functionality 
libreoffice: please use getlibre.sh script to download official binary instead.
"

HELP_SLACKDESC="libreoffice-help-%lang%: libreoffice-help-%lang% $PKGVER (%lang% help pack for libreoffice)
libreoffice-help-%lang%: 
libreoffice-help-%lang%: This is the %lang% language help pack for libreoffice $PKGVER
libreoffice-help-%lang%: Installing this package enables you to view help contents offline,
libreoffice-help-%lang%: without any connection to the Internet.
libreoffice-help-%lang%: 
libreoffice-help-%lang%: http://www.documentfoundation.org
libreoffice-help-%lang%: 
libreoffice-help-%lang%: 
libreoffice-help-%lang%: 
libreoffice-help-%lang%: 
"

DICT_SLACKDESC="libreoffice-%lang%: libreoffice-%lang% $PKGVER (%lang% dictionary for libreoffice)
libreoffice-%lang%: 
libreoffice-%lang%: This is the %lang% dictionary for libreoffice $PKGVER
libreoffice-%lang%: Installing this package enables you to do spell check and thesaures
libreoffice-%lang%: for the given language.
libreoffice-%lang%: 
libreoffice-%lang%: http://www.documentfoundation.org
libreoffice-%lang%: 
libreoffice-%lang%: 
libreoffice-%lang%: 
libreoffice-%lang%: 
"

LO_PREFIX=/opt/libreoffice-$PKGVER # or /usr

### download
pkg_download() {
	pkg_check $SRC_DIR $DICT_TARBALL $DICT_MD5SUM $DICT_URL &&
	pkg_check $SRC_DIR $HELP_TARBALL $HELP_MD5SUM $HELP_URL &&
	pkg_check $SRC_DIR $TRAN_TARBALL $TRAN_MD5SUM $TRAN_URL &&
	pkg_check $SRC_DIR $DEPS_TARBALL $DEPS_MD5SUM $DEPS_URL &&
	pkg_check $SRC_DIR $TARBALL      $MD5SUM      $SRC_URL
}

### prepare
pkg_prepare() {
	local TARBALL_TARGET=./tmp/libreoffice-$PKGVER/external/tarballs

	pkg_download &&
	tar -xf $SRC_DIR/$TARBALL -C ./tmp --no-overwrite-dir &&
	
	mkdir -p $TARBALL_TARGET &&
	cp $SRC_DIR/$HELP_TARBALL $TARBALL_TARGET &&	
	cp $SRC_DIR/$DICT_TARBALL $TARBALL_TARGET &&
	cp $SRC_DIR/$TRAN_TARBALL $TARBALL_TARGET &&
	tar -xf $SRC_DIR/$DEPS_TARBALL --strip-components=2 -C $TARBALL_TARGET &&
	#mkdir -p ./etc && echo nameserver 8.8.8.8 > ./etc/resolv.conf # collect deps tarball &&

	true
}


### build
pkg_build() {	
	# qt5
	export PATH="$PATH:/opt/qt5/bin"
	export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/opt/qt5/$LIBDIR"
	export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:/opt/qt5/$LIBDIR/pkgconfig"
	export CPLUS_INCLUDE_PATH="$CPLUS_INCLUDE_PATH:/opt/qt5/include"
	export QT5_PATH="/opt/qt5/bin"
	export QT5_LD_LIBRARY_PATH="/opt/qt5/$LIBDIR"
	export QT5_PKG_CONFIG_PATH="/opt/qt5/$LIBDIR/pkgconfig"
	export QT5_CPLUS_INCLUDE_PATH="/opt/qt5/include" 
	export QT5DIR="/opt/qt5"
	
	# Let ICU know where it's data is
	export ICU_DATA=/usr/share/icu/current/

	# start the build
	cd /tmp/libreoffice-$PKGVER/ &&

	# make sure our icons are complete
	# according to icon-themes readme, the layer is tango <- colibre <- breeze
	# this is a hack, alternatively we have to build at least tango, colibre and breeze themes
	# merge_icon_themes colibre to tango
	# merge_icon_themes breeze to tango

	# BLFS patch: disable manpage compression & fix install
	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 && # only works if we use DESTDIR, which we don't

	# BLFS 11.3 fix compilation bug:
	# The instructions in the package unpack some tarballs into a location it cannot find later.
	# Create some symlinks to help out:
	ln -sv src/libreoffice-help-$PKGVER/helpcontent2/ &&
	ln -sv src/libreoffice-dictionaries-$PKGVER/dictionaries/ &&
	ln -sv src/libreoffice-translations-$PKGVER/translations/ &&

	# BLFS 11.3 If building on a 32 bit machine, CFLAGS is set to -Os, which breaks the build.
	# Prevent this:
	case $(uname -m) in
	   i?86) sed /-Os/d -i solenv/gbuild/platform/LINUX_INTEL_GCC.mk ;;
	esac &&
	unset CFLAGS CXXFLAGS CPPFLAGS && # our flags also have -Os in it too, so unset

	# BLFS 11.3 The latest version of GPGME breaks configure due to a library change.
	export ac_cv_lib_gpgmepp_progress_callback=yes &&

	# configure
	./configure --prefix=$LO_PREFIX --libdir=$LO_PREFIX/$LIBDIR \
			--sysconfdir=/etc --localstatedir=/var  \
			--with-parallelism=4        \
			--with-vendor=Fatdog64      \
			--with-lang='en-US en-GB pl fr de it es'  \
			--with-help                 \
			--with-myspell-dicts        \
			--without-junit             \
			--without-system-dicts      \
			--disable-dconf             \
			--disable-odk               \
			--enable-release-build=yes  \
			                            \
			--with-system-boost         \
			--with-system-curl          \
			--with-system-epoxy         \
			--with-system-expat         \
			--with-system-glm           \
			--with-system-gpgmepp       \
			--with-system-graphite      \
			--with-system-harfbuzz      \
			--with-system-icu           \
			--with-system-jpeg          \
			--with-system-lcms2         \
			--with-system-libatomic_ops \
			--with-system-libpng        \
			--with-system-libxml        \
			--with-system-nss           \
			--with-system-openssl       \
			--with-system-libtiff       \
			--with-system-libwebp       \
			--with-system-zlib          \
			                            \
			--with-system-cairo         \
			--enable-gstreamer-1-0      \
			--enable-gtk3               \
			--enable-qt5                \
                                        \
			--without-krb5              \
			--without-gssapi            \
			--without-java              \
			--enable-python=no          \
			--disable-dbus              \
			--disable-sdremote          \
			--disable-sdremote-bluetooth   \
			--disable-gio                  \
			--disable-scripting-beanshell  \
			--disable-scripting-javascript \
			--disable-lpsolve           \
			--disable-report-builder    \
			                            \
			--without-fonts		        \
			--without-galleries         \
			--disable-coinmp            \
			--disable-online-update     \
			--disable-lotuswordpro      \
			                            \
			--enable-mergelibs          \
			--enable-lto                \
			--with-theme="elementary sukapura sukapura_dark colibre colibre_dark breeze breeze_dark" \
			&&
			## BLFS options we disable
			## --with-jdk-home=/opt/jdk # we don't have JDK installed
			## --enable-python=system   # we don't want to use system python
			## --with-system-clucene    # we don't have it
			## --with-system-odbc       # we don't have it
			## --with-system-postgresql # we don't have it
			## --with-system-redland    # we don't have it
			## --with-system-openldap   # we don't have it.
			## --with-system-poppler - FTBFS if we use this. System is version 23, theirs is version 22.
			##
			## Extra options that we do use
			## --enable-mergelibs # make one giant libmergedlo.so instead of many small ones
			## --enable-lto       # longer compile times, but make libraries smaller. By a lot, they say.
			##
			## Other options we could, but don't, use.
			## --disable-firebird-sdbc   # this probably disable embedded firebird too
			## --disable-postgresql-sdbc # without this, libreoffice will build postgresql to get the driver
			## --disable-mariadb-sdbc    # without this, libreoffice will build mariadb to get the driver
			## --disable-lpsolve         # make it smaller if we don't use the feature
			## --disable-report-builder  # make it smaller if we don't use the feature
			## --disable-skia            # if disabled then use cairo, no OpenGL required. Can be disabled on demand.
			## --disable-ccache          # if build crashes
			## --without-help                 
			## --without-helppack-integration  
			## --without-myspell-dicts        
			## --with-system-dicts      
			## --with-external-dict-dir=/usr/share/myspell 
			## --with-locales="en-US en-AU en-GB pl-pl fr-fr de-de it-it es-es"    
			## --with-theme="tango industrial galaxy" # themes change all the time
			## --without-system-openssl               # we use system so we benefit from openssl updates
			## --with-theme="tango colibre breeze"    # themes change all the time
			## --with-tls=openssl                     # prefer openssl over nss? but why?


	# Stupid root check
	sed -i -e '/exit 1;/d' Makefile &&
	
	# build
	make build gb_SUPPRESS_TESTS=1 gb_COLOR=1 GCC_COLORS=1 &&
	make distro-pack-install &&

	pkg_build_slackdesc
}

# ===== support functions ======

# Merge icons from one theme to another
# $1-source, $2-IGNORED, $3-target, PWD=current dir - LO source root
merge_icon_themes() {
	local src=$1 tgt=$3	
	echo merging icon-theme $src to $tgt	
	cd icon-themes/$src
	cp -nr * ../$tgt
	cd ../..
}


### package - additional packaging for help files
pkg_package() {
	local p	

	# Note: we don't chain all the operations with &&, because failure
	# here is okay. Libreoffice takes a lot of time to build, packaging
	# failure is okay because we can still adjust it manually later
	
	# BLFS 11.3 setup for non /usr installation, modified for our purpose
	if [ "$LO_PREFIX" != "/usr" ]; then
		# Set up a generic location independent of version number
		ln -sfv libreoffice-$PKGVER ./opt/libreoffice

		# instead of symlinkings, we just relocate bin/ and share/ to /usr
		cp -a ./$LO_PREFIX/bin ./$LO_PREFIX/share ./usr
		rm -r ./$LO_PREFIX/bin ./$LO_PREFIX/share
	fi

	################# Fatdog Packaging ####################

	# 0. clean ups
	#find ./usr/share/fonts -name .uuid -delete && # not needed anymore
	rm -rf ./var  # delete fontconfig cache if it gets created 
	rm -f  ./etc/resolv.conf

	# 1. remove readme and icons (icons always in /usr, see relocation above)
	rm -r ./$LO_PREFIX/$LIBDIR/libreoffice/readmes &&
	rm -rf ./usr/share/icons/hicolor/512x512 && # keep the icons except the largest one

	# 2. copy ROX mime handlers
	cp -r ./tmp/pkg/etc ./

	# 3. adjust categories in desktop files
	for app in base calc draw impress math startcenter writer; do
		case $app in
			base|calc) cat="Database;Spreadsheet;" ;;
			draw|math) cat="Graphics;" ;;
			impress)   cat="Presentation;" ;;
			startcenter|writer) cat="WordProcessor;" ;;
		esac
		sed -i -e "s/^Categories=.*$/Categories=${cat}/" ./$LO_PREFIX/$LIBDIR/libreoffice/share/xdg/${app}.desktop
	done

	# 4. Remove presentation templates bigger than 100k
	#for p in ./$LO_PREFIX/$LIBDIR/libreoffice/share/template/common/presnt/*; do
	#	if [[ $(stat -c %s $p) -gt 100000 ]] ; then
	#		rm $p
	#	fi
	#done &&
	# 4. delete large templates
	(
		cd ./$LO_PREFIX/$LIBDIR/libreoffice/share/template/common/presnt/
		rm -f Candy.otp Vintage.otp Blueprint_Plans.otp
	)

	# 5. optional stuff
	# Note: only symlink if we use system dicts - see configure flags comments above
	#ln -sf $LO_PREFIX/$LIBDIR/seamonkey/dictionaries ./usr/share/myspell &&
	
	# if we want to use script (for workarounds) instead of symlink
    #rm -f ./usr/bin/libreoffice &&
    #install -m755 ./tmp/pkg/libreoffice ./usr/bin/ &&
    #sed -i -e "s|@LO_PREFIX@|$LO_PREFIX|" ./usr/bin/libreoffice

	# 5. package help files
	mkdir ./tmp/lang
	for p in ./$LO_PREFIX/$LIBDIR/libreoffice/help/*; do
		if [ -d $p ]; then
			mkdir -p ./tmp/lang/${p##*/}/$LO_PREFIX/$LIBDIR/libreoffice/help/
			mv -v $p ./tmp/lang/${p##*/}/$LO_PREFIX/$LIBDIR/libreoffice/help/
			mkdir -p ./tmp/lang/${p##*/}/$LO_PREFIX/$LIBDIR/libreoffice/share/config/
			cp ./$LO_PREFIX/$LIBDIR/libreoffice/share/config/images_helpimg.zip ./tmp/lang/${p##*/}/$LO_PREFIX/$LIBDIR/libreoffice/share/config/
		fi
	done
	rmdir ./tmp/lang
	rm ./$LO_PREFIX/$LIBDIR/libreoffice/share/config/images_helpimg.zip

	# export languages
	if [ -d ./tmp/lang ]; then
		echo Processing help packs
		for p in ./tmp/lang/*; do
		( 
			cd $p
			mkdir ./install
			echo "$HELP_SLACKDESC" | sed "s|%lang%|${p##*/}|g" > ./install/slack-desc
			echo Packing $PKGNAME-help-${p##*/}-$PKGVER-$PKGARCH-$PKGBUILD.$PKG_TYPE 				
			makepkg -c n -l n $OUTPUT_DIR/$PKGNAME-help-${p##*/}-$PKGVER-$PKGARCH-$PKGBUILD.$PKG_TYPE > /dev/null 2>&1
		)
		done
	fi

	# 6. package dict files
	mkdir ./tmp/dict
	for p in ./$LO_PREFIX/$LIBDIR/libreoffice/share/extensions/dict-*; do
		if [ -d $p ]; then
			mkdir -p ./tmp/dict/${p##*/}/$LO_PREFIX/$LIBDIR/libreoffice/share/extensions/
			mv -v $p ./tmp/dict/${p##*/}/$LO_PREFIX/$LIBDIR/libreoffice/share/extensions/
		fi
	done
	rmdir ./tmp/dict

	# export dictionaries
	if [ -d ./tmp/dict ]; then
		echo Processing dictionaries
		for p in ./tmp/dict/*; do
		( 
			cd $p
			mkdir ./install
			echo "$DICT_SLACKDESC" | sed "s|%lang%|${p##*/}|g" > ./install/slack-desc
			echo Packing $PKGNAME-${p##*/}-$PKGVER-$PKGARCH-$PKGBUILD.$PKG_TYPE 				
			makepkg -c n -l n $OUTPUT_DIR/$PKGNAME-${p##*/}-$PKGVER-$PKGARCH-$PKGBUILD.$PKG_TYPE > /dev/null 2>&1
		)
		done
	fi	

	echo Packing main package now ...
}
