#!/bin/bash
#
# packager                          : Frederic Boulet <bipbip> bipbiponlinux~AT~gmail.com
#
# dernières révisions       : 9/08/2009
#
#package			: vacuum
#homepage			: http://apocalypse.rulez.org/vacuum
#source location	: http://downloads.sourceforge.net/vacuum/vacuum-0.13a.tar.gz
#dependency         : SDL,SDL_perl

softname='vacuum'
softversion='0.13a'
softversionsrc='0.13'
packageversion='62.1'

#
# ZenWalk standard: type CPUet architecture
#
# arch='i486' -> le compileur utilise seulement les instructions 80486
# cpu='i686'  -> optimisation faîte pour PII et >
#
arch='i486'
cpu='i686'

#
# destination du package
#
prefix=/usr

#
# log
#
(

#
# building-computer dependent: the number of parallel 'jobs' of 'make'
#
# "gros" PC: 6 compilations à la fois
#
# numjobs='-j6'
#
# "léger" PC: 2 compilations à la fois
#
numjobs='-j2'

#
# paramètres pour compiler
#
buildir=$(pwd)
src="$buildir/$softname-$softversionsrc"
srcpkg="$buildir/$softname-$softversion.tar.gz"
package="$softname-$softversion-$arch-$packageversion"
dest="$buildir/$package"
source=http://downloads.sourceforge.net/vacuum/$softname-$softversion.tar.gz

#
# preparer le répertoire qui accueillera la compilation
#
rm -rf $dest
mkdir -p $dest
mkdir -p $dest/install
mkdir -p $dest/usr/doc
mkdir -p $dest/usr/share/applications
mkdir -p $dest/usr/share/icons/hicolor/48x48/apps
mkdir -p $dest/usr/src/$softname-$softversion

#
# mettre le slack-desc en place. 
#
cat <<EOF > $dest/install/slack-desc 
      |-----handy-ruler------------------------------------------------------|
vacuum: vacuum - A fast-paced action game
vacuum:
vacuum: The point of the game is using your magical vacuum field to collect 
vacuum: food and defend against monsters. Food and certain monsters can also 
vacuum: be spat out and used as a projectile against other monsters. 
vacuum: 
vacuum: 
vacuum: 
vacuum: 
vacuum: 
vacuum:
EOF

#
# Verifie si package est present
#
if [ -f $softname-$softversion.tar.gz ]; then
	echo "Source file already here, no need to download";
else
 	echo "Downloading source"
	wget -c --no-check-certificate $source
fi

#
# extraire les sources
#
# tar.gz : tar zxvf $srcpkg
# tar.bz2: tar xjvf $srcpkg
# tgz    : tar xzvf $srcpkg
#
cd $buildir
tar xzvf $srcpkg
cd $src	

#
# "faire" le package package
#
export CFLAGS="-O2 -march=$arch -mtune=$cpu"
export CXXFLAGS="-O2 -march=$arch -mtune=$cpu"


./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

#
# "faire" le package package
#
make $numjobs DESTDIR=$dest

#
# installer le package dans son répertoire de destination
#
make install DESTDIR=$dest

#
# faire le fichier .desktop
#
cat << "EOF" > $dest/usr/share/applications/$softname.desktop
[Desktop Entry]
Name=Vacuum
Comment=A fast-paced action game
Exec=vacuum
Icon=vacuum
Terminal=false
Type=Application
Categories=Application;Game;
EOF
   
cp ../vacuum.png  $dest/usr/share/icons/hicolor/48x48/apps
   
#
# ajout des fichiers de docs, infos, ...
#
	cd $src
    mkdir -p $dest/usr/doc/$softname-$softversion
    cp -a \
      NEWS COPYING INSTALL TODO VERSION AUTHORS ChangeLog README \
      $dest/usr/doc/$softname-$softversion
    
#
# Stripper les éxécutables
# (Commenter ces lignes si besoin d'infos de débuggage)
#
( 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
)

#
# Compresser les pages Man
#
( cd $dest/$prefix/man
  find . -name "*.?" | xargs gzip -9
)

#
# Mettre le zenbuild à sa place
#
cd $buildir
cp build-$softname.sh $dest/$prefix/src/$softname-$softversion

#
# fixer les 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 {} \;

#
# fabriquer le zenpackage
#
cd $dest
makepkg -l y -c n $dest.tgz

#
# produire la signature md5 du package
#
cd $buildir
md5sum $package.tgz > $package.md5

#
# produire le fichier .dep
#
cd $buildir
echo -e "SDL_perl,perl" > $package.dep

#
# produire le fichier source
#
echo http://zenwalk.pinguix.com/user-accounts/bip/EXTRA/games/vacuum/source/build-vacuum.sh > $package.src
echo http://zenwalk.pinguix.com/user-accounts/bip/EXTRA/games/vacuum/source/vacuum.png >> $package.src
echo $source >> $package.src

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

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