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

# source: kirk
TARBALL=libreoffice-$PKGVER.tar.xz
MD5SUM=95cb3b05efd537716fcbf0674cbebc99
SRC_URL=https://downloadarchive.documentfoundation.org/libreoffice/old/$PKGVER/src/$TARBALL
BUNDLE=

DEPS_TARBALL=libreoffice-$PKGVER-external-deps.tar.gz
DEPS_MD5SUM=7aaa9c2761bbb0e9113393d59e0da858
DEPS_URL=http://distro.ibiblio.org/fatdog/source/710/$DEPS_TARBALL

HELP_TARBALL=libreoffice-help-$PKGVER.tar.xz
HELP_MD5SUM=994c4f1aa14747109ef7e763597c757b
HELP_URL=https://downloadarchive.documentfoundation.org/libreoffice/old/$PKGVER/src/$HELP_TARBALL

DICT_TARBALL=libreoffice-dictionaries-$PKGVER.tar.xz
DICT_MD5SUM=aaa11f4cc98207e2f95abd648ec95812
DICT_URL=https://downloadarchive.documentfoundation.org/libreoffice/old/$PKGVER/src/$DICT_TARBALL

SLACKREQ='libglew libboost-common libnss libnspr vlc-qt gtk2 
openssl poppler libneon cairo expat curl libboost-common harfbuzz-icu libicu'
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 get-libre.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%: 
"

USE_PREBUILT=

### default pkg_download
### prepare
pkg_prepare() {
	if [ $USE_PREBUILT ]; then
		tar -xf /mnt/sda5/fatdog64/710/backup/$PKGNAME-with-dict-$PKGVER-$PKGARCH-$PKGBUILD.$PKG_TYPE
		return 0
	fi

	pkg_prepare_default &&
	
	TARBALL=$HELP_TARBALL SRC_URL=$HELP_URL MD5SUM=$HELP_MD5SUM &&
	pkg_check $SRC_DIR $TARBALL $MD5SUM &&
	cp $SRC_DIR/$TARBALL tmp/pkg &&
	
	TARBALL=$DICT_TARBALL SRC_URL=$DICT_URL MD5SUM=$DICT_MD5SUM &&
	pkg_check $SRC_DIR $TARBALL $MD5SUM &&
	cp $SRC_DIR/$TARBALL tmp/pkg &&	
	
	TARBALL=$DEPS_TARBALL MD5SUM=$DEPS_MD5SUM SRC_URL=$DEPS_URL &&
	
	pkg_prepare_default &&
	
	cd tmp/libreoffice* &&
	patch -Np1 -i $CHROOT_DIR/tmp/pkg/libreoffice5.harfbuzz-icu.patch
	
}

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

	# 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
	
	# 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 ...
	return 0 # always successful
}

### build
pkg_build() {
if [ "$USE_PREBUILT" ]; then
	echo adjust recipe now; read p;
else
	mv /tmp/libreoffice-$PKGVER-external-deps/tarballs/ /tmp/libreoffice-$PKGVER/external/ &&
	cd /tmp/libreoffice-$PKGVER/ &&
	mv /tmp/pkg/$HELP_TARBALL external/tarballs &&
	mv /tmp/pkg/$DICT_TARBALL external/tarballs &&
	
	# make sure our icons are complete
	# according to readme, the layer is tango <- industrial <- galaxy
	# this is a hack, alternatively we have to build at least tango, galaxy and industrial themes
	merge_icon_themes industrial to tango
	merge_icon_themes galaxy to tango

	# build with -Os
	export CFLAGS="-Os" CXXFLAGS="-Os" &&
	sed -i -e "s/-O2/-Os/" configure.ac &&
	
	# BLFS patches: 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 &&
	
	# enable VLC integration by default
	sed -r -i 's/\|\|.*/\)/' avmedia/source/vlc/vlcuno.cxx &&
	
	# configure
	./autogen.sh CXXFLAGS=-Os CFLAGS=-Os --prefix=/usr --libdir=/usr/$LIBDIR \
             --sysconfdir=/etc --localstatedir=/var  \
             --with-vendor=Fatdog64      \
             --with-lang='en-US en-GB pl fr de it es'      \
             --enable-gtk           \
             --disable-gtk3            \
             --disable-dbus           \
             --disable-sdremote         \
             --disable-sdremote-bluetooth  \
             --disable-gio                 \
             --disable-tdeab             \
             --disable-firebird-sdbc    \
             --disable-collada       \
             --disable-scripting-beanshell \
             --disable-scripting-javascript \
             --with-alloc=system         \
             --without-java              \
             --disable-dconf             \
             --disable-odk               \
             --enable-release-build=yes  \
             --enable-python=no     \
             --disable-report-builder   \
             --disable-lpsolve          \
             --disable-coinmp           \
             --disable-online-update     \
             --disable-lotuswordpro      \
             --disable-lpsolve           \
             --disable-postgresql-sdbc   \
             --disable-gltf   		\
             --without-galleries         \
             --with-theme="tango"	\
             --without-fonts		\
             --with-system-cairo         \
             --disable-cairo-canvas      \
             --with-system-apr 			\
             --with-system-curl          \
             --with-system-expat         \
             --with-system-jpeg          \
             --with-system-lcms2         \
             --with-system-libpng        \
             --with-system-libxml        \
             --with-system-neon          \
             --with-system-openssl       \
             --with-system-poppler       \
             --with-system-serf          \
             --with-system-zlib        \
             --with-system-nss        \
             --enable-vlc   			\
             --enable-mergelibs        \
             --with-system-boost		\
             --disable-gstreamer-1-0   \
             --disable-gstreamer-0-10	\
             --disable-ccache         \
             --with-help              \
             --with-myspell-dicts     \
             --without-system-dicts   \
             --disable-systray \
             --disable-cve-tests \
             --without-system-harfbuzz
	
	# build
	sed -i 's/check-if-root:/nope:/' Makefile  #Remove stupid root check.
	make build-nocheck &&
	make distro-pack-install &&

	# clean ups
	rm -r /usr/$LIBDIR/libreoffice/readmes &&
	rm -r /usr/share/icons &&
	cp -r /tmp/pkg/icons /usr/share/icons  &&
	cp -r /tmp/pkg/etc /
fi  &&

	# adjust icons categories as needed
	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}/" /usr/$LIBDIR/libreoffice/share/xdg/${app}.desktop
	done

	pkg_build_slackdesc &&	
	# Note: only symlink if we use system dicts - see comments above
	#ln -sf /usr/$LIBDIR/seamonkey/dictionaries /usr/share/myspell &&

	# run everything as a block to preserve exit code
	{	
		# package help files
		mkdir /tmp/lang
		for p in /usr/$LIBDIR/libreoffice/help/*; do
			if [ -d $p ]; then
				mkdir -p /tmp/lang/${p##*/}/usr/$LIBDIR/libreoffice/help/
				mv -v $p /tmp/lang/${p##*/}/usr/$LIBDIR/libreoffice/help/
			fi
		done
		rmdir /tmp/lang
		
		# package dict files
		mkdir /tmp/dict
		for p in /usr/$LIBDIR/libreoffice/share/extensions/dict-*; do
			if [ -d $p ]; then
				mkdir -p /tmp/dict/${p##*/}/usr/$LIBDIR/libreoffice/share/extensions/
				mv -v $p /tmp/dict/${p##*/}/usr/$LIBDIR/libreoffice/share/extensions/
			fi
		done
		rmdir /tmp/dict
		
		# always successful if we get here
		return 0 
	}
}

# ===== 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 ../..
}
