#!/bin/sh
# Heavily based on the Slackware 11.0 SlackBuild 
# http://www.gnu.org/software/units
# http://www.itweb.it/linux/units.php?lng=it
# Packager baldelario ~at~ gmail ~dot~ com
# http://www.slacky.it

CWD=`pwd`
TMP=${TMP:-/tmp/tgz}
PKG=$TMP/package-units
rm -rf $PKG
mkdir -p $PKG

NAME=units
VERSION=1.86
CHOST=i486
ARCH=${ARCH:-i486}
BUILD=1bj

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

cd $TMP
rm -rf $NAME-$VERSION
tar xzvf $CWD/$NAME-$VERSION.tar.gz
cd $NAME-$VERSION
chown -R root:root .
CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure --prefix=/usr \
--datadir=/usr/share/units \
$CHOST-slackware-linux
make
make install DESTDIR=$PKG
strip $PKG/usr/bin/*
find $PKG/usr/{man,info} -type f -exec gzip -9 {} \;

(cd $PKG/usr/share/units ; tar xzvf $CWD/it_units.dat.tar.gz
chown root.root *
)

cat << EOF >> $PKG/usr/share/units/units.dat
!include /usr/share/units/it_units.dat
!include /usr/share/units/old_it_units.dat
EOF

mkdir -p $PKG/usr/doc/$NAME-$VERSION
cp -a INSTALL COPYING ChangeLog NEWS README* \
  $PKG/usr/doc/$NAME-$VERSION
mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/slack-desc > $PKG/usr/doc/$NAME-$VERSION/slack-desc
cat $CWD/$NAME.SlackBuild > $PKG/usr/doc/$NAME-$VERSION/$NAME.SlackBuild
cd $PKG
requiredbuilder -v -y -s $CWD $PKG
makepkg -l y -c n $CWD/$NAME-$VERSION-$ARCH-$BUILD.tgz 

if [ "$1" = "--cleanup" ]; then 
  rm -rf $TMP
fi
