#!/bin/bash
#
#  pdftk Slackbuild Script
#  Copyright (C) 2006-2008  C. J. Meidlinger <cj@meidlinger.org>
#
#  At a minimum, you need to edit some vars and the slack-desc
#  information in the "build package" section.  Other tweaks
#  may be necessary depending on the source code and its requirements.
#
############################
#                          #
#  Vars                    #
#                          #
################################################################################

SOURCE_NAME=pdftk
SOURCE_VERSION=1.41
PACKAGE_VERSION=1
build_dir=`pwd`
PKG_DIR=$build_dir/pkg
PKG_BUILDER="C. J. Meidlinger <cj@meidlinger.org>"
PKG_BUILDER_INITIALS=cjm
PATCHES=

############################
#                          #
#  unpack source           #
#                          #
################################################################################

tar -xzvf ${SOURCE_NAME}-${SOURCE_VERSION}.tar.gz
for t in $PATCHES; do 
     ( cd ${SOURCE_NAME}-*/ && patch -p1 < ../$t )
done

############################
#                          #
#  configure and compile   #
#                          #
################################################################################

( cd ${SOURCE_NAME}-*/pdftk \
     &&  sed s/CPPFLAGS=\ -O2/CPPFLAGS=\ -O2\ -march=i486\ -mtune=i686/g Makefile.Generic > Makefile.Slackware \
     &&  make -f Makefile.Slackware \
     &&  chown -R root.root . \
     &&  find . -perm 777 -exec chmod 755 {} \; \
     &&  find . -perm 555 -exec chmod 755 {} \; \
     &&  find . -perm 444 -exec chmod 644 {} \; \
     &&  find . -perm 666 -exec chmod 644 {} \; \
     &&  find . -perm 664 -exec chmod 644 {} \; )


mkdir -p $PKG_DIR/usr/bin
cp ${SOURCE_NAME}-*/pdftk/pdftk $PKG_DIR/usr/bin

############################
#                          #
#  build package           #
#                          #
################################################################################

#
#  slack-desc
#
mkdir -p $PKG_DIR/install
cat > $PKG_DIR/install/slack-desc << EOF
# HOW TO EDIT THIS FILE:
# The "handy ruler" below makes it easier to edit a package description. Line
# up the first '|' above the ':' following the base package name, and the '|'
# on the right side marks the last column you can put a character in. You must
# make exactly 11 lines for the formatting to be correct. It's also
# customary to leave one space after the ':'.

            |-----handy-ruler------------------------------------------------------|
$SOURCE_NAME: $SOURCE_NAME $SOURCE_VERSION (PDF toolkit)
$SOURCE_NAME:
$SOURCE_NAME: If PDF is electronic paper, then pdftk is an electronic
$SOURCE_NAME: staple-remover, hole-punch, binder, secret-decoder-ring, and
$SOURCE_NAME: X-Ray-glasses. Pdftk is a command-line tool for doing everyday things
$SOURCE_NAME: with PDF documents. Keep one in the top drawer of your desktop.
$SOURCE_NAME:
$SOURCE_NAME:
$SOURCE_NAME: Package created by $PKG_BUILDER
$SOURCE_NAME:
$SOURCE_NAME: 
EOF

#
#  slack-required
#
#cat > $PKG_DIR/install/slack-required << EOF
#
#EOF


#
#  documentation
#
mkdir -p $PKG_DIR/usr/doc/${SOURCE_NAME}-${SOURCE_VERSION}
#
cp ${build_dir}/${SOURCE_NAME}*/pdftk.1* $PKG_DIR/usr/doc/${SOURCE_NAME}-${SOURCE_VERSION}

#  man page
mkdir -p $PKG_DIR/usr/man/man1
cp ${build_dir}/${SOURCE_NAME}*/debian/pdftk.1 $PKG_DIR/usr/man/man1

#  license?
cat > $PKG_DIR/usr/doc/${SOURCE_NAME}-${SOURCE_VERSION}/COPYING << EOF
The exact text on the website is:

    Pdftk is free software (GPL).

Unfortunately, I cannot find a copy of the license in the source
tarball to include in this package.

CJM
EOF

#
# SlackBuild
#
mkdir -p $PKG_DIR/usr/src/slackbuilds/${SOURCE_NAME}-${SOURCE_VERSION}
cp ${SOURCE_NAME}.SlackBuild $PKG_DIR/usr/src/slackbuilds/${SOURCE_NAME}-${SOURCE_VERSION}
cp $PATCHES $PKG_DIR/usr/src/slackbuilds/${SOURCE_NAME}-${SOURCE_VERSION}
chmod 755 $PKG_DIR/usr/src/slackbuilds/${SOURCE_NAME}-${SOURCE_VERSION}/${SOURCE_NAME}.SlackBuild

#
#  gzip man pages
#
for t in `find $PKG_DIR/usr/man/ -type f` ; do
     gzip $t
done


#
#  strip binaries
#
(cd $PKG_DIR ; find . | xargs file | grep "current ar archive" | cut -f 1 -d : | xargs strip --strip-debug )
(cd $PKG_DIR ; find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded )
(cd $PKG_DIR ; find . | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded )

#
#  package
#
( cd $PKG_DIR && chown root:root . && chmod 755 . && makepkg -l y -c n ../${SOURCE_NAME}-${SOURCE_VERSION}-i486-${PACKAGE_VERSION}${PKG_BUILDER_INITIALS}.tgz )
