#!/bin/sh
# Set initial variables:
CWD=`pwd`
if [ "$TMP" = "" ]; then
  TMP=/tmp
fi
PKG=$TMP/package-hdparm

VERSION=${VERSION:-9.3}
ARCH=${ARCH:-i486}
BUILD=${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 hdparm-$VERSION
tar xvf $CWD/hdparm-$VERSION.tar.bz2 || exit 1
cd hdparm-$VERSION
chown -R root:root .
make -j4 || exit 1
mkdir -p $PKG/sbin
cat hdparm > $PKG/sbin/hdparm
chmod 755 $PKG/sbin/hdparm
mkdir -p $PKG/usr/sbin
( cd $PKG/usr/sbin
  ln -sf ../../sbin/hdparm .
)
mkdir -p $PKG/usr/man/man8
cat hdparm.8 | gzip -9c > $PKG/usr/man/man8/hdparm.8.gz
mkdir -p $PKG/usr/doc/hdparm-$VERSION
cp -a \
  LICENSE.TXT README.acoustic hdparm.lsm contrib \
  $PKG/usr/doc/hdparm-$VERSION
# I'm also not sure why I'm putting a binary in the docs, but...
rm -f $PKG/usr/doc/hdparm-$VERSION/contrib/fix_standby.c

( 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
cat $CWD/slack-desc > $PKG/install/slack-desc

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

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