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

softname='gstreamer'
softversion='0.10.36'
packageversion='72.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/gstreamer/$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------------------------------------------------------|
gstreamer: GStreamer (streaming multimedia framework)
gstreamer: 
gstreamer: GStreamer is a library that allows the construction of graphs of a
gstreamer: media-handling components, ranging from simple Ogg/Vorbis playback 
gstreamer: to complex audio (mixing) and video (non-linear editing) processing.
gstreamer: Applications can take advantage of advances in codec and filter 
gstreamer: technology transparently. Developers can add new codecs and filters
gstreamer: by writing a simple plugin with a clean, generic interface.
gstreamer: 
gstreamer: GStreamer is released under the LGPL.
gstreamer: 
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"
  
#patch -p1 < ../Makefile.patch 
  
./configure \
	--prefix=/usr \
        --libdir=/usr/lib \
        --sysconfdir=/etc \
        --localstatedir=/var \
        --mandir=/usr/man \
        --docdir=/usr/doc/$softname-$softversion \
        --disable-static \
        --disable-debug \
        --disable-gst-debug \
        --with-package-name='gstreamer' \
        --with-package-origin='http://gstreamer.freedesktop.org' \
        --build=$arch-slackware-linux

make
make install DESTDIR=$dest

# remove unversioned gst-* binaries 
cd $dest/usr/bin
for bins in `ls *-0.10`; do
    rm -f ${bins/-0.10/}
done
cd -

# add 'default' files
mkdir -p $dest/usr/doc/$softname-$softversion
cp -a \
   AUTHORS ChangeLog COPYING INSTALL RELEASE ABOUT-NLS NEWS README TODO\
   $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 "glib2,libxml2,zlib" > $package.dep

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

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

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