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

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

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

if [ $DISTRO = slamd64 ]; then
	LIBSUFFIX=64
else
	LIBSUFFIX=
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

if [ "x$LIBSUFFIX" = "x64" ]; then
	# Multilib fixes from http://cblfs.cross-lfs.org/index.php/Gtk2
	zcat $CWD/gtk+-2.10.9-multilib-1.diff.gz | patch -p1 --verbose || exit 1
	sed -i "s/BUILDENV/64/" gdk-pixbuf/gdk-pixbuf-io.c \
    gdk-pixbuf/Makefile.am gdk-pixbuf/Makefile.in \
    modules/input/Makefile.am modules/input/Makefile.in \
    gtk/gtkrc.c
elif [ "x$LIBSUFFIX" != "x" ]; then
	echo "Unsupported libsuffix!"
	exit 1
fi

# 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 {} \;

CFLAGS="$SLKCFLAGS" \
./configure \
  --prefix=/usr \
	--libdir=/usr/lib$LIBSUFFIX \
  --sysconfdir=/etc \
  --mandir=/usr/man \
  --with-xinput=yes

make -j9 || exit 1
make install DESTDIR=$PKG || exit 1

mkdir -p $PKG/var/log/setup
cat << EOF > $PKG/var/log/setup/setup.07.gtk-update-icon-cache
chroot . /usr/bin/gtk-update-icon-cache -f -t /usr/share/icons/hicolor 1> /dev/null 2> /dev/null
EOF
chmod 755 $PKG/var/log/setup/setup.07.gtk-update-icon-cache

# I'm spamming your root partition because /etc/gtk-2.0 won't install to $DESTDIR
make install || exit 1
if [ "x$LIBSUFFIX" = "x64" ]; then
	EXTRAETC=-64
else
	EXTRAETC=
fi
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
)

# 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
cat << EOF > $PKG/install/doinst.sh
#!/bin/sh
if [ -x /usr/bin/gtk-update-icon-cache ]; then
  /usr/bin/gtk-update-icon-cache -f -t /usr/share/icons/hicolor 1> /dev/null 2> /dev/null
fi
EOF

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