#!/bin/bash
# packager          : Frederic Boulet <bipbip> bipbiponlinux~AT~gmail.com
# source location   : http://v2v.cc/~j/ffmpeg2theora/
# source location	: http://v2v.cc/~j/ffmpeg2theora/ffmpeg2theora-0.27.tar.bz2
# dependency        : ffmpeg,lame,libtheora,libkate

softname='ffmpeg2theora'
softversion='0.27'
packageversion='66.1'

arch='i486'
cpu='i686'

prefix=/usr

# 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://v2v.cc/~j/ffmpeg2theora/downloads/$softname-$softversion.tar.bz2

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

# Slack-desc
cat <<EOF > $dest/install/slack-desc
             |-----handy-ruler------------------------------------------------------|
ffmpeg2theora: ffmpeg2theora - A simple converter to create Ogg Theora files
ffmpeg2theora:
ffmpeg2theora: you can convert any file that ffmpeg can decode to theora.
ffmpeg2theora: right now the settings are hardcoded into
ffmpeg2theora: the binary. the idea is to provide ffmpeg2theora as a binary
ffmpeg2theora: along sites like v2v.cc to enable as many people as possible
ffmpeg2theora: to encode video clips with the same settings.
ffmpeg2theora: 
ffmpeg2theora: 
ffmpeg2theora:
ffmpeg2theora: 
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 $source
fi

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

# build

    export CFLAGS="-O2 -march=$arch -mtune=$cpu"
	
	patch -p0 < ../ffmpeg2theora-0.23-zenflags.patch
	
    scons
    scons install prefix=$dest/usr

# add 'default' files
    mkdir -p $dest/usr/doc/$softname-$softversion
    cp -a \
      AUTHORS COPYING INSTALL README TODO ChangeLog subtitles.txt\
      $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
  find . | xargs file | grep "ar archive" | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
)

# Compress Man pages
gzip -9 $dest/$prefix/man/*/*

# ZenBuild
cd $buildir
cp build-$softname.sh $dest/$prefix/src/$softname-$softversion
cp ffmpeg2theora-0.23-zenflags.patch $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 $buildir/$package.txz

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

# .dep
cd $buildir
echo -e "alsa-lib,bzip2,dirac,faac,ffmpeg,gcc,gcc-g++,jack,lame,\
libX11,libXau,libXdmcp,libXext,libkate,libogg,liboil,libtheora,\
libvorbis,libxcb,schroedinger,speex,x264,xvidcore,zlib" > $package.dep

# .src
echo http://zenwalk.pinguix.com/user-accounts/bip/EXTRA/xap/$softname/source/build-$softname.sh > $package.src
echo http://zenwalk.pinguix.com/user-accounts/bip/EXTRA/xap/$softname/source/ffmpeg2theora-0.23-zenflags.patch >> $package.src
echo $source >> $package.src

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

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