#### this file is sourced not run
PKGVER=6.2.8.1
PKGBUILD=2
PKGARCH=x86_64

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

DEPS_TARBALL=libreoffice-$PKGVER-external-deps.tar.xz
DEPS_MD5SUM=0c702135550b607b77d1610208afc542
DEPS_URL=http://distro.ibiblio.org/fatdog/source/800/$DEPS_TARBALL

# james note: these two tarballs, in the future should be part of DEPS_TARBALL
FIREBIRD_TARBALL=Firebird-3.0.0.32483-0.tar.bz2
FIREBIRD_MD5SUM=821260b61dafc22899d1464d4e91ee6a
FIREBIRD_URLL=https://dev-www.libreoffice.org/src/$FIREBIRD_TARBALL
LTM_TARBALL=ltm-1.0.zip
LTM_MD5SUM=da283d2e3e72137d0c600ac36b991c9d
LTM_URLL=https://dev-www.libreoffice.org/src/$LTM_TARBALL

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

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

TRAN_TARBALL=libreoffice-translations-$PKGVER.tar.xz
TRAN_MD5SUM=342fcc65fdc3c1eeb399abfa1060f164
TRAN_URL=https://downloadarchive.documentfoundation.org/libreoffice/old/$PKGVER/src/$TRAN_TARBALL

SLACKREQ='libglew libboost-common gtk2 openssl poppler libneon cairo
expat curl libboost-common libicu
gstreamer2 gst2-plugins-base gst2-plugins-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%: 
"

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=$TRAN_TARBALL MD5SUM=$TRAN_MD5SUM SRC_URL=$TRAN_URL &&
	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 &&

	# james note: in the future these are not necessary as they will be packed together with DEPS_TARBALL
	TARBALL=$FIREBIRD_TARBALL MD5SUM=$FIREBIRD_MD5SUM SRC_URL=$FIREBIRD_URL &&
	pkg_check $SRC_DIR $TARBALL $MD5SUM &&
	cp $SRC_DIR/$TARBALL tmp/libreoffice-$PKGVER-external-deps/tarballs &&
	TARBALL=$LTM_TARBALL MD5SUM=$LTM_MD5SUM SRC_URL=$LTM_URL &&
	pkg_check $SRC_DIR $TARBALL $MD5SUM &&
	cp $SRC_DIR/$TARBALL tmp/libreoffice-$PKGVER-external-deps/tarballs &&

	true
}

### 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 &&
	mv /tmp/pkg/$TRAN_TARBALL external/tarballs &&
	
	# make sure our icons are complete
	# according to 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

	# build with -Os
	export CFLAGS="-Os" CPPFLAGS="-Os" &&
	sed -i -e "s/-O2/-Os/" configure &&
	
	# 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 #No. using gstreamer.
	#sed -r -i 's/\|\|.*/\)/' avmedia/source/vlc/vlcuno.cxx &&
	
	
	# qt5 In case we want to use the qt5 plugin one day.
	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/
	
	# configure
	./configure --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-qt5  \
             --disable-dbus           \
             --disable-sdremote         \
             --disable-sdremote-bluetooth  \
             --disable-gio                 \
             --disable-scripting-beanshell \
             --disable-scripting-javascript \
             --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   \
             --without-galleries         \
             --with-theme="tango colibre breeze" \
             --without-fonts		\
             --with-system-cairo         \
             --with-system-apr 			\
             --with-system-curl          \
             --with-system-expat         \
             --with-system-jpeg          \
             --with-system-lcms2         \
             --with-system-libpng        \
             --with-system-libxml        \
             --with-system-neon          \
             --without-system-openssl       \
             --with-system-poppler       \
             --with-system-serf          \
             --with-system-zlib        \
             --with-system-nss        \
             --disable-vlc   			\
             --with-tls=openssl 		\
             --with-system-boost		\
             --enable-gstreamer-1-0   \
             --disable-gstreamer-0-10	\
             --disable-ccache         \
             --with-help              \
             --with-myspell-dicts     \
             --without-system-dicts   \
             --with-system-gpgmepp \
             --with-system-harfbuzz \
             --with-system-graphite \
             --with-system-epoxy \
             --with-system-icu \
             --enable-mergelibs &&
             ##--disable-firebird-sdbc    \
             ##--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"	\
             
	#Stupid root check
	sed -i -e 's/exit 1;//' Makefile
	
	# build
	make build-nocheck &&
	make distro-pack-install &&

	# clean ups
	#find /usr/share/fonts -name .uuid -delete &&	
	rm -rf /var/cache/fontconfig /var/db/fontconfig && # somehow the cache gets created	
	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 &&
	
	# if we want to use script (for workarounds) instead of symlink
    #rm -f /usr/bin/libreoffice &&
    #install -m755 /tmp/pkg/libreoffice /usr/bin/ &&

	# 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/
				mkdir -p /tmp/lang/${p##*/}/usr/$LIBDIR/libreoffice/share/config/
				cp /usr/lib64/libreoffice/share/config/images_helpimg.zip /tmp/lang/${p##*/}/usr/$LIBDIR/libreoffice/share/config/
			fi
		done
		rmdir /tmp/lang
		rm /usr/$LIBDIR/libreoffice/share/config/images_helpimg.zip
		
		# 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
		
		#Remove presentation templates bigger than 100k
		for p in /usr/$LIBDIR/libreoffice/share/template/common/presnt/*; do
			if [[ $(ls -s $p | cut -d " " -f 1) -gt 100 ]] ; then
				rm $p
			fi
		done
		
		# 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 ../..
}
