#!/bin/bash
#
# packager                          : Frederic Boulet <bipbip> bipbiponlinux~AT~gmail.com
#
# dernières révisions       : 4/12/2008
#
#package			: slime volley
#homepage			: http://slime.tuxfamily.org/
#source location	: http://downloads.tuxfamily.org/slime/debian/dists/stable/main/source/slimevolley_2.4.1.tar.gz
#dependency         : SDL

softname='slimevolley'
softversion='2.4.1'
packageversion='54.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-$softversion"
srcpkg="$buildir/"$softname"_$softversion.tar.gz"
package="$softname-$softversion-$arch-$packageversion"
dest="$buildir/$package"
source=http://downloads.tuxfamily.org/slime/debian/dists/stable/main/source/"$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/$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

#
# mettre le slack-desc en place. 
#
cat <<EOF > $dest/install/slack-desc 
           |-----handy-ruler------------------------------------------------------|
slimevolley: slimevolley - a volley ball game
slimevolley:
slimevolley: Slime Volley is a 2D volleyball game written in C using SDL.
slimevolley: 
slimevolley: 
slimevolley: 
slimevolley: 
slimevolley:
slimevolley:
slimevolley:
slimevolley:
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"
 
sed -i -e 's|${CMAKE_INSTALL_PREFIX}|/usr|g' CMakeLists.txt
sed -i -e 's|/share/locale|/locale|g' CMakeLists.txt
sed -i -e 's|/share/games/slimevolley|/share/slimevolley|g' CMakeLists.txt
sed -i -e 's|/share/man/man6|/man/man6|g' CMakeLists.txt
sed -i -e 's|${CMAKE_INSTALL_PREFIX}|/usr|g' data/CMakeLists.txt
sed -i -e 's|games|/usr/bin|g' CMakeLists.txt
 
cmake .   

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

#
# installer le package dans son répertoire de destination
#
make install DESTDIR=$dest
   
#
# "faire" icon
#
convert data/icone.png -resize 48x48 $dest/usr/share/icons/hicolor/48x48/apps/slimevolley.png

# "faire" .desktop
rm $dest/usr/share/applications/$softname.desktop
cat << "EOF" > $dest/usr/share/applications/$softname.desktop
[Desktop Entry]
Encoding=UTF-8
Name=Slime Volley
Encoding=UTF-8
Comment=2D volleyball simulation
Comment[fr]=Simulation de volley ball en 2D
Exec=slimevolley
Icon=slimevolley
Terminal=false
Type=Application
Categories=Game;SportsGame;
EOF

#
# ajout des fichiers de docs, infos, ...
#
	cd $src
    mkdir -p $dest/usr/doc/$softname-$softversion
    cp -a \
      README CREDIT TODO\
      $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
#
#~ ( cp $src/slimevolley.6 $dest/usr/man/man6
  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 "directfb,freetype,libjpeg,libpng,libtiff,sdl,zlib" > $package.dep

#
# src file
#
echo http://zenwalk.pinguix.com/user-accounts/bip/extra/games/slimevolley/source/build-slimevolley.sh > $package.src
echo $source >> $package.src

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

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