#!/bin/bash
# author           : Fred. Galusik <fredg> (fred@zenwalk.fr)
# homepage         : http://supertux.lethargik.org/wiki/Download/Installation_Milestone_1.9
# last revised     : 04.01.2008

# You will need ftjam to build it

softname='supertux'
softversion='0.3.1d'
packageversion='50.1'

arch='i686'
cpu='i686'

prefix=/usr

# log ;)
(

# parameters needed for the build process
buildir=$(pwd)
srcpkg="$buildir/$softname-$softversion.tar.bz2"
src="$buildir/$softname-0.3.1"
package="$softname-$softversion-$arch-$packageversion"
dest="$buildir/$package"

# prepare the build result directory
dest="$buildir/$package"
rm -rf $dest
mkdir -p $dest
mkdir -p $dest/install
mkdir -p $dest/usr/doc/$softname-$softversion
mkdir -p $dest/usr/share/applications
mkdir -p $dest/usr/share/icons/hicolor/48x48/apps/
mkdir -p $dest/usr/src/$softname-$softversion

# slack-desc
cat <<EOF > $dest/install/slack-desc
        |-----handy-ruler------------------------------------------------------|
supertux: supertux (2D jump'n run sidescroller game)
supertux: 
supertux: SuperTux is a classic 2D jump'n run sidescroller game 
supertux: in a style similar to the original SuperMario games.
supertux:  
supertux:   
supertux: 
supertux: 
supertux: 
supertux:
supertux: 
EOF

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

# building
    export CFLAGS="-O2 -march=$arch -mtune=$cpu"
    export CXXFLAGS="-O2 -march=$arch -mtune=$cpu"   
    
    ./configure \
      --prefix=$prefix \
      --sysconfdir=/etc \
      --localstatedir=/var/lib \
      --datadir=/usr/share \
      --build=$arch-slackware-linux \
     
    jam
    jam install DESTDIR=$dest

# add 'default' files
    cp -a \
      COPYING INSTALL README WHATSNEW.txt \
      $dest/usr/doc/$softname-$softversion 
    rm -rf $dest/usr/share/doc

# 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
)

# fix the .desktop file ;)
cd $dest/usr/share/applications/
sed -i 's@supertux.png@supertux@g' supertux2.desktop

# Make the package freedesktop compliant
cp $src/data/images/engine/icons/$softname.png $dest/usr/share/icons/hicolor/48x48/apps/

# put the ZenWalk build in the right directory
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.tgz

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

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