#!/bin/bash
# packager          : Frederic Boulet <bipbip> bipbiponlinux~AT~gmail.com
# homepage			: http://zim-wiki.org/
# source location	: http://www.zim-wiki.org/downloads/Zim-0.28.tar.gz
# dependency        : perl-gtk2,
# last revised      : 27.09.2009

softname='Zim'
softversion='0.28'
packageversion='1fb'

arch='i486'
cpu='i686'

prefix=/usr

# log
(

# parameters needed for the build process
buildir=$(pwd)
srcpkg="$buildir/$softname-$softversion.tar.gz"
src="$buildir/$softname-$softversion"
package="$softname-$softversion-$arch-$packageversion"
dest="$buildir/$package"
source=http://www.zim-wiki.org/downloads/$softname-$softversion.tar.gz

# prepare the build result directory
rm -rf $dest
mkdir -p $dest
mkdir -p $dest/install
mkdir -p $dest/$prefix/bin
mkdir -p $dest/usr/share/icons/hicolor/48x48/apps
mkdir -p $dest/usr/doc/$softname-$softversion
mkdir -p $dest/usr/src/$softname-$softversion

# Slac-desc
cat <<EOF > $dest/install/slack-desc
   |-----handy-ruler------------------------------------------------------|
Zim: Zim (A desktop wiki and outliner)
Zim:
Zim: Zim is a WYSIWYG text editor written in Gtk2-Perl which aims to 
Zim: bring the concept of a wiki to your desktop. Every page is saved as 
Zim: a text file with wiki markup. Pages can contain links to other 
Zim: pages, and are saved automatically. Creating a new page is as 
Zim: easy as linking to a non-existing page. Pages are ordered in 
Zim: a hierarchical structure that gives it the look and feel of 
Zim: an outliner. This tool is intended to keep track of TODO lists or 
Zim: to serve as a personal scratch book.
Zim:
EOF

# check if source is present
if [ -f $softname-$softversion.tar.gz ]; then
	echo "Source file already here, no need to download";
else
 	echo "Downloading source"
	wget -c --no-check-certificate $source
fi

# extract the source code
cd $buildir
tar xzvf $srcpkg
cd $src

# build
export CFLAGS="-O2 -march=$arch -mtune=$cpu"
export CXXFLAGS="-O2 -march=$arch -mtune=$cpu"

./configure
make 
make install DESTDIR=$dest

# Remove perllocal.pod and .packlist if present in the package
( for i in perllocal.pod .packlist; do
        find $dest -name "$i" -exec rm -rf {} \;
  done
) || exit 1

# make the .desktop file
convert $dest/usr/share/pixmaps/zim.png -resize 48x48 -depth 8 $dest/usr/share/icons/hicolor/48x48/apps/zim.png
   
# update desktop and mime database 
cat << "EOF" > $dest/install/doinst.sh
# 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
 
# add 'default' files
cp -a \
    Changes HACKING MANIFEST PACKAGING README TRANSLATORS \
   $dest/usr/doc/$softname-$softversion

# strip
( cd $dest
  find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
  find . | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
)

# compress man pages
( mv $dest/usr/share/man $dest/usr/man
  cd $dest/$prefix/man
  find . -name "*.?" | xargs gzip -9
)

# SalixBuild
cp $buildir/build-$softname.sh $dest/$prefix/src/$softname-$softversion

# set target permissions
chown -R root:root $dest

cd $dest
find . -perm 664 -exec chmod 644 {} \;
find . -perm 640 -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 777 -exec chmod 755 {} \;
find . -perm 775 -exec chmod 755 {} \;
find . -perm 511 -exec chmod 755 {} \;
find . -perm 711 -exec chmod 755 {} \;
find . -perm 555 -exec chmod 755 {} \;
find . -type d -exec chmod 755 {} \;

# real packaging work
cd $dest
makepkg -l y -c n $dest.txz

# computes the md5sum signature for the package
cd $buildir
md5sum $package.txz > $package.md5

# .dep
echo -e "perl,perl-File-BaseDir,perl-File-DesktopEntry,perl-File-MimeInfo,perl-gtk2,perl-pango" > $package.dep

# .src
rm -rf $src
rm -rf $dest

# erase
echo http://zenwalk.pinguix.com/user-accounts/bip/divers/salix/xap/zim/build-Zim.sh > $package.src
echo $source >>$package.src

# log
) 2>&1 | tee "$softname-$softversion-build.log"
