#!/bin/bash
#
# author           : George Vlahavas (vlahavas~AT~gmail~DOT~com, gapan@zenwalk forums)
# project web page : http://briquolo.free.fr/en
# source           : http://briquolo.free.fr/download/briquolo-0.5.6.tar.bz2

# Package name
NAME="briquolo"
# Package version
VERSION="0.5.6"
# Source code URL
SOURCE="http://briquolo.free.fr/download/briquolo-$VERSION.tar.bz2"
# ./configure options
CONFIGOPTIONS="--prefix=/usr"
# Files that should be placed in /usr/doc/package-version/
DOCS="ABOUT-NLS AUTHORS COPYING ChangeLog DejaVuSans.ttf-LICENSE INSTALL NEWS README README.fr"

CWD=`pwd`
PKG="$CWD/$NAME-install"
ARCH="i486"
CPU="i686"
BUILD="48.1"

rm -rf $PKG
mkdir -p $PKG

# Create package install directory
mkdir $PKG/install

# Create the slack-desc file
cat > $PKG/install/slack-desc << END
$NAME: Briquolo - a 3D breakout clone
$NAME:
$NAME: Briquolo is a breakout clone with 3D representation based on
$NAME: OpenGL.
$NAME:
$NAME: 
$NAME:
$NAME:
$NAME:
$NAME:
$NAME:
END

# Check to see if source is present. If it's not, download it
rm -rf $NAME-$VERSION
if [ -f $NAME-$VERSION.tar.bz2 ]; then
	echo "Source file already here, no need to download";
else
 	echo "Downloading source"
	wget -c $SOURCE
fi

# Check to see if an icon is present. If it's not, download it
if [ -f $NAME.png ]; then
	echo "Icon file already here, no need to download";
else
 	echo "Downloading icon file"
#	wget -c http://users.zenwalk.org/user-accounts/gapan/$NAME/$NAME.png
fi

# Unpack source
tar xvjf $NAME-$VERSION.tar.bz2

#
# building-computer dependent: the number of parallel 'jobs' of 'make'
#
# 'heavy' computer: 6 build-jobs at once
#
#numjobs='-j6'
#
# 'light' computer: 2 build-jobs at once
#
numjobs='-j2'

# Configure and make
cd $NAME-$VERSION
export CFLAGS="-O2 -march=$ARCH -mtune=$CPU"
export CXXFLAGS="-O2 -march=$ARCH -mtune=$CPU"
# Make some patches
sed -i -e 's/-O3/-O2 -mtune=i486 -march=i686/' configure
sed -i -e 's/-O3/-O2 -mtune=i486 -march=i686/' configure.ac
sed -i -e 's/-O3/-O2 -mtune=i486 -march=i686/' data/tableau/old/Makefile
sed -i -e 's/-O3/-O2 -mtune=i486 -march=i686/' data/tableau/Makefile
sed -i -e 's/-O3/-O2 -mtune=i486 -march=i686/' data/tableau/new/Makefile
sed -i -e 's/-O3/-O2 -mtune=i486 -march=i686/' data/data/Makefile
sed -i -e 's/-O3/-O2 -mtune=i486 -march=i686/' data/Makefile
./configure $CONFIGOPTIONS
make $numjobs

# Don't install the icon, it gets installed in the wrong place anyway
sed -i -e 's,install-data-am\: install-data-local install-iconDATA,install-data-am\: install-data-local,' desktop/Makefile

# Install on temporary directory
mkdir -p $PKG/usr/share/applications
make install DESTDIR=$PKG

# Fix desktop file
sed -i -e 's,Icon=/usr/share/pixmaps/briquolo.svg,Icon=briquolo,' $PKG/usr/share/applications/briquolo.desktop

# Put icons in the right place
mkdir -p $PKG/usr/share/icons/hicolor/scalable/apps
mkdir -p $PKG/usr/share/icons/hicolor/48x48/apps
cp -a $CWD/$NAME-$VERSION/desktop/briquolo.svg $PKG/usr/share/icons/hicolor/scalable/apps
cp -a $CWD/$NAME.png $PKG/usr/share/icons/hicolor/48x48/apps

# Strip binaries
cd $PKG
find . | xargs file | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null

# Copy Docs
mkdir -p $PKG/usr/doc/$NAME-$VERSION
cd $CWD/$NAME-$VERSION
cp -a $DOCS $PKG/usr/doc/$NAME-$VERSION

# Compress the man page(s)
find $PKG/usr/man -type f -exec gzip -9 {} \;

# Add the build-file in the package
mkdir -p $PKG/usr/src/$NAME-$VERSION
cp "$CWD/build-$NAME.sh" "$PKG/usr/src/$NAME-$VERSION/build-$NAME.sh"
chmod a+x "$PKG/usr/src/$NAME-$VERSION/build-$NAME.sh"

# Make sure ownerships and permissions are sane
cd $PKG
chown -R root:root .
find . -perm 666 -exec chmod 644 {} \;
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 {} \;

# Make the package
makepkg -l y -c n $CWD/$NAME-$VERSION-$ARCH-$BUILD.tgz

# Calculate md5sum
cd $CWD/
md5sum $NAME-$VERSION-$ARCH-$BUILD.tgz > $NAME-$VERSION-$ARCH-$BUILD.md5

# Remove source-code and temporary install directories
# since they are not needed anymore
rm -rf $CWD/$NAME-$VERSION
rm -rf $PKG
