#!/bin/sh
# Heavily based on the Slackware 12.0 SlackBuild 
# Packager baldelario ~at~ gmail ~dot~ com
# http://www.slacky.it
# http://tukaani.org/lzma/ - http://7-zip.org/sdk.html

CWD=`pwd`
TMP=${TMP:-/tmp/tgz}
PKG=$TMP/package-lzma
rm -rf $PKG
mkdir -p $PKG

NAME=lzma
VERSION=4.32.5
ARCH=${ARCH:-i486}
BUILD=1bj

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

SOURCE=http://tukaani.org/$NAME/$NAME-$VERSION.tar.gz

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

cd $TMP
rm -rf $NAME-$VERSION
tar xzf $CWD/$NAME-$VERSION.tar.gz || exit 1
cd $NAME-$VERSION
chown -R root:root .
CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure \
 --prefix=/usr \
 --disable-static \
 --mandir=/usr/man \
 --program-prefix= \
 --program-suffix= \
 --build=$ARCH-slackware-linux
make || exit 1
make install DESTDIR=$PKG || exit 1

mkdir -p $PKG/usr/doc/$NAME-$VERSION
cp -a AUTHORS ChangeLog COPYING* INSTALL NEWS README THANKS \
$PKG/usr/doc/$NAME-$VERSION

# Fix manpage symlinks:
if [ -d $PKG/usr/man ]; then
  ( cd $PKG/usr/man
       for i in `find . -type l` ; do 
         ln -s $( readlink $i ).gz $i.gz
         rm $i
    done
    gzip -9 */*.?
   )
fi

cd $PKG
find . | xargs file | grep "executable" | grep ELF | cut -d : -f 1 | xargs -r strip --strip-unneeded
find . | xargs file | grep "shared object" | grep ELF | cut -d : -f 1 | xargs -r strip --strip-unneeded
find . | xargs file | grep "current ar archive" | cut -f 1 -d : | xargs -r strip --strip-debug

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
requiredbuilder -v -y -s $CWD $PKG
makepkg -l y -c n $CWD/$NAME-$VERSION-$ARCH-$BUILD.tgz

if [ "$1" = "--cleanup" ]; then
 rm -rf $TMP/$NAME-$VERSION
 rm -rf $PKG
fi
