#!/bin/sh

VERSION=2.4z
# See also version number 2.4 in the symlinks below...)
ARCH=${ARCH:-i486}
BUILD=${BUILD:-1tukaani}

set -e
PKG=/tmp/package-blt
rm -rf "$PKG"

CWD=`pwd`

if [ "$ARCH" = "i386" ]; then
  SLKCFLAGS="-O2 -march=i386 -mtune=athlon-xp"
elif [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=athlon-xp"
elif [ "$ARCH" = "s390" ]; then
  SLKCFLAGS="-O2"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2"
fi

cd /tmp
rm -rf "blt$VERSION"
tar xzvf $CWD/blt$VERSION.tar.gz
cd blt$VERSION
zcat $CWD/blt2.4z-patch-2.gz | patch -p1
CFLAGS="$SLKCFLAGS" \
./configure --prefix=/usr
make
# Got tired to play with INSTALL_ROOT & fellows:
fakedestdir "$PKG" install

( cd $PKG/usr/bin
  rm -f bltsh24 bltwish24
  ln -s bltsh bltsh24
  ln -s bltwish bltwish24
)

( cd $PKG/usr/lib
  strip -g *.so *.a
)
strip $PKG/usr/bin/*

gzip -9 $PKG/usr/man/*/*
( cd $PKG/usr/man/mann
  rm -f hierbox.n.gz hiertable.n.gz
  ln -s treeview.n.gz hierbox.n.gz
  ln -s treeview.n.gz hiertable.n.gz
)

sed -i '1{s|\.\./src/bltwish|/usr/bin/bltwish|}' $PKG/usr/lib/blt2.4/demos/*.tcl

mkdir -p $PKG/usr/doc/blt$VERSION
cp -a INSTALL MANIFEST NEWS PROBLEMS README html $PKG/usr/doc/blt$VERSION
rm -f $PKG/usr/doc/blt$VERSION/html/Makefile.vc

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

cd "$PKG"
makepkg -c y -l y ../blt-$VERSION-$ARCH-$BUILD.tlz
