#!/bin/sh
CWD=`pwd`
if [ "$TMP" = "" ]; then
  TMP=/tmp
fi
PKG=$TMP/package-atk32
rm -rf $PKG
mkdir -p $PKG/usr

VERSION=1.18.0
ARCH=${ARCH:-x86_64}
BUILD=1
DISTRO=${DISTRO:-slamd64}

if [ $DISTRO = slackware ]; then
	PKGARCH=$ARCH
else
	PKGARCH=${ARCH}_${DISTRO}
fi

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 -fPIC"
fi

cd $TMP
rm -rf atk-$VERSION
tar xjvf $CWD/atk-$VERSION.tar.bz2
cd atk-$VERSION
chown -R root:root .
find . -perm 664 -exec chmod 644 {} \;
(
. /etc/profile.d/32dev.sh
CFLAGS="$SLKCFLAGS" \
./configure --prefix=/usr \
						--libdir=/usr/lib \
            --sysconfdir=/etc
make -j9 || exit 1
make install DESTDIR=$PKG || exit 1
)
( 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
	rm -rf usr/{share,doc,include}
)
mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

cd $PKG
makepkg -l y -c n ../atk32-$VERSION-$PKGARCH-$BUILD.tgz
