#!/bin/bash
# packager           : Frederic Boulet <bipbip> bipbiponlinux~AT~gmail.com
# homepage		     : http://icculus.org/neverball/
# source location    : http://archive.xfce.org/src/apps/parole/0.1/parole-0.1.99.tar.bz2
# last revised       : 01.12.2009
# dep                : SDL

softname='neverball'
softname_bonus='neverputt'
softversion='1.5.4'
packageversion='1fb'

arch='i486'
cpu='i686'

prefix=/usr

# log
(

# parameters needed for the build process
buildir=$(pwd)
src="$buildir/$softname-$softversion"
srcpkg="$buildir/$softname-$softversion.tar.gz"
package="$softname-$softversion-$arch-$packageversion"
dest="$buildir/$package"
source=http://neverball.org/$softname-$softversion.tar.gz

# 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/neverball/data
mkdir -p $dest/usr/share/applications
mkdir -p $dest/usr/share/icons/hicolor/48x48/apps
mkdir -p $dest/usr/man/man6
mkdir -p $dest/usr/doc
mkdir -p $dest/usr/src/$softname-$softversion

# Slack-desc
cat <<EOF > $dest/install/slack-desc 
         |-----handy-ruler------------------------------------------------------|
neverball: Neverball - Puzzle & Action Game
neverball:
neverball: Tilt the floor to roll a ball through an obstacle course before
neverball: time runs out. Neverball is part puzzle game, part action game,
neverball: and entirely a test of skill. Also found here is Neverputt, a
neverball: hot-seat multiplayer miniature golf game using the physics and
neverball: graphics of Neverball.
neverball:
neverball: 
neverball:
neverball:
EOF

# check if source is 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

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

# build
export CFLAGS="-O2 -march=$arch -mtune=$cpu"
export CXXFLAGS="-O2 -march=$arch -mtune=$cpu"

patch -p0 < ../flags.patch 
patch -p0 < ../base_config.h.patch

make 

cp -a $softname $dest/usr/bin
cp -a $softname_bonus $dest/usr/bin
cp -a data $dest/usr/share/$softname
cp -a locale $dest/usr/share/
cp -a dist/*.6 $dest/usr/man/man6

# make icon
cp dist/neverball_48.png $dest/usr/share/icons/hicolor/48x48/apps/$softname.png
cp dist/neverputt_48.png $dest/usr/share/icons/hicolor/48x48/apps/$softname_bonus.png

# make .desktop
cat << "EOF" > $dest/usr/share/applications/$softname.desktop
[Desktop Entry]
Name=Neverball
Comment=A 3D arcade game with a ball
Exec=neverball-bin
Icon=neverball
Terminal=false
Type=Application
Categories=Game;ActionGame;
Encoding=UTF-8
EOF

cat << "EOF" > $dest/usr/share/applications/$softname_bonus.desktop
[Desktop Entry]
Version=1.0
Type=Application
Name=Neverputt
Comment=A 3D mini golf game
Comment[ca]=Un joc de minigolf en 3D
Comment[de]=Ein 3D-Minigolf Spiel
Comment[fr]=Un jeu de minigolf en 3D
Comment[nn]=Minigolfspel i 3D
Icon=neverputt
Exec=neverputt-bin
Terminal=false
Categories=Game;ArcadeGame;
EOF

cat <<EOF > $dest/usr/bin/neverball-bin
#!/bin/sh
NEVERBALL_DATA=/usr/share/neverball/data neverball "\$@"
EOF

chmod 755 $dest/usr/bin/neverball-bin

cat <<EOF > $dest/usr/bin/neverputt-bin
#!/bin/sh
NEVERBALL_DATA=/usr/share/neverball/data neverputt "\$@"
EOF

chmod 755 $dest/usr/bin/neverputt-bin

# add 'default' files
	cd $src
    mkdir -p $dest/usr/doc/$softname-$softversion
    cp -a \
      COPYING INSTALL README CHANGES\
      $dest/usr/doc/$softname-$softversion
	cp doc/* $dest/usr/doc/$softname-$softversion

# strip executables
( 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
)

# SalixBuild
cd $buildir
cp build-$softname.sh $dest/$prefix/src/$softname-$softversion
cp flags.patch $dest/$prefix/src/$softname-$softversion
cp base_config.h.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 $dest.txz

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

# .dep
cd $buildir
echo -e "physfs,mesa,sdl" > $package.dep

# .src
echo http://zenwalk.pinguix.com/user-accounts/bip/divers/salix/games/neverball/build-neverball.sh > $package.src
echo http://zenwalk.pinguix.com/user-accounts/bip/divers/salix/games/neverball/flags.patch >> $package.src
echo http://zenwalk.pinguix.com/user-accounts/bip/divers/salix/games/neverball/base_config.h.patch >> $package.src
echo $source >> $package.src

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

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