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

softname='gst-plugins-ugly'
softversion='0.10.17'
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-ugly/$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-ugly: gst-plugins-ugly - plug-ins which might have distribution problems 
gst-plugins-ugly: 
gst-plugins-ugly: GStreamer Ugly Plug-ins is a set of plug-ins that have good quality
gst-plugins-ugly: and correct functionality, but distributing them might pose problems.
gst-plugins-ugly: The license on either the plug-ins or the supporting libraries might
gst-plugins-ugly: not be how we'd like. The code might be widely known to present patent
gst-plugins-ugly: problems.
gst-plugins-ugly: 
gst-plugins-ugly: plugins:a52dec,cdio,mad,mpeg2dec 
gst-plugins-ugly:
gst-plugins-ugly: 
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"

#Fix b_bframe rename in the new x264
sed -i -e "s/b_bframe_pyramid/i_bframe_pyramid/g" ext/x264/gstx264enc.c  
  
./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
mkdir -p $dest/usr/doc/$softname-$softversion
cp -a \
   AUTHORS ChangeLog COPYING INSTALL NEWS README\
   $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 {} \;

# create the split package for libraries who need dependencies
cd $buildir
mkdir -p $softname-extra-$softversion-$arch-$packageversion/usr/lib/gstreamer-0.10
mkdir -p $softname-extra-$softversion-$arch-$packageversion/install
cat <<EOF > $softname-extra-$softversion-$arch-$packageversion/install/slack-desc
                      |-----handy-ruler------------------------------------------------------|
gst-plugins-ugly-extra: gst-plugins-ugly-extra - a set of ugly-quality plug-ins 
gst-plugins-ugly-extra: 
gst-plugins-ugly-extra: GStreamer Ugly Plug-ins is a set of plug-ins that have good quality
gst-plugins-ugly-extra: and correct functionality, but distributing them might pose problems.
gst-plugins-ugly-extra: The license on either the plug-ins or the supporting libraries might
gst-plugins-ugly-extra: not be how we'd like. The code might be widely known to present patent
gst-plugins-ugly-extra: problems.
gst-plugins-ugly-extra: 
gst-plugins-ugly-extra: plugins:dvdread,lame,sid,twolame,x264
gst-plugins-ugly-extra:
gst-plugins-ugly-extra: 
 
EOF
liste="dvdread lame sid twolame x264"
for name in $liste; do
	echo "move libgst${name} in the extra package"
	mv -v $dest/usr/lib/gstreamer-0.10/libgst${name}* $softname-extra-$softversion-$arch-$packageversion/usr/lib/gstreamer-0.10
done
echo "create the extra package"
cd $softname-extra-$softversion-$arch-$packageversion
makepkg -l y -c n $buildir/$softname-extra-$softversion-$arch-$packageversion.txz
cd ..
echo "generate md5sum for extra package"
md5sum $softname-extra-$softversion-$arch-$packageversion.txz > $package.md5
rm -rf $softname-extra-$softversion-$arch-$packageversion

# 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 "a52dec,glib2,gst-plugins-base,gstreamer,libX11,\
libXau,libXdmcp,libcdio,libid3tag,libmad,liboil,libxcb,libxml2,mpeg2dec,zlib" > $package.dep

echo -e "cxxlibs|gcc-g++,gcc,glib2,gst-plugins-base,gstreamer,lame,libX11,\
libXau,libXdmcp,libdvdread,libsidplay,libxcb,libxml2,twolame,x264,zlib" > $softname-extra-$softversion-$arch-$packageversion.dep

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

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

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