#!/bin/bash

#author				: hennes (jm@rapidrabbit.de) -> original build-script by Michael Verret (AxXium)
#last changed		: 01-august-2007

#package			: luola
#homepage			: http://luolamies.org/software/luola/
#source location	: http://luolamies.org/software/luola/luola-1.3.0.tar.gz
#patch1 location	: http://luolamies.org/software/luola/luola-1.3.0-1.patch
#patch1 location	: http://luolamies.org/software/luola/luola-1.3.0-2.patch
#stdlevvels			: http://luolamies.org/software/luola/stdlevels-6.0.tar.gz
#nostlevels			: http://luolamies.org/software/luola/nostalgia-1.2.tar.gz
#dependencies		: SDL_gfx,coreutils,cxxlibs,directfb,expat,fontconfig,freeglut,freetype,gcc,gcc-g++,gdbm,libdrm,libjpeg,libpng,libtermcap,libtiff,mesa,ncurses,openssl,perl,sdl,sysfsutils,utempter,xorg,xterm,zlib

# ----- credentials
softname="luola"
softversion="1.3.0"
srcurl="http://luolamies.org/software/luola/luola-1.3.0.tar.gz"
pat1url="http://luolamies.org/software/luola/luola-1.3.0-1.patch"
pat2url="http://luolamies.org/software/luola/luola-1.3.0-2.patch"
stdlevurl="http://luolamies.org/software/luola/stdlevels-6.0.tar.gz"
nostlevurl="http://luolamies.org/software/luola/nostalgia-1.2.tar.gz"
arch=i486
cpu=i686
build="1"
zenversion="48"
packageversion="$zenversion.$build"

# ----- verify script
if [ "$(id -u)" != "0" ]; then
			echo "Please run as root!"
			exit 0
fi

# ----- help
if [ "${1}" = "--help" ]; then
	echo \
"$softname ZenBuild

usage:	--help 		Display this text
	--cleanup	Clean up after creating the package
"
	exit 0
fi

# ----- build parameters
numjobs="-j3"
builddir="$(pwd)"
package="$softname-$softversion-$arch-$packageversion"

# ----- prepare directories
dest="$builddir/$package"
rm -rf $dest
prefix="/usr"
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/32x32/apps
mkdir -p $dest/usr/share/icons/hicolor/36x36/apps
mkdir -p $dest/usr/share/icons/hicolor/48x48/apps
mkdir -p $dest/usr/share/icons/hicolor/64x64/apps
mkdir -p $dest/usr/doc
destsrc="$dest/usr/src/$softname-$softversion"
mkdir -p $destsrc

# ----- make slack-desc-file
cat << EOF > $dest/install/slack-desc
     |-----handy-ruler------------------------------------------------------|
luola: Luola is a cavern-flying game for 1-4 players
luola: 
luola: Luola is a 2D arcade game where you fly a small V shaped ship
luola: in different kinds of levels. It's genre "Luolalentely"
luola: (Cave-flying) is (or was) very popular in Finland. Though
luola: cavern-flying games are not originally Finnish, nowdays most
luola: of them are. Luola is largely inspired by V-Wing, a popular
luola: cave-flying game. While V-Wing's graphics might not have been
luola: flashy, they were crisp and clear, and the playability and
luola: choice of weapons was superb. This is the aim with Luola, not
luola: too much eye-candy, but solid playability.
EOF

# ----- get patches
if [ -f luola-1.3.0-1.patch ]; then
   echo "looking for patch 1 .. found"
else
   echo "looking for patch 1 .. none found"
   wget -c $pat1url
fi
if [ -f luola-1.3.0-2.patch ]; then
   echo "looking for patch 2 .. found"
else
   echo "looking for patch 2 .. none found"
   wget -c $pat2url
fi

# ----- get levels
if [ -f stdlevels-6.0.tar.gz ]; then
   echo "looking for standard levels .. found"
else
   echo "looking for standard levels .. none found"
   wget -c $stdlevurl
fi
if [ -f nostalgia-1.2.tar.gz ]; then
   echo "looking for nostalgia levels .. found"
