#!/bin/sh
CWD=`pwd`
TMP=/tmp
PKG=/tmp/package-smartmontools

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

if [ ! -d $TMP ]; then
  mkdir -p $TMP # location to build the source
fi
rm -rf $PKG
mkdir -p $PKG

cd $TMP
rm -rf smartmontools-$VERSION
tar xzvf $CWD/smartmontools-$VERSION.tar.gz
cd smartmontools-$VERSION
chown -R root:root .
./configure \
  --prefix=/usr \
  --sysconfdir=/etc \
  $ARCH-slackware-linux
make
make install DESTDIR=$PKG
# Not used on Slackware:
rm -rf $PKG/etc/rc.d
mv $PKG/etc/smartd.conf $PKG/etc/smartd.conf.new
gzip -9 $PKG/usr/man/man?/*.?
mv $PKG/usr/share/doc $PKG/usr
rmdir $PKG/usr/share
rm -f $PKG/usr/doc/smartmontools-$VERSION/CHANGELOG \
      $PKG/usr/doc/smartmontools-$VERSION/smartd.conf
( 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/install
zcat $CWD/doinst.sh.gz > $PKG/install/doinst.sh
cat $CWD/slack-desc > $PKG/install/slack-desc

# Build the package:
cd $PKG
makepkg -l y -c n $TMP/smartmontools-$VERSION-$ARCH-$BUILD.tgz

# Clean up the extra stuff:
if [ "$1" = "--cleanup" ]; then
  rm -rf $TMP/smartmontools-$VERSION
  rm -rf $PKG
fi