#!/bin/bash

# libtiff.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter <stuart@armedslack.org>
# 23-Jul-2004

# Record toolchain & other info for the build log:
slackbuildinfo

# Paths to skeleton port's source & real Slackware source tree:
export CWD=$SLACKSOURCE/$PKGSERIES/$PACKAGE
export PORTCWD=$PWD

# Temporary build locations:
export TMPBUILD=$TMP/build-$PACKAGE
export PKG=$TMP/package-$PACKAGE
mkpkgdirs # Delete & re-create temporary directories then cd into $TMPBUILD

# Determine the CFLAGS for the known architectures:
case $PORTARCH in
   arm)     export SLKCFLAGS="-O2 -march=armv4 -mtune=xscale" ;;
esac

# Extract source:
tar xvvf $CWD/tiff-$VERSION.tar.bz2
cd tiff-* || exit 1
slackhousekeeping

# Apply patches:
zcat $CWD/tiffsplit-overflow.patch.gz | patch -p1 --verbose || exit 1
zcat $CWD/libtiff-3.8.2-ormandy.patch.gz | patch -p1 --verbose || exit 1
zcat $CWD/libtiff-3.8.2-CVE-2006-2193.patch.gz | patch -p1 --verbose || exit 1
zcat $CWD/libtiff-3.8.2-mantypo.patch.gz | patch -p1 --verbose || exit 1
zcat $CWD/libtiff-3.8.2-lzw-bugs.patch.gz | patch -p1 --verbose || exit 1
zcat $CWD/libtiff-3.8.2-CVE-2009-2347.patch.gz | patch -p1 --verbose || exit 1
zcat $CWD/libtiff-jpeg-scanline.patch.gz | patch -p1 --verbose || exit 1
zcat $CWD/libtiff-ycbcr-clamp.patch.gz | patch -p1 --verbose || exit 1
zcat $CWD/libtiff-CVE-2010-1411.patch.gz | patch -p1 --verbose || exit 1
zcat $CWD/libtiff-CVE-2010-2065.patch.gz | patch -p1 --verbose || exit 1
zcat $CWD/libtiff-tiffcp-no-subsample.patch.gz | patch -p1 --verbose || exit 1
zcat $CWD/libtiff-tiff2pdf-bugs.patch.gz | patch -p1 --verbose || exit 1
zcat $CWD/libtiff-unknown-tags.patch.gz | patch -p1 --verbose || exit 1
zcat $CWD/libtiff-3samples.patch.gz | patch -p1 --verbose || exit 1
zcat $CWD/libtiff-subsampling.patch.gz | patch -p1 --verbose || exit 1
zcat $CWD/libtiff-tiffdump.patch.gz | patch -p1 --verbose || exit 1

# Configure:
CFLAGS="$SLKCFLAGS" \
./configure \
   --prefix=/usr \
   --program-prefix="" \
   --program-suffix="" \
   $ARCH-slackware-linux || failconfig

# Build:
make -j2 || failmake

# Install into package:
make install DESTDIR=$PKG || failinstall
chmod 755 $PKG/usr/lib/libtiff.so.*

# This is junk:
rm -rf $PKG/usr/share

# Install docs:
mkdir -p $PKG/usr/doc/$PACKAGE-$VERSION
cp -favv COPYRIGHT README RELEASE-DATE TODO VERSION \
         $PKG/usr/doc/$PACKAGE-$VERSION

# Move man pages:
mv $PKG/usr/local/man $PKG/usr
rm -rf $PKG/usr/local

# If necessary, start the fakeroot server so we can set file/dir ownerships:
start_fakeroot

# Apply generic Slackware packaging policies:
cd $PKG
slackstripall   # strip all .a archives and all ELFs
slackgzpages -i # compress man & info pages and delete usr/info/dir
slackslack      # chown -R root:root, chmod -R og-w, slackchown, slack644docs
slackdesc       # install slack-desc and doinst.sh
slackmp         # run makepkg

# Perform any final checks on the package:
cd $PKG
slackhlinks     # search for any hard links	
