#!/bin/sh
# Heavily based on the Slackware 11.0 SlackBuild
# http://web.wt.net/~billw/gkrellm/gkrellm.html
# Packager Vincenzi Loris - slacky@slacky.it

CWD=`pwd`
TMP=${TMP:-/tmp/tgz}
PKG=$TMP/package
NAME=gkrellm
VERSION=2.3.1
COUNTDOWN=0.1.1
ARCH=i686
BUILD=1sl

if [ ! -d $TMP ]; then
 mkdir -p $TMP
fi
if [ ! -d $PKG ]; then
 mkdir -p $PKG
fi

cd $TMP
tar xjvf $CWD/$NAME-$VERSION.tar.bz2
cd $NAME-$VERSION
find . \( -perm 777 -o -perm 775 -o -perm 711 \) -exec chmod 755 {} \;
find . \( -perm 700 -o -perm 555 -o -perm 511 \) -exec chmod 755 {} \;
find . \( -perm 666 -o -perm 664 -o -perm 600 \) -exec chmod 644 {} \;
find . \( -perm 444 -o -perm 440 -o -perm 400 \) -exec chmod 644 {} \;
zcat $CWD/config-theme.diff.gz | patch -p1 --verbose
make \
INSTALLROOT=/usr \
INSTALLDIR=/usr/bin \
INCLUDEDIR=/usr/include \
MANDIR=/usr/man/man1 \
LOCALEDIR=/usr/share/locale \
PKGCONFIGDIR=/usr/lib/pkgconfig
make install
make install \
INSTALLROOT=$PKG/usr \
INSTALLDIR=$PKG/usr/bin \
INCLUDEDIR=$PKG/usr/include \
MANDIR=$PKG/usr/man/man1 \
LOCALEDIR=$PKG/usr/share/locale \
PKGCONFIGDIR=$PKG/usr/lib/pkgconfig
mkdir -p $PKG/usr/doc/$NAME-$VERSION
cp -a Changelog Changelog-* COPYRIGHT CREDITS INSTALL README $PKG/usr/doc/$NAME-$VERSION
cd $TMP
mkdir -p $PKG/usr/lib/gkrellm2/plugins
tar xzf $CWD/gkrellm-countdown-$COUNTDOWN.tar.gz
cd gkrellm-countdown
find . \( -perm 777 -o -perm 775 -o -perm 711 \) -exec chmod 755 {} \;
find . \( -perm 700 -o -perm 555 -o -perm 511 \) -exec chmod 755 {} \;
find . \( -perm 666 -o -perm 664 -o -perm 600 \) -exec chmod 644 {} \;
find . \( -perm 444 -o -perm 440 -o -perm 400 \) -exec chmod 644 {} \;
make
cp gkrellm-countdown.so $PKG/usr/lib/gkrellm2/plugins
mkdir -p $PKG/usr/doc/gkrellm-countdown-$COUNTDOWN
cp -a COPYING ChangeLog README $PKG/usr/doc/gkrellm-countdown-$COUNTDOWN
cd $PKG
chown -R root.root .
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
find $PKG/usr/man -type f -exec gzip -9 {} \;
mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/slack-desc > $PKG/usr/doc/$NAME-$VERSION/slack-desc
cat $CWD/$NAME.SlackBuild > $PKG/usr/doc/$NAME-$VERSION/$NAME.SlackBuild
requiredbuilder -v -y -s $CWD $PKG
makepkg -l y -c n $CWD/$NAME-$VERSION-$ARCH-$BUILD.tgz

if [ "$1" = "--cleanup" ]; then
  rm -rf $TMP
fi