else
   echo "looking for nostalgia levels .. none found"
   wget -c $nostlevurl
fi

# ----- extract source-archive
srcdir=$softname-$softversion
rm -rf $srcdir
if [ -f $softname-$softversion.tar.gz ]; then
   echo "looking for source-file .. found"
else
   echo "looking for source-file .. none found"
   wget -c $srcurl
fi
tar -zxvf $softname-$softversion.tar.gz
cd $srcdir

# ----- create package-dependent directories
mkdir -p $dest/usr/doc/$srcdir

# ----- set target-permissions
chown -R root.root .
find . -perm 777 -exec chmod 755 {} \;
find . -perm 775 -exec chmod 755 {} \;
find . -perm 711 -exec chmod 755 {} \;
find . -perm 666 -exec chmod 644 {} \;
find . -perm 664 -exec chmod 644 {} \;
find . -perm 600 -exec chmod 644 {} \;
find . -perm 555 -exec chmod 755 {} \;
find . -perm 511 -exec chmod 755 {} \;
find . -perm 444 -exec chmod 644 {} \;
find . -perm 440 -exec chmod 644 {} \;
find . -perm 400 -exec chmod 644 {} \;

# ----- patch files
patch -p0 src/parser.c ../luola-1.3.0-1.patch
patch -p0 src/animation.c ../luola-1.3.0-2.patch

# ----- configure
export CFLAGS="-O2 -march=$arch -mtune=$cpu"
export CXXFLAGS="-O2 -march=$arch -mtune=$cpu"
./configure \
	--prefix=$prefix \
	--disable-debug
	
# ----- make
make $numjobs
	
# ----- make install
make install DESTDIR=$dest

# ----- install levels
tar -zxvf $builddir/nostalgia-1.2.tar.gz --directory=$dest/usr/share/$softname/levels
tar -zxvf $builddir/stdlevels-6.0.tar.gz --directory=$dest/usr/share/$softname/levels

# ----- add default-files
cp -a \
	AUTHORS COPYING ChangeLog DATAFILE FAQ INSTALL LEVELFILE NEWS README RELEASENOTES.txt RIP TODO \
	$dest/usr/doc/$srcdir

# ----- treat icon-files
tar -jxvf $builddir/icons.tar.bz2 --directory=$builddir
mv $builddir/icon-luola-32.png $dest/usr/share/icons/hicolor/32x32/apps/$softname.png
mv $builddir/icon-luola-36.png $dest/usr/share/icons/hicolor/36x36/apps/$softname.png
mv $builddir/icon-luola-48.png $dest/usr/share/icons/hicolor/48x48/apps/$softname.png
mv $builddir/icon-luola-64.png $dest/usr/share/icons/hicolor/64x64/apps/$softname.png
cp $builddir/icons.tar.bz2 $destsrc

# ----- make .desktop-file
cat << "EOF" > $dest/usr/share/applications/$softname.desktop
[Desktop Entry]
Name=Luola
Comment=A cavern-flying game for 1-4 players
Exec=luola
Icon=luola
Terminal=false
Type=Application
Categories=Game;ArcadeGame;
Encoding=UTF-8
EOF

# ----- copy build-file
cp "$builddir/build-$softname.sh" "$destsrc/build-$softname.sh"
chmod a+x "$destsrc/build-$softname.sh"

# ----- post-build processing
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
	
# ----- packaging
chown -R root.root $dest
cd $dest
makepkg -l y -c n $dest.tgz
cd ..
md5sum $package.tgz > $package.md5
cat << "EOF" > $package.dep
SDL_gfx,coreutils,cxxlibs,directfb,expat,fontconfig,freeglut,freetype,gcc,gcc-g++,gdbm,libdrm,libjpeg,libpng,libtermcap,libtiff,mesa,ncurses,openssl,perl,sdl,sysfsutils,utempter,xorg,xterm,zlib
EOF

# ----- cleaning up
if [ "${1}" = "--cleanup" ]; then
rm -r $dest
rm -r $builddir/$srcdir
fi
