#!/bin/sh
# Heavily based on the Slackware 12.1 SlackBuild
# http://pdfedit.petricek.net
# Packager Andrea Sciucca ( gohanz at infinito.it)
# http://www.slacky.it
#
# Required: Boost

CWD=`pwd`
TMP=${TMP:-/tmp/tgz}
PKG=$TMP/package/pdfedit
NAME=pdfedit
VERSION=0.4.1
CHOST=i486
ARCH=${ARCH:-i686}
BUILD=2as

SOURCE=http://ovh.dl.sourceforge.net/sourceforge/pdfedit/$NAME-$VERSION.tar.bz2

if [ ! -e $NAME-$VERSION.tar.bz2 ]; then
wget -c $SOURCE
fi

if [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O3 -march=i686 -pipe -fomit-frame-pointer"
elif [ "$ARCH" = "athlon64" ]; then
  SLKCFLAGS="-O2 -march=athlon64 -pipe"
elif [ "$ARCH" = "athlonxp" ]; then
  SLKCFLAGS="-O3 -march=athlon-xp -pipe -fomit-frame-pointer"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2"
elif [ "$ARCH" = "s390" ]; then
  SLKCFLAGS="-O2"
fi

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

cd $TMP
tar xvjf $CWD/$NAME-$VERSION.tar.bz2

echo -e "\E[0;32m+--------------------------------+\E[0;0m"
echo -e "\E[0;32m| Start SlackBuild $NAME-$VERSION |\E[0;0m"
echo -e "\E[0;32m+--------------------------------+\E[0;0m"

cd $NAME-$VERSION

find . -perm 777 -exec chmod 755 {} \;
find . -perm 775 -exec chmod 755 {} \;
find . -perm 555 -exec chmod 755 {} \;
find . -perm 666 -exec chmod 644 {} \;
find . -perm 664 -exec chmod 644 {} \;
find . -perm 444 -exec chmod 644 {} \;

chown -R root:root .

export QTDIR=/usr/lib/qt
export QMAKESPEC=${QTDIR}/mkspecs/linux-g++
CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure  --prefix=/usr \
--mandir=/usr/man \
--disable-debug \
--program-prefix= \
--program-suffix= \
--build=$CHOST-slackware-linux
make
make install

mkdir -p $PKG/usr/bin
mkdir -p $PKG/usr/share
mkdir -p $PKG/usr/man/man1

cp -a /usr/bin/pdfedit $PKG/usr/bin
rm /usr/bin/pdfedit

cp -a -r /usr/share/pdfedit $PKG/usr/share
rm -r    /usr/share/pdfedit

cp -a /usr/man/man1/pdfedit.1 $PKG/usr/man/man1
rm    /usr/man/man1/pdfedit.1

mkdir -p $PKG/usr/doc/$NAME-$VERSION
cp -a Changelog README $PKG/usr/doc/$NAME-$VERSION

cp -a -r /usr/doc/pdfedit/* $PKG/usr/doc/$NAME-$VERSION
rm -r    /usr/doc/pdfedit

( 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
)

mkdir -p  $PKG/usr/share/applications

cat << EOF > $PKG/usr/share/applications/$NAME.desktop
[Desktop Entry]
Type=Application
Encoding=UTF-8
Name=PDFedit
GenericName=Editor PDF 
Comment=Editor for manipulating PDF documents
Icon=pdfedit
TryExec=pdfedit
Exec=pdfedit
Terminal=false
Categories=Qt;Application;Graphics
EOF

mkdir -p  $PKG/usr/share/icons/default.kde/16x16/apps
mkdir -p  $PKG/usr/share/icons/default.kde/32x32/apps
mkdir -p  $PKG/usr/share/icons/default.kde/48x48/apps
mkdir -p  $PKG/usr/share/icons/default.kde/64x64/apps

( cd src/gui/icon
  cp -a pdfedit_icon_64.png $PKG/usr/share/icons/default.kde/64x64/apps/pdfedit.png
  cp -a pdfedit_icon_16.png $PKG/usr/share/icons/default.kde/16x16/apps/pdfedit.png
  cp -a pdfedit_icon_32.png $PKG/usr/share/icons/default.kde/32x32/apps/pdfedit.png
  cp -a pdfedit_icon_48.png $PKG/usr/share/icons/default.kde/48x48/apps/pdfedit.png
)

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

gzip -9 $PKG/usr/man/*/*

( cd $PKG/usr/share/pdfedit
  find . -perm 600 -exec chmod 644 {} \;
)

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