#!/bin/bash
# packager           : Frederic Boulet <bipbip> bipbiponlinux~AT~gmail.com
# homepage           : http://gstreamer.freedesktop.org/
# source location    : http://gstreamer.freedesktop.org/src/gst-plugins-base/gst-plugins-base-0.10.32.tar.bz2
# last revised       : 22.02.2011

softname='gst-plugins-base'
softversion='0.10.32'
packageversion='70.1'

arch='i486'
cpu='i686'

# log ;)
(

# parameters needed for the build process
buildir=$(pwd)
srcpkg="$buildir/$softname-$softversion.tar.bz2"
src="$buildir/$softname-$softversion"
package="$softname-$softversion-$arch-$packageversion"
dest="$buildir/$package"
source=http://gstreamer.freedesktop.org/src/gst-plugins-base/$softname-$softversion.tar.bz2

# prepare the build result directory
dest="$buildir/$package"
rm -rf $dest
mkdir -p $dest
mkdir -p $dest/install
mkdir -p $dest/usr/src/$softname-$softversion

# Slack-desc
cat <<EOF > $dest/install/slack-desc
                |-----handy-ruler------------------------------------------------------|
gst-plugins-base: gst-plugins-base (base set of GStreamer plugins)
gst-plugins-base:
gst-plugins-base: GStreamer Base Plug-ins is a well-groomed and well-maintained 
gst-plugins-base: collection of GStreamer plug-ins and elements, spanning the 
gst-plugins-base: range of possible types of elements one would want to write 
gst-plugins-base: for GStreamer.  It also contains helper libraries and base 
gst-plugins-base: classes useful for writing elements.  A wide range of video 
gst-plugins-base: and audio decoders, encoders, and filters are included.
gst-plugins-base:
gst-plugins-base:
gst-plugins-base: 
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 \
	--prefix=/usr \
    --libdir=/usr/lib \
    --sysconfdir=/etc \
    --localstatedir=/var \
    --mandir=/usr/man \
    --disable-debug \
    --disable-static \
    --build=$arch-slackware-linux  

make
make install DESTDIR=$dest

# add 'default' files
mkdir -p $dest/usr/doc/$softname-$softversion
cp -a \
   ABOUT-NLS AUTHORS COPYING* INSTALL NEWS README* RELEASE REQUIREMENTS\
   $dest/usr/doc/$softname-$softversion
        
# strip executables
( 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/usr/man
  find . -name "*.?" | xargs gzip -9
)

# ZenBuild
cp $buildir/build-$softname.sh $dest/usr/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

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

# .dep
cd $buildir
echo -e "GConf,ORBit2,alsa-lib,avahi,cairo,cdparanoia,cxxlibs|gcc-g++,dbus,\
dbus-glib,expat,fontconfig,freetype,gcc,glib2,gnome-vfs,gstreamer,libICE,\
libSM,libX11,libXau,libXdmcp,libXext,libXrender,libXv,libogg,libpng,\
libtheora,libvisual,libvorbis,libxcb,libxml2,openssl,pango,pixman,\
udev,util-linux-ng,zlib" > $package.dep

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

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

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