#!/bin/bash
# This is a script to build a Zenwalk package. Check the Zenwalk homepage: www.zenwalk.org
#
# SOFTWARE			: lincity-ng-1.1.2
# PROJECT HOMEPAGE 	: http://lincity-ng.berlios.de/wiki/index.php/Main_Page
# SOURCE           	: http://download.berlios.de/lincity-ng/
#
# AUTHOR         	: skywatcher (s.skywatcher|AT|google|DOT|com, simone antoniucci)
# LATEST REVISION	: 2009-01-31
#
# BUILD-ONLY DEPS	: ftjam
# NOTES				: -

### Checking for unbound variables
set -u

### Setting main variables
NAME="lincity-ng"
VERSION="2.0"
ARCH="i486"
CPU="i686"
BUILD=54.1

HOMEPAGE="http://lincity-ng.berlios.de/wiki/index.php/Main_Page"
DESCRIPTION="City simulation game"
SRCLNK="http://download.berlios.de/lincity-ng/$NAME-$VERSION.tar.bz2"

CWD=`pwd`
PKGDIR=$CWD/$NAME-install
PKG=$NAME-$VERSION-$ARCH-$BUILD
SRCPKG=$NAME-$VERSION.tar.bz2
SRCDIR=$CWD/$NAME-$VERSION
ICONDIR=$PKGDIR/usr/share/icons/hicolor/48x48/apps/		

URL=http://zenwalk.pinguix.com/user-accounts/skywatcher/$NAME/$VERSION

ADD="Jamrules"
ADDLNK=$URL/$ADD

DOCS="COPYING* README RELNOTES TODO"

NJOBS="-j2"
export CFLAGS="-O2 -march=$ARCH -mtune=$CPU"
export CXXFLAGS="-O2 -march=$ARCH -mtune=$CPU"

### Cleaning
rm -rf $PKGDIR
rm -rf $SRCDIR

### Mkdir
mkdir -p $PKGDIR
mkdir -p $PKGDIR/install
mkdir -p $PKGDIR/usr/doc/$NAME-$VERSION
mkdir -p $PKGDIR/usr/src/$NAME-$VERSION
mkdir -p $PKGDIR/usr/share/applications
mkdir -p $ICONDIR

mkdir -p $PKGDIR/usr/bin
mkdir -p $PKGDIR/usr/share/$NAME
mkdir -p $PKGDIR/usr/man/man6

### Checking if source is present. If it's not, download it
if [ -f $SRCPKG ]; then
	echo "Source file is already present, no need to download it";
else
 	echo "Downloading source..."
	wget -c -nv $SRCLNK
fi

### Checking if additional stuff is present. If it's not, download it
if [ -f $ADD ]; then
	echo "Additional stuff is already present, no need to download it";
else
 	echo "Downloading additional stuff..."
	wget -c -nv $ADDLNK
fi

### Extracting source
tar xfv $SRCPKG

### Configure, make, install
cd $SRCDIR
./configure --prefix=/usr || exit 1

#change flags
cp -a $CWD/$ADD $SRCDIR
jam || exit 1
#jam install || exit 1

#cannot use install since jam does not accept DESTDIR; using prefix=$PKGDIR/usr causes the executable not to find the game data
#just copy things around
cp -a $NAME $PKGDIR/usr/bin
cp -a data/* $PKGDIR/usr/share/$NAME
cp -a doc/$NAME.6 $PKGDIR/usr/man/man6

### Stripping binaries
cd $PKGDIR
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

### Docs, man, info
cd $SRCDIR
cp -a $DOCS $PKGDIR/usr/doc/$NAME-$VERSION
find $PKGDIR/usr/man -type f -exec gzip -9 {} \;

### Adding build-file and other stuff to the package
cp -a $CWD/$0 $PKGDIR/usr/src/$NAME-$VERSION/build-$NAME.sh
chmod a+x $PKGDIR/usr/src/$NAME-$VERSION/build-$NAME.sh
cp -a $CWD/$ADD $PKGDIR/usr/src/$NAME-$VERSION

### Slack-desc 
#    |-----handy-ruler------------------------------------------------------|
cat > $PKGDIR/install/slack-desc << EOF
$NAME: $NAME - $DESCRIPTION
$NAME:  
$NAME: LinCity-NG is a City Simulation Game. It is a polished and improved
$NAME: version of the classic LinCity game, for which a new iso-3D graphics 
$NAME: engine has been created, with a completely redone and modern GUI.
$NAME: 
$NAME: 
$NAME: 
$NAME: 
$NAME: 
$NAME: $HOMEPAGE
EOF

### Desktop entry
cp -a $SRCDIR/$NAME.desktop $PKGDIR/usr/share/applications
sed -i s:lincity-ng.png:lincity-ng: $PKGDIR/usr/share/applications/$NAME.desktop
sed -i s:"electricityand":"electricity and": $PKGDIR/usr/share/applications/$NAME.desktop

### Icon
mv $PKGDIR/usr/share/$NAME/$NAME.png $ICONDIR
cd $ICONDIR
mogrify -resize 48x48 $NAME.png

### Ownerships and permissions
cd $PKGDIR
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 {} \;

### Unnedeed stuff
#rm -fr $PKGDIR/usr/share/pixmaps
#rm -fr $PKGDIR/usr/share/doc
rm -fr $PKGDIR/usr/share/$NAME/Jamfile
rm -fr $PKGDIR/usr/share/$NAME/gui/Jamfile

### Making the package
makepkg -l y -c n $CWD/$PKG.tgz

### md5 file
cd $CWD
md5sum $PKG.tgz > $PKG.md5

### dep file
#~ requiredbuilder -z $PKG.tgz > $PKG.dep
depfinder $PKG.tgz > $PKG.dep

### txt file
cat $PKGDIR/install/slack-desc > $PKG.txt

### src file
rm -fr $PKG.src
for link in $URL/$0 $SRCLNK $ADDLNK
	do echo $link >> $PKG.src
done

### Cleaning
rm -rf $SRCDIR
rm -rf $PKGDIR

#Done. Ciao!
