#!/bin/bash
# packager           : Frederic Boulet <bipbip> bipbiponlinux~AT~gmail.com
# homepage           : http://www.bombono.org
# source location    : 
# last revised       : 31.12.2010
# dep                : mjpegtools,libdvdread,dvdauthor,ffmpeg

softname='bombono-dvd'
softversion='1.0.0'
packageversion='70.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://freefr.dl.sourceforge.net/project/bombono/bombono-dvd/0.8/$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/share/applications
mkdir -p $dest/usr/share/icons/hicolor/48x48/apps
mkdir -p $dest/usr/doc
mkdir -p $dest/usr/src/$softname-$softversion

# Slack-desc
cat <<EOF > $dest/install/slack-desc 
           |-----handy-ruler------------------------------------------------------|
bombono-dvd: bombono-dvd (A DVD authoring program for Linux)
bombono-dvd:
bombono-dvd: The main features of Bombono DVD are:
bombono-dvd: Excellent MPEG viewer: Timeline and Monitor 
bombono-dvd: Real WYSIWYG Menu Editor with live thumbnails
bombono-dvd: Comfortable Drag-n-Drop support
bombono-dvd: You can author to folder, make ISO-image or burn directly to DVD
bombono-dvd: Reauthoring: you can import video from DVD discs
bombono-dvd:
bombono-dvd: 
bombono-dvd:
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"

sed -i -e "s|'-O2'|'-O2', '-march=i486', '-mtune=i686'|g" SConstruct

scons PREFIX=/usr DESTDIR=$dest
scons install 

# make icon
gm convert -size 48x48 $dest/usr/share/pixmaps/$softname.png -resize 48x48 $dest/usr/share/pixmaps/$softname.png
cd $dest/usr/share/icons/hicolor/48x48/apps/
ln -s ../../../../pixmaps/$softname.png $softname.png

# add 'default' files
	cd $src
    mkdir -p $dest/usr/doc/$softname-$softversion
    cp -a \
      COPYING 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
cp twolame_pass.diff $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

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

# .dep
cd $buildir
echo -e "atk,cairo,cairomm,cxxlibs|gcc-g++,dvdauthor,dvd+rw-tools,expat,ffmpeg,fontconfig,\
freetype,gcc,glib2,glibmm,gtk+2,gtkmm,libX11,libXau,libXcomposite,\
libXcursor,libXdamage,libXdmcp,libXext,libXfixes,libXi,libXinerama,\
libXrandr,libXrender,libdvdread,libpng,libsigc++,libxcb,libxml++,\
libxml2,mjpegtools,pango,pangomm,pixman,twolame,zlib" > $package.dep

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

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

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