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

VERSION=2.10.13
ARCH=${ARCH:-x86_64}
BUILD=${BUILD:-2}
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 gtk+-$VERSION
tar xjvf $CWD/gtk+-$VERSION.tar.bz2
cd gtk+-$VERSION

# There's been this long standing debate about PNG and -lz
# and this patch is the workaround.  ;-)
zcat $CWD/gtk.pnglz.diff.gz | patch -p1 --verbose || exit 1

chown -R root:root .
find . -perm 666 -exec chmod 644 {} \;
find . -perm 664 -exec chmod 644 {} \;
find . -perm 600 -exec chmod 644 {} \;
find . -perm 444 -exec chmod 644 {} \;
find . -perm 400 -exec chmod 644 {} \;
find . -perm 440 -exec chmod 644 {} \;
find . -perm 777 -exec chmod 755 {} \;
find . -perm 775 -exec chmod 755 {} \;
find . -perm 511 -exec chmod 755 {} \;
find . -perm 711 -exec chmod 755 {} \;
find . -perm 555 -exec chmod 755 {} \;
(
LDFLAGS="-L/usr/lib" \
CFLAGS="$SLKCFLAGS -m32" \
linux32 ./configure \
  --prefix=/usr \
	--libdir=/usr/lib$LIBSUFFIX \
  --sysconfdir=/etc \
  --mandir=/usr/man \
  --with-xinput=yes \
	i686-slamd64-linux

make -j 10 || exit 1
make install DESTDIR=$PKG || exit 1
)


# I'm spamming your root partition because /etc/gtk-2.0 won't install to $DESTDIR
make install || exit 1
	EXTRAETC=
mkdir -p $PKG/etc/gtk-2.0$EXTRAETC
cp -a /etc/gtk-2.0$EXTRAETC/* $PKG/etc/gtk-2.0$EXTRAETC

( 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,include,man}
	mkdir usr/bin/32
	mv usr/bin/* usr/bin/32/
)

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

cd $PKG
install -o root -g root -m 644 -D $CWD/slack-desc install/slack-desc
makepkg -l y -c n $TMP/gtk+2-32-$VERSION-$PKGARCH-$BUILD.tgz
