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

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

rm -rf $PKG
mkdir -p $PKG
cd $TMP
rm -rf sysfsutils-$VERSION
tar xjvf $CWD/sysfsutils-$VERSION.tar.bz2
cd sysfsutils-$VERSION
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 {} \;

./configure \
  --prefix=/usr \
  --libdir=/lib \
  --enable-static=no \
  $ARCH-slackware-linux
make
make install DESTDIR=$PKG

# Unless things start whining later that this libtool junk is
# required for some reason, out it goes.  Especially in /lib,
# where probably nothing would find it anyway.
rm -f $PKG/lib/*.la

# .so links should really go in /usr/lib:
rm $PKG/lib/*.so
mkdir -p $PKG/usr/lib
# sanity check
if [ ! -L $PKG/lib/libsysfs.so.2 ]; then
  exit 1
fi
( cd $PKG/usr/lib ; ln -sf /lib/libsysfs.so.2 libsysfs.so )

( 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
)

mkdir -p $PKG/usr/doc/sysfsutils-$VERSION
cp -a \
  AUTHORS COPYING CREDITS NEWS README TODO \
  docs/* \
  $PKG/usr/doc/sysfsutils-$VERSION

# Compress and/or relink manpages:
if [ -d $PKG/usr/man ]; then
  ( cd $PKG/usr/man
    for manpagedir in $(find . -type d -name "man*") ; do
      ( cd $manpagedir
        for eachpage in $( find . -type l -maxdepth 1) ; do
          ln -s $( readlink $eachpage ).gz $eachpage.gz
          rm $eachpage
        done
        gzip -9 *.?
      )
    done
  )
fi

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

cd $PKG
makepkg -l y -c n ../sysfsutils-$VERSION-$ARCH-$BUILD.tgz