#!/bin/bash
# packager           : Frederic Boulet <bipbip> bipbiponlinux~AT~gmail.com
# source location    : http://live.gnome.org/GooCanvas
# last revised       : 03.06.2010

softname='goocanvas'
softversion='0.15'
packageversion='66.1'

arch='i486'
cpu='i686'

prefix=/usr

# log
(

# parameters needed for the build process
buildir=$(pwd)
src="$buildir/$softname-$softversion"
srcpkg="$buildir/$softname-$softversion.tar.bz2"
package="$softname-$softversion-$arch-$packageversion"
dest="$buildir/$package"
source=http://ftp.acc.umu.se/pub/GNOME/sources/goocanvas/0.15/$softversion/$softname-$softversion.tar.bz2

#  prepare the build result directory
rm -rf $dest
mkdir -p $dest
mkdir -p $dest/install
mkdir -p $dest/usr/doc
mkdir -p $dest/usr/src/$softname-$softversion

# Slack-desc
cat <<EOF > $dest/install/slack-desc
         |-----handy-ruler------------------------------------------------------|
goocanvas: goocanvas (Cairo Canvas Widget for GTK+)
goocanvas: 
goocanvas: GooCanvas is a new canvas widget for GTK+ that uses the cairo 2D 
goocanvas: library for drawing. It has a model/view split, and uses interfaces 
goocanvas: for canvas items and views, so you can easily turn any application 
goocanvas: object into canvas items.
goocanvas: 
goocanvas: 
goocanvas: 
goocanvas: 
goocanvas: 
EOF

# check if source is present
if [ -f $softname-$softversion.tar.bz2 ]; 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 xvf $srcpkg
cd $src	

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

./configure \
	--program-prefix="" \
	--program-suffix="" \
	--prefix="/usr" \
	--sysconfdir="/etc" \
	--localstatedir="/var" \
	--mandir="/usr/man" \
	--infodir="/usr/info" \
	--enable-static="no" \
	--enable-shared="yes" \
	--build=$arch-slackware-linux || exit

make 
make install DESTDIR=$dest

# add 'default' files
	cd $src
    mkdir -p $dest/usr/doc/$softname-$softversion
    cp -a \
      AUTHORS COPYING TODO ChangeLog INSTALL NEWS README \
      $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
( cd $dest/$prefix/man
  find . -name "*.?" | xargs gzip -9
)

# ZenBuild 
cd $buildir
cp 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 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 {} \;

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

# md5sum signature
cd $buildir
md5sum $package.txz > $package.md5

# .dep
cd $buildir
echo -e "atk,cairo,cxxlibs|gcc-g++,expat,fontconfig,freetype,gcc,glib2,gtk+2,\
libX11,libXau,libXcomposite,libXcursor,libXdamage,libXdmcp,libXext,libXfixes,\
libXi,libXinerama,libXrandr,libXrender,libpng,libxcb,pango,pixman,zlib" > $package.dep

# .src
echo http://zenwalk.pinguix.com/user-accounts/bip/EXTRA/l/goocanvas/source/build-goocanvas.sh > $package.src
echo $source >> $package.src

# erase source
rm -rf $src
rm -rf $dest

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