#!/bin/sh

# Build script for LibreOffice
# Author: Ejis <ejis@mailoo.org>

# Homepage: https://www.libreoffice.org/


# Stop when an error occurs.

set -e


# These variables set the name of the package, its version,
# which Zenwalk version it's built for and the number of this
# build for this version.

SOFTNAME=libreoffice-core
SERIES=3.5
VERSION=$SERIES.3
DIRVERSION=${VERSION}rc2
NUMBER=2
ZENVER=72
BUILD=1

# Zenwalk category of the package (e.g.: extra/xap)

CATEGORY=xap

# The architecture this package is built for. It will be mostly
# i486, but when the package won't run right on i486, this value
# can be i686.
# This determines the flags to use during compile.

MARCH=i586
FLAGS="-O2 -march=$MARCH -mtune=i686"

# The DOTNEW variable tells the buildscript if it should convert
# every file in /etc to .new files, preventing previously
# installed files to be erased after an upgrade.
# The DOINST variable tells the buildscript to create a
# doinst.sh file, that contains post-install commands.

DOTNEW=1
DOINST=1

# Current directory.

CWD=$(pwd)

# Name of the package file before the extension.

PKG=$SOFTNAME-$VERSION-$MARCH-$ZENVER.$BUILD

# Source directory and the directory where the package will be
# installed before compression.

SOFTDIR=$CWD/LibO_${DIRVERSION}_Linux_x86_install-rpm_en-US
PKGDIR=$CWD/$PKG

# Files created by the script.
# The .txz file is the package file, the .md5 is its md5sum, the
# .src file is the file that contains the URLs where to download
# the files used for building and the .dep file is the list of
# the dependencies.

PKGFILE=$PKGDIR.txz
MD5=$PKGDIR.md5
SRC=$PKGDIR.src
DEP=$PKGDIR.dep

# Files needed to make the package. Most of the time, it will
# only be the source tarball, but sometimes, some extra files
# are needed, like patches or icon images.
# Some files are kept in /usr/src. The buildscript is
# mandatory but some of the source files can also be added if
# they are small files.

SRCFILES=(
	LibO_${VERSION}_Linux_x86_install-rpm_en-US.tar.gz
	intro-zen.png
)
KEPTSRC=(
	$CWD/build-$SOFTNAME.sh
	$CWD/${SRCFILES[1]}
)

# The host url is the url where you will be able to find the
# package files.
# The source URLs are the URLs where the source files can be
# found. Sometimes, it can be the host URL.

HOSTURL=http://zenwalk.pinguix.com/user-accounts/ejis/$CATEGORY/$SOFTNAME/$ZENVER
SRCURLS=(
	http://download.documentfoundation.org/libreoffice/stable/$VERSION/rpm/x86
	$HOSTURL
)

# When the source files and URLs are set, the complete links
# to them must be set.

SRCLINKS=(
	${SRCURLS[0]}/${SRCFILES[0]}
	${SRCURLS[1]}/${SRCFILES[1]}
)

# These files will be put in /usr/doc. These are mostly the text
# files you find in the source directory.

DOC=(
	$SOFTDIR/readmes/README_en-US
)

# Sometimes, some dependencies can't be found, so they must be
# named in order to be added to the .dep file.
# On the other hand, some dependencies can be wrong or
# unnecessary, so they should be removed from the .dep file.

EXTRADEPS=(
	liboasis
	atk
	avahi
	cxxlibs
	dbus
	dbus-glib
	e2fsprogs
	expat
	fontconfig
	freetype
	gcc
	glib2
	gnome-vfs
	gst-plugins-base
	gstreamer
	gtk+2
	icu
	libICE
	libSM
	libX11
	libXau
	libXaw
	libXcomposite
	libXcursor
	libXdamage
	libXdmcp
	libXext
	libXfixes
	libXft
	libXi
	libXinerama
	libXmu
	libXpm
	libXrandr
	libXrender
	libXt
	libXtst
	libXxf86vm
	libdrm
	libpng
	libxcb
	libxml2
	mesa
	net-tools
	openssl
	pango
	perl
	pixman
	python
	xcb-util
	zlib
)

LESSDEPS=(
)


# Check for source files and download them if necessary

if [ ! -f ${SRCFILES[0]} ]; then
	wget -c -t 0 -T 50 -w 0 ${SRCLINKS[0]}
fi

if [ ! -f ${SRCFILES[1]} ]; then
	wget -c -t 0 -T 50 -w 0 ${SRCLINKS[1]}
fi


# Erase the previous build files

rm -rf $PKGFILE $SOFTDIR $PKGDIR $MD5 $SRC $DEP
mkdir -p $PKG


# Build

