#!/bin/sh

[ -z $PREPKG ] && exit 1

PACKAGE=xz
VERSION=5.2.1
FILES=$(cd $(dirname $0) ; pwd)

mkdir -p \
  $PREPKG/{install,bin,usr/lib}/

tar xf $FILES/$PACKAGE-$VERSION.tar.xz || exit 1

cd $PACKAGE-$VERSION/
CFLAGS="-O2 -march=i486 -mtune=i686" \
  ./configure \
  --prefix=/usr \
  --docdir=/usr/doc/$PACKAGE-$VERSION \
  --libdir=/lib \
  --mandir=/usr/man \
  --disable-static \
  --disable-rpath \
  --enable-shared \
  --build=i486-slackware-linux &> configure.output || exit 1
make $MAKEJOBS &> make.output || exit 1
make DESTDIR=$PREPKG install &> make_install.output || exit 1

# move main binary to /bin/ so it's available for pkgtools
mv \
  $PREPKG/usr/bin/$PACKAGE \
  $PREPKG/bin/
ln -s \
  ../../bin/$PACKAGE \
  $PREPKG/usr/bin/$PACKAGE

# put a few things where they really belong
mv \
  $PREPKG/lib/{pkgconfig/,*.la} \
  $PREPKG/usr/lib/

cp -a \
  doc/[a-l,o-z]* ABOUT-NLS AUTHORS COPYING* \
  ChangeLog NEWS README* THANKS TODO \
  $PREPKG/usr/doc/$PACKAGE-$VERSION

for MANPAGE in $(find $PREPKG/usr/man/ ! -type d -name '*.?') ; do
  if [ -L $MANPAGE ] ; then
    ln -sf $(readlink $MANPAGE).gz $MANPAGE.gz
    rm $MANPAGE
  else
    gzip -9 $MANPAGE
  fi
done

cp \
  $FILES/slack-desc \
  $PREPKG/install/

find $PREPKG -exec file {} \; \
  | awk -F : '/ ELF / {print $1}' \
  | xargs strip -p --strip-unneeded
