#!/bin/sh
#
# Heavily based on the Slackware 12.1 SlackBuild
# http://www.ollisalonen.com/btnx
# Packager neongen ( neongen at libero.it)
# http://www.slacky.it

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

NAME=btnx-config
VERSION=0.4.9
ARCH=${ARCH:-i486}
CHOST=i486
BUILD=1ng
PKG=$TMP/package-$NAME

SOURCE=http://www.ollisalonen.com/btnx/$NAME-$VERSION.tar.gz

if [ "$ARCH" = "i386" ]; then
  SLKCFLAGS="-O2 -march=i386 -mcpu=i686"
elif [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
elif [ "$ARCH" = "s390" ]; 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


echo "+=============================================================================="
echo "| $NAME-$VERSION"
echo "+=============================================================================="


if [ ! -e $NAME-$VERSION.tar.gz ]; then
echo "Downloading source"
wget -c $SOURCE
fi

cd $TMP

echo "Unpacking $NAME..."
tar -xzf $CWD/$NAME-$VERSION.tar.gz

cd $NAME-$VERSION

find . -perm 777 -exec chmod 755 {} \;
find . -perm 775 -exec chmod 755 {} \;
find . -perm 555 -exec chmod 755 {} \;
find . -perm 666 -exec chmod 644 {} \;
find . -perm 664 -exec chmod 644 {} \;
find . -perm 444 -exec chmod 644 {} \;

chown -R root:root .

CFLAGS="$SLKCFLAGS" \
./configure  --prefix=/usr \
--build=$CHOST-slackware-linux 
#--docdir=/usr/doc/$NAME-$VERSION \


make
make install DESTDIR=$PKG

mkdir -p $PKG/usr/doc/$NAME-$VERSION
cp -a \
 AUTHORS COPYING COPYING-DOCS ChangeLog INSTALL NEWS README \
  $PKG/usr/doc/$NAME-$VERSION

chown root:root -R $PKG
chmod 644 $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

ln -s /usr/share/btnx-config/doc/btnx-manual $PKG/usr/doc/$NAME-$VERSION/btnx-manual

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/$NAME-$VERSION
  rm -rf $PKG
  rm -rf $TMP/$NAME-$VERSION.tar.gz*
fi
