#!/bin/sh
# Heavily based on the Slackware 12.2 SlackBuild
# http://www.scribus.org.uk/index.php

CWD=`pwd`
TMP=${TMP:-/tmp/tgz}
PKG=$TMP/package-scribus
NAME=scribus
VERSION=1.3.3.13
ARCH=i686
BUILD=1ms

if [ ! -f scribus-1.3.3.13.tar.bz2 ];
then
	wget -c "http://downloads.sourceforge.net/scribus/scribus-1.3.3.13.tar.bz2?modtime=1200098773&big_mirror=0"
fi

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 {} \;
cmake . -DCMAKE_INSTALL_PREFIX:PATH=/usr
make
make install DESTDIR=$PKG
mv $PKG/usr/share/man $PKG/usr
mv $PKG/usr/share/doc $PKG/usr
mv $PKG/usr/doc/$NAME $PKG/usr/doc/$NAME-$VERSION

mkdir -p $PKG/usr/share/applications
cat $CWD/scribus.desktop > $PKG/usr/share/applications/scribus.desktop

( cd $PKG
  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
)

( cd $PKG/usr/man
  find . -name "*.?" | xargs gzip -9
)

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cd $CWD
cp -a *.SlackBuild slack-desc $PKG/usr/doc/$NAME-$VERSION
cd $PKG
chown -R root:root .
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
