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

VERSION=2.12.12
ARCH=${ARCH:-i486}
BUILD=${BUILD:-1}

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

cd $TMP
rm -rf gtk+-$VERSION
tar xjvf $CWD/gtk+-$VERSION.tar.bz2 || exit 1
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
# Fix Flash in Konquoror:
zcat $CWD/gtk.nsplugin.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 {} \;

CFLAGS="$SLKCFLAGS" \
./configure \
  --prefix=/usr \
  --sysconfdir=/etc \
  --mandir=/usr/man \
  --with-xinput=yes \
  --build=$ARCH-slackware-linux

make -j4 || 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
mkdir -p $PKG/etc/gtk-2.0
cp -a /etc/gtk-2.0/* $PKG/etc/gtk-2.0
mv $PKG/etc/gtk-2.0/im-multipress.conf $PKG/etc/gtk-2.0/im-multipress.conf.new || 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
)

# Compress and if needed symlink the man pages:
if [ -d $PKG/usr/man ]; then
  ( cd $PKG/usr/man
    for manpagedir in $(find . -type d -name "man*") ; do
      ( cd $manpagedir
        for eachpage in $( find . -type l -maxdepth 1) ; do
          ln -s $( readlink $eachpage ).gz $eachpage.gz
          rm $eachpage
        done
        gzip -9 *.?
      )
    done
  )
fi

mkdir -p $PKG/usr/doc/gtk+-$VERSION
cp -a \
  AUTHORS COPYING NEWS README TODO \
  $PKG/usr/doc/gtk+-$VERSION

# I'm almost inclined to think this stuff is obsolete, but I don't know for sure.
# I do know that leaving it in doesn't hurt (aside from a few wasted bytes, but
# probably less than the comments here waste ;-)
mkdir -p $PKG/etc/profile.d
cp -a $CWD/profile.d/* $PKG/etc/profile.d
chmod 755 $PKG/etc/profile.d/*

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
zcat $CWD/doinst.sh.gz > $PKG/install/doinst.sh

cd $PKG
makepkg -l y -c n $TMP/gtk+2-$VERSION-$ARCH-$BUILD.tgz