#!/bin/sh
CWD=$(pwd)
if [ "$TMP" = "" ]; then
  TMP=/tmp
fi
PKG=$TMP/package-libtiff32

VERSION=3.8.2
ARCH=${ARCH:-x86_64}
BUILD=1

if [ "$ARCH" = "i386" ]; then
  SLKCFLAGS="-O2 -march=i386 -mcpu=i686"
elif [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
elif [ "$ARCH" = "s390" ]; then
  SLKCFLAGS="-O2"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
fi

rm -rf $PKG
mkdir -p $PKG
cd $TMP
rm -rf tiff-$VERSION
tar xjvf $CWD/tiff-$VERSION.tar.bz2
cd tiff-$VERSION

chown -R root:root .
find . -perm 777 -exec chmod 755 {} \;
find . -perm 664 -exec chmod 644 {} \;

zcat $CWD/tiff-3.8.2.goo-sec.diff.gz | patch -p1 --verbose || exit 1
zcat $CWD/tiff-3.8.2.tiffsplit.commandline.overflow.diff.gz | patch -p1 --verbose || exit 1
( 
. /etc/profile.d/32dev.sh
LDFLAGS="-L/usr/lib" \
CFLAGS="$SLKCFLAGS" \
./configure \
  --prefix=/usr \
	--libdir=/usr/lib \
  --program-prefix="" \
  --program-suffix="" \
  i686-slamd64-linux

make -j9 || exit 1
make install DESTDIR=$PKG || exit 1
)
rm -r $PKG/usr/share
( 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
	rm -rf usr/{local,share,man,include,bin}
)
strip -g $PKG/usr/lib/lib*.a
chmod 755 $PKG/usr/lib/libtiff*.so.*

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

cd $PKG
makepkg -l y -c n ../libtiff32-$VERSION-${ARCH}_slamd64-$BUILD.tgz