#!/bin/bash
#
# author           : George Vlahavas (vlahavas~AT~gmail~DOT~com)
# project web page : http://www.accesspdf.com/
#
# This package was created with gcc, g++ and gcc-java 3.4.6.
# pdftk won't compile with gcc>=4.3.3 so I'm putting the required gcj
# lib inside the package and I'm hoping I won't ever need to update this
# again.

# Package name
NAME="pdftk"
# Package version
VERSION="1.41"

DOCS="pdftk.1.html pdftk.1.notes pdftk.1.txt"

CWD=`pwd`
PKG="$CWD/$NAME-install"
ARCH="i486"
CPU="i686"
BUILD="1gv"

rm -rf $PKG
mkdir -p $PKG

# Create package install directory
mkdir $PKG/install

# Create the slack-desc file
cat > $PKG/install/slack-desc << END
$NAME: pdftk is a CLI tool for doing everyday things with PDF documents.
$NAME:
$NAME: If PDF is electronic paper, then pdftk is an electronic
$NAME: staple-remover, hole-punch, binder, secret-decoder-ring, and
$NAME: X-Ray-glasses. Pdftk is a simple tool for doing everyday things with
$NAME: PDF documents.
$NAME:
$NAME:
$NAME:
$NAME:
$NAME:
END

# Unpack source
tar xf $NAME-$VERSION.tar.gz

# Patch the makefile for using the correct FLAGS
sed -i -e 's/-O2/-O2 -march=i486 -mtune=i686/' $CWD/$NAME-$VERSION/pdftk/Makefile.Generic

# make
cd $NAME-$VERSION/$NAME
make -f Makefile.Generic

# Install
mkdir -p $PKG/usr/bin
cp $CWD/pdftk $PKG/usr/bin
chmod 755 $PKG/usr/bin/pdftk
mkdir -p $PKG/usr/share/pdftk
cp pdftk $PKG/usr/share/pdftk/
cp /usr/lib/libgcj.so.5.0.0 $PKG/usr/share/pdftk
cd $CWD/$NAME-$VERSION
mkdir -p $PKG/usr/man/man1
cp $CWD/pdftk.1.gz $PKG/usr/man/man1/

# Strip binaries
cd $PKG
find . | xargs file | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null

# Copy Docs
mkdir -p $PKG/usr/doc/$NAME-$VERSION
cd $CWD/$NAME-$VERSION
cp -a $DOCS $PKG/usr/doc/$NAME-$VERSION/

# Make sure ownerships and permissions are sane
cd $PKG
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 {} \;

# Make the package
/sbin/makepkg -l y -c n $CWD/$NAME-$VERSION-$ARCH-$BUILD.txz

# Calculate md5sum
cd $CWD/
md5sum $NAME-$VERSION-$ARCH-$BUILD.txz > $NAME-$VERSION-$ARCH-$BUILD.md5

# Remove source-code and temporary install directories
# since they are not needed anymore
rm -rf $CWD/$NAME-$VERSION
rm -rf $PKG
