#!/bin/bash
# packager           : Markus Muttilainen <stillborn.zenwalk@gmail.com>
# source location    : http://tilda.sourceforge.net/wiki/index.php/Main_Page
# last revised       : 22.10.2010
set -e -x -u

softname='tilda'
softversion='0.9.6'
packageversion='66.1'

arch='i486'
cpu='i686'

# log ;)
(

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

# prepare the build result directory
dest="$buildir/$package"
rm -rf $dest
mkdir -p $dest
mkdir -p $dest/install
mkdir -p $dest/usr/doc/$softname-$softversion
mkdir -p $dest/usr/share/icons/hicolor/48x48/apps/
mkdir -p $dest/usr/share/applications
mkdir -p $dest/usr/share/$softname
mkdir -p $dest/usr/src/$softname-$softversion

# Slack-desc
cat <<EOF > $dest/install/slack-desc
     |-----handy-ruler------------------------------------------------------|
tilda: Tilda (a drop-down terminal)
tilda:
tilda: Tilda is a Linux terminal taking after the likeness of many classic
tilda: terminals from first person shooter games, Quake, Doom and Half-Life
tilda: (to name a few), where the terminal has no border and is hidden from
tilda: the desktop until a key is pressed.
tilda:
tilda: 
tilda:
tilda: 
tilda:

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 xvf $srcpkg
cd $src

# build

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

	patch -p1 < ../03_gtk_window_realize.dpatch --verbose || exit 1
        patch -Np1 -i $buildir/fix.patch --verbose || exit 1

    ./configure \
      --prefix=/usr \
      --datarootdir=/usr/share/$softname \
      --localedir=/usr/share/locale \
      --program-prefix="" \
      --program-suffix="" \
      --build=$arch-slackware-linux

    make || exit 1
    make install DESTDIR=$dest || exit 1

# add 'default' files
    cp -a \
      ABOUT-NLS AUTHORS ChangeLog COPYING INSTALL NEWS README TODO \
      $dest/usr/doc/$softname-$softversion
      
# Strip
( cd $dest
  find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | xargs -r strip --strip-unneeded 2> /dev/null
  find . | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | xargs -r strip --strip-unneeded 2> /dev/null
)

# .desktop file
rm -rf $dest/usr/share/$softname/applications/
cat << "EOF" > $dest/usr/share/applications/$softname.desktop 
[Desktop Entry]
Encoding=UTF-8
Name=Tilda
Comment=A drop down terminal
Exec= tilda
Icon=tilda
Terminal=false
Type=Application
Categories=System;TerminalEmulator;
StartupNotify=true
EOF

# Make the package freedesktop compliant
cp $dest/usr/share/$softname/pixmaps/$softname.png $dest/usr/share/icons/hicolor/48x48/apps/

# ZenBuild
cp $buildir/build-$softname.sh $dest/usr/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 {} \;

#> helperline
(cd $dest;find . -printf "%y %m %u:%g %p %l\n") >$buildir/pkgtree.log

# real packaging work
cd $dest
makepkg -l y -c n $dest.txz

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

# generate source file
echo http://www.auroracore.biz/user-accounts/stillborn/tilda/build-tilda.sh > $package.src
echo http://www.auroracore.biz/user-accounts/stillborn/tilda/03_gtk_window_realize.dpatch.sh >> $package.src
echo http://www.auroracore.biz/user-accounts/stillborn/tilda/fix.patch >> $package.src
echo $source >> $package.src

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

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