#!/bin/sh
# Heavily based on the Slackware 12.0 SlackBuild
# http://www.guzu.net/linux/hddtemp.php
# http://www.slacky.it/
# Packager Salbutamolo ( alessandro_ranieri@virgilio.it )

CWD=`pwd`
if ["$TMP" = ""]; then
 TMP=/tmp
fi

PKG=$TMP/package-hddtemp
NAME=hddtemp
VERSION=0.3-beta15
CHOST=i486
ARCH=${ARCH:-i486}
BUILD=1sal

if [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 "
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2"
fi

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

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

cd $TMP
tar xf $CWD/$NAME-$VERSION.tar.bz2
cd $NAME-$VERSION
patch -p1 < $CWD/hddtemp-0.3_beta15-satacmds.patch
chown -R root:root .
CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure \
    --prefix=/usr/ \
    --build=$CHOST-slackware-linux
make
make install DESTDIR=$PKG
mkdir -p $PKG/usr/share/misc
cp $CWD/hddtemp.db $PKG/usr/share/misc
mkdir -p $PKG/usr/doc/$NAME-$VERSION
cp -a ChangeLog COPYING INSTALL README TODO GPL-2 contribs \
$PKG/usr/doc/$NAME-$VERSION

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

gzip $PKG/usr/man/man8/*
mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cd $CWD
cp -a $NAME.SlackBuild slack-desc $PKG/usr/doc/$NAME-$VERSION
chown -R root:root $PKG/usr/doc/$NAME-$VERSION
cd $PKG
requiredbuilder -v -y -s $CWD $PKG
makepkg -l y -c n $CWD/$NAME-0.3.beta15-$ARCH-$BUILD.tgz

if [ "$1" = "--cleanup" ]; then
  rm -rf $TMP/$NAME-$VERSION
  rm -rf $PKG
fi