echo ""
echo "-----Building-----"
echo ""

tar -xvf $CWD/${SRCFILES[0]}

cd $SOFTDIR

export CFLAGS=$FLAGS
export CXXFLAGS=$FLAGS

#rpm2txz $SOFTDIR/RPMS/libobasis${SERIES}-base-$VERSION-$NUMBER.i586.rpm
#rpm2txz $SOFTDIR/RPMS/libobasis${SERIES}-binfilter-$VERSION-$NUMBER.i586.rpm
#rpm2txz $SOFTDIR/RPMS/libobasis${SERIES}-calc-$VERSION-$NUMBER.i586.rpm
#rpm2txz $SOFTDIR/RPMS/libobasis${SERIES}-core01-$VERSION-$NUMBER.i586.rpm
#rpm2txz $SOFTDIR/RPMS/libobasis${SERIES}-core02-$VERSION-$NUMBER.i586.rpm
#rpm2txz $SOFTDIR/RPMS/libobasis${SERIES}-core03-$VERSION-$NUMBER.i586.rpm
#rpm2txz $SOFTDIR/RPMS/libobasis${SERIES}-core04-$VERSION-$NUMBER.i586.rpm
#rpm2txz $SOFTDIR/RPMS/libobasis${SERIES}-core05-$VERSION-$NUMBER.i586.rpm
#rpm2txz $SOFTDIR/RPMS/libobasis${SERIES}-core06-$VERSION-$NUMBER.i586.rpm
#rpm2txz $SOFTDIR/RPMS/libobasis${SERIES}-core07-$VERSION-$NUMBER.i586.rpm
#rpm2txz $SOFTDIR/RPMS/libobasis${SERIES}-draw-$VERSION-$NUMBER.i586.rpm
rpm2txz $SOFTDIR/RPMS/libobasis${SERIES}-en-US-$VERSION-$NUMBER.i586.rpm
#rpm2txz $SOFTDIR/RPMS/libobasis${SERIES}-en-US-base-$VERSION-$NUMBER.i586.rpm
#rpm2txz $SOFTDIR/RPMS/libobasis${SERIES}-en-US-calc-$VERSION-$NUMBER.i586.rpm
#rpm2txz $SOFTDIR/RPMS/libobasis${SERIES}-en-US-math-$VERSION-$NUMBER.i586.rpm
rpm2txz $SOFTDIR/RPMS/libobasis${SERIES}-en-US-res-$VERSION-$NUMBER.i586.rpm
#rpm2txz $SOFTDIR/RPMS/libobasis${SERIES}-en-US-writer-$VERSION-$NUMBER.i586.rpm
#rpm2txz $SOFTDIR/RPMS/libobasis${SERIES}-extension-beanshell-script-provider-$VERSION-$NUMBER.i586.rpm
#rpm2txz $SOFTDIR/RPMS/libobasis${SERIES}-extension-javascript-script-provider-$VERSION-$NUMBER.i586.rpm
#rpm2txz $SOFTDIR/RPMS/libobasis${SERIES}-extension-mediawiki-publisher-$VERSION-$NUMBER.i586.rpm
#rpm2txz $SOFTDIR/RPMS/libobasis${SERIES}-extension-nlpsolver-$VERSION-$NUMBER.i586.rpm
#rpm2txz $SOFTDIR/RPMS/libobasis${SERIES}-extension-pdf-import-$VERSION-$NUMBER.i586.rpm
#rpm2txz $SOFTDIR/RPMS/libobasis${SERIES}-extension-presentation-minimizer-$VERSION-$NUMBER.i586.rpm
#rpm2txz $SOFTDIR/RPMS/libobasis${SERIES}-extension-presenter-screen-$VERSION-$NUMBER.i586.rpm
#rpm2txz $SOFTDIR/RPMS/libobasis${SERIES}-extension-python-script-provider-$VERSION-$NUMBER.i586.rpm
#rpm2txz $SOFTDIR/RPMS/libobasis${SERIES}-extension-report-builder-$VERSION-$NUMBER.i586.rpm
rpm2txz $SOFTDIR/RPMS/libobasis${SERIES}-gnome-integration-$VERSION-$NUMBER.i586.rpm
#rpm2txz $SOFTDIR/RPMS/libobasis${SERIES}-graphicfilter-$VERSION-$NUMBER.i586.rpm
#rpm2txz $SOFTDIR/RPMS/libobasis${SERIES}-images-$VERSION-$NUMBER.i586.rpm
#rpm2txz $SOFTDIR/RPMS/libobasis${SERIES}-impress-$VERSION-$NUMBER.i586.rpm
#rpm2txz $SOFTDIR/RPMS/libobasis${SERIES}-javafilter-$VERSION-$NUMBER.i586.rpm
#rpm2txz $SOFTDIR/RPMS/libobasis${SERIES}-kde-integration-$VERSION-$NUMBER.i586.rpm
#rpm2txz $SOFTDIR/RPMS/libobasis${SERIES}-math-$VERSION-$NUMBER.i586.rpm
#rpm2txz $SOFTDIR/RPMS/libobasis${SERIES}-ogltrans-$VERSION-$NUMBER.i586.rpm
#rpm2txz $SOFTDIR/RPMS/libobasis${SERIES}-onlineupdate-$VERSION-$NUMBER.i586.rpm
#rpm2txz $SOFTDIR/RPMS/libobasis${SERIES}-ooofonts-$VERSION-$NUMBER.i586.rpm
#rpm2txz $SOFTDIR/RPMS/libobasis${SERIES}-ooolinguistic-$VERSION-$NUMBER.i586.rpm
#rpm2txz $SOFTDIR/RPMS/libobasis${SERIES}-postgresql-sdbc-$VERSION-$NUMBER.i586.rpm
#rpm2txz $SOFTDIR/RPMS/libobasis${SERIES}-pyuno-$VERSION-$NUMBER.i586.rpm
#rpm2txz $SOFTDIR/RPMS/libobasis${SERIES}-writer-$VERSION-$NUMBER.i586.rpm
#rpm2txz $SOFTDIR/RPMS/libobasis${SERIES}-xsltfilter-$VERSION-$NUMBER.i586.rpm
rpm2txz $SOFTDIR/RPMS/libreoffice${SERIES}-$VERSION-$NUMBER.i586.rpm
#rpm2txz $SOFTDIR/RPMS/libreoffice${SERIES}-base-$VERSION-$NUMBER.i586.rpm
#rpm2txz $SOFTDIR/RPMS/libreoffice${SERIES}-calc-$VERSION-$NUMBER.i586.rpm
rpm2txz $SOFTDIR/RPMS/libreoffice${SERIES}-dict-en-$VERSION-$NUMBER.i586.rpm
rpm2txz $SOFTDIR/RPMS/libreoffice${SERIES}-dict-es-$VERSION-$NUMBER.i586.rpm
rpm2txz $SOFTDIR/RPMS/libreoffice${SERIES}-dict-fr-$VERSION-$NUMBER.i586.rpm
#rpm2txz $SOFTDIR/RPMS/libreoffice${SERIES}-draw-$VERSION-$NUMBER.i586.rpm
rpm2txz $SOFTDIR/RPMS/libreoffice${SERIES}-en-US-$VERSION-$NUMBER.i586.rpm
#rpm2txz $SOFTDIR/RPMS/libreoffice${SERIES}-impress-$VERSION-$NUMBER.i586.rpm
#rpm2txz $SOFTDIR/RPMS/libreoffice${SERIES}-math-$VERSION-$NUMBER.i586.rpm
rpm2txz $SOFTDIR/RPMS/libreoffice${SERIES}-stdlibs-$VERSION-$NUMBER.i586.rpm
rpm2txz $SOFTDIR/RPMS/libreoffice${SERIES}-ure-$VERSION-$NUMBER.i586.rpm
#rpm2txz $SOFTDIR/RPMS/libreoffice${SERIES}-writer-$VERSION-$NUMBER.i586.rpm
rpm2txz $SOFTDIR/RPMS/desktop-integration/libreoffice${SERIES}-freedesktop-menus-$VERSION-$NUMBER.noarch.rpm


fakeroot /sbin/installpkg --root $PKGDIR *.txz
rm -rvf $PKGDIR/var


mkdir -pv $PKGDIR/usr/share/applications
mv -v $PKGDIR/opt/libreoffice${SERIES} $PKGDIR/usr/share/
rm -rvf $PKGDIR/opt

cd $PKGDIR/usr/bin

rm -vf libreoffice${SERIES}
ln -vs ../share/libreoffice${SERIES}/program/soffice \
	libreoffice${SERIES}
sed -i "s:/opt:/usr/share:g" ./libreoffice${SERIES}-printeradmin 

cd $PKGDIR/usr/share/libreoffice${SERIES}/program/
cp -v $CWD/${SRCFILES[1]} ./intro.png

cd $PKGDIR/usr/share/applications
rm -vf *

desktops=(
#	base
#	calc
#	draw
#	impress
	javafilter
#	math
#	printeradmin
#	startcenter
#	writer
)

for desktop in ${desktops[@]} ; do
	ln -vs ../libreoffice${SERIES}/share/xdg/$desktop.desktop \
	libreoffice${SERIES}-$desktop.desktop
done


# Packaging

echo ""
echo "-----Packaging-----"
echo ""

cd $PKGDIR

mkdir -p $PKGDIR/install
mkdir -p $PKGDIR/usr/src/$SOFTNAME-$VERSION
mkdir -p $PKGDIR/usr/doc/$SOFTNAME-$VERSION


	       #|-----handy-ruler------------------------------------------------------|
cat << EOF > $PKGDIR/install/slack-desc
$SOFTNAME: $SOFTNAME
$SOFTNAME:
$SOFTNAME: LibreOffice is the power-packed free, libre and open source personal
$SOFTNAME: productivity suite for Windows, Macintosh and GNU/Linux, that gives you
$SOFTNAME: six feature-rich applications for all your document production and data
$SOFTNAME: processing needs: Writer, Calc, Impress, Draw, Math and Base.
$SOFTNAME: 
$SOFTNAME: 
$SOFTNAME: 
$SOFTNAME: 
$SOFTNAME: 
EOF


# Redundent

if [ -d $PKGDIR/usr/share/doc ]; then
	mv $PKGDIR/usr/share/doc/$SOFTNAME/* $PKGDIR/usr/doc/$SOFTNAME-$VERSION/
	rm -rvf $PKGDIR/usr/share/doc
fi

if [ -d $PKGDIR/etc ]; then
	if [ $DOTNEW == 1 ]; then
		dotnewfiles=$(find $PKGDIR/etc -type f -print -exec \
			mv {} {}.new \;)
		cat << EOF > $PKGDIR/install/doinst.sh
dotnewfiles=(${dotnewfiles[@]})

for file in \${dotnewfiles[@]}; do
	if [ ! -f \$file ]; then
		mv \$file.new \$file
	fi
done
EOF
		sed -i "s:$PKGDIR/::g" $PKGDIR/install/doinst.sh
	fi
fi

if [ $DOINST == 1 ]; then
	cat << EOF >> $PKGDIR/install/doinst.sh
doinst() {
# update the mime desktop database
if [ -x usr/bin/update-desktop-database ]; then
	usr/bin/update-desktop-database -q
fi
# update the mime database
if [ -x usr/bin/update-mime-database ]; then
	usr/bin/update-mime-database usr/share/mime > /dev/null 2>&1
fi
}
EOF
fi

find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | \
	xargs strip --strip-unneeded 2> /dev/null || true
find . | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | \
	xargs strip --strip-unneeded 2> /dev/null || true

if [ -d $PKGDIR/usr/man ]; then
	
	( cd $PKGDIR/usr/man
		find . -type f -exec gzip -9 {} \;
		for i in $( find . -type l ) ; do ln -s $( readlink $i ).gz $i.gz ; rm $i ; done
	)
	
fi

if [ -d $PKGDIR/usr/info ]; then
	
	rm -f $PKGDIR/usr/info/dir
	gzip -9 $PKGDIR/usr/info/*.info*
	
fi

find . -name perllocal.pod \
	-o -name ".packlist" \
	-o -name "*.bs" \
	| xargs rm -f


cp -v ${KEPTSRC[@]} $PKGDIR/usr/src/$SOFTNAME-$VERSION/

cp -rv	${DOC[@]} $PKGDIR/usr/doc/$SOFTNAME-$VERSION/


# fix permissions
find . -perm 777 -exec chmod 755 {} \;
find . -perm 775 -exec chmod 755 {} \;
find . -perm 666 -exec chmod 644 {} \;
find . -perm 664 -exec chmod 644 {} \;
find . -perm 600 -exec chmod 644 {} \;
find . -perm 444 -exec chmod 644 {} \;
find . -perm 400 -exec chmod 644 {} \;
find . -perm 440 -exec chmod 644 {} \;
find . -perm 511 -exec chmod 755 {} \;
find . -perm 711 -exec chmod 755 {} \;
find . -perm 555 -exec chmod 755 {} \;


fakeroot /sbin/makepkg -l y -c n $PKGFILE


cd $CWD

echo 'Creating .md5 file'
md5sum $PKG.txz > $MD5

echo 'Creating .src file'
echo "$HOSTURL/build-$SOFTNAME.sh" > $SRC
for link in ${SRCLINKS[@]}; do
	echo $link >> $SRC
done

echo 'Checking for dependencies'
deps=$(depfinder $PKGFILE)

for dep in ${EXTRADEPS[@]}; do
	deps="$deps,$dep"
done

for dep in ${LESSDEPS[@]}; do
	deps=${deps/$dep/}
done

order=$(echo ${deps//,/ } | sort -d)
echo ${order// /,} > $DEP

echo "Removing build directories"
rm -rf $SOFTDIR $PKGDIR
