#!/bin/sh
CWD=`pwd`
TMP=${TMP:-/tmp}
PKG=$TMP/package-xfsprogs

VERSION=2.9.7-1
ARCH=${ARCH:-i486}
BUILD=1

if [ ! -d $TMP ]; then
  mkdir -p $TMP
fi
rm -rf $PKG
mkdir -p $PKG

cd $TMP
rm -rf xfsprogs-$(echo $VERSION | cut -f 1 -d '-')
tar xzvf $CWD/xfsprogs_$VERSION.tar.gz
cd xfsprogs-$(echo $VERSION | cut -f 1 -d '-')

chown -R root:root .
find . -perm 666 -exec chmod 644 {} \;
find . -perm 664 -exec chmod 644 {} \;
find . -perm 600 -exec chmod 644 {} \;
find . -perm 444 -exec chmod 644 {} \;
find . -perm 400 -exec chmod 644 {} \;
find . -perm 440 -exec chmod 644 {} \;
find . -perm 777 -exec chmod 755 {} \;
find . -perm 775 -exec chmod 755 {} \;
find . -perm 511 -exec chmod 755 {} \;
find . -perm 711 -exec chmod 755 {} \;
find . -perm 555 -exec chmod 755 {} \;

# Make sure you have the same version of autoconf as the
# developers did...  ;-)
autoconf

./configure \
  --prefix=/usr \
  --libdir=/lib \
  --libexecdir=/usr/lib \
  --sbindir=/sbin \
  --bindir=/usr/sbin \
  --enable-shared=yes \
  --enable-gettext=yes \
  --enable-shared-uuid=yes \
  --mandir=/usr/man \
  --datadir=/usr/share

make || exit 1
make install DIST_ROOT=$PKG
make install-dev DIST_ROOT=$PKG
make install-lib DIST_ROOT=$PKG

mv $PKG/usr/share/doc $PKG/usr
mv $PKG/usr/doc/xfsprogs $PKG/usr/doc/xfsprogs-$VERSION
# It would be nice to keep the same timestamps that the files have in the source:
cp -a \
  README doc/COPYING doc/CREDITS doc/PORTING \
  $PKG/usr/doc/xfsprogs-$VERSION

# Remove bogus files:
rm -f $PKG/lib/*.a $PKG/lib/*.so $PKG/lib/*.la $PKG/usr/lib/*.la
# Make /usr/lib/libhandle.so a symlink to /lib/libhandle.so.1:
if [ -f $PKG/lib/libhandle.so.1 ]; then
  ( cd $PKG/usr/lib ; rm -f libhandle.so ; ln -sf /lib/libhandle.so.1 libhandle.so )
else
  exit 1
  # and fix your script!  ;-)
fi
# Fix shared library perms:
chmod 755 $PKG/lib/*

# Strip libraries:
( 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
)
strip -g $PKG/usr/lib/*.a

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

cd $PKG
makepkg -l y -c n $TMP/xfsprogs-$(echo $VERSION | tr - _ )-$ARCH-$BUILD.tgz