#!/bin/bash

# gtk+2.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter <mozes@slackware.com>
# 26-Aug-2004

# Record toolchain & other info for the build log:
slackbuildinfo

# Paths to skeleton port's source & real Slackware source tree:
export CWD=$SLACKSOURCE/$PKGSERIES/$PKGNAM
export PORTCWD=$PWD

# Temporary build locations:
export TMPBUILD=$TMP/build-$PKGNAM
export PKG=$TMP/package-$PKGNAM
mkpkgdirs # Delete & re-create temporary directories then cd into $TMPBUILD

# Determine the CFLAGS for the known architectures:
case $ARCH in
   arm)     export SLKCFLAGS="-O2 -march=armv4t"
            export LIBDIRSUFFIX="" ;;
   *)       export SLKCFLAGS="-O2" ;;
esac

# Extract source:
tar xvvf $CWD/gtk+-$VERSION.tar.?z*
cd gtk+-$VERSION || exit 1
slackhousekeeping 

# 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

# Do not flood the console or .xsession-errors with "trouble ahead"
# warnings (most noticable when using Firefox and Flash):
zcat $CWD/gtk.xid.nowarningflood.diff.gz | patch -p1 --verbose || exit 1

# Use libpng-1.4.x:
zcat $CWD/gtk.png14.diff.gz | patch -p1 --verbose || exit 1

# Use /etc/gtk-2.0/$ARCH-slackware-linux instead of /etc/gtk-2.0/
sed -i "s/GTK_SYSCONFDIR, \\\"gtk-2\\.0\\\"/GTK_SYSCONFDIR, \\\"gtk-2\\.0\\\", HOST/g" ./gtk/gtkrc.c ./gdk-pixbuf/gdk-pixbuf-io.c
sed -i 's/sysconfdir)\\"/&      \\\n\t-DHOST=\\\"$(host_triplet)\\\"/g' \
gdk-pixbuf/Makefile.am \
gtk/Makefile.am \
modules/input/Makefile.am

# Regenerate ./configure:
libtoolize --force
autoreconf -vif

# Autoconf changes linux to linux-gnu.
# Our host is $ARCH-slackware-linux-gnueabi not $ARCH-slackware-linux-gnu:
sed -i -e 's#linux|linux-gnu|#linux|linux-gnueabi|#' config.sub

# And this was how I got it build for ARMedslack prior to that patch
# being included in Slackware.
# See http://mail.gnome.org/archives/gtk-devel-list/2006-May/msg00257.html
# for the reason:
#sed -i 's?LIBPNG =\(.*\)$?LIBPNG = \1 -lz?' gdk-pixbuf/Makefile 

# Configure:
#export PKG_CONFIG_PATH=/usr/lib/pkgconfig/
CFLAGS="$SLKCFLAGS -DHOST='"'"${ARCH}-slackware-linux-gnueabi"'"'" \
./configure \
  --prefix=/usr \
  --libdir=/usr/lib${LIBDIRSUFFIX} \
  --sysconfdir=/etc \
  --mandir=/usr/man \
  --with-xinput=yes \
  --build=$ARCH-slackware-linux-gnueabi || failconfig

# Build:
make $NUMJOBS || failmake

# Check the host value that is passed to the compile to the one in this script:
host="$ARCH-slackware-linux-gnueabi"
compile_host=$(grep 'host_triplet =' gtk/Makefile | sed -e "s/.* = //")
if [ "x$compile_host" != "x$host" ]; then
  echo "Host mismatch: compile='$compile_host', SlackBuild='$host'" && exit 1
fi

# Install:
make install DESTDIR=$PKG \
  RUN_QUERY_IMMODULES_TEST=false RUN_QUERY_LOADER_TEST=false \
  || exit 1

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

mv $PKG/etc/gtk-2.0/$host/im-multipress.conf $PKG/etc/gtk-2.0/$host/im-multipress.conf.new || exit 1

# We need to have separate 32-bit and 64-bit binaries
# for places where we have two copies of the GTK+ package installed.
# (we might have x86_64 and i486 packages on the same system, for example.)
case "$host" in
  s390x*|x86_64*)
   mv $PKG/usr/bin/gtk-query-immodules-2.0{,-64}
   mv $PKG/usr/bin/gdk-pixbuf-query-loaders{,-64}
   ( cd $PKG/usr/bin
     ln -sf gtk-query-immodules-2.0-64 gtk-query-immodules-2.0
     ln -sf gdk-pixbuf-query-loaders-64 gdk-pixbuf-query-loaders
   )
   ;;
  *)
   mv $PKG/usr/bin/gtk-query-immodules-2.0{,-32}
   mv $PKG/usr/bin/gdk-pixbuf-query-loaders{,-32}
   ( cd $PKG/usr/bin
     ln -sf gtk-query-immodules-2.0-32 gtk-query-immodules-2.0
     ln -sf gdk-pixbuf-query-loaders-32 gdk-pixbuf-query-loaders
   )
   ;;
esac

mkdir -p $PKG/usr/doc/gtk+-$VERSION
cp -a \
  AUTHORS COPYING NEWS README TODO \
  $PKG/usr/doc/gtk+-$VERSION
( cd $PKG/usr/doc/gtk+-$VERSION
  ln -s /usr/share/gtk-doc/html/gail-libgail-util .
  ln -s /usr/share/gtk-doc/html/gdk .
  ln -s /usr/share/gtk-doc/html/gdk-pixbuf .
  ln -s /usr/share/gtk-doc/html/gtk .
)
changelogliposuction ChangeLog $PKGNAM $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/*

# If necessary, start the fakeroot server so we can set file/dir ownerships:
start_fakeroot

# Apply generic Slackware packaging policies:
cd $PKG
slackstripall   # strip all .a archives and all ELFs
slackgzpages -i # compress man & info pages and delete usr/info/dir
slackslack      # chown -R root:root, chmod -R og-w, slackchown, slack644docs
slackdesc       # install slack-desc and doinst.sh
slackmp         # run makepkg -l y -c n

# Perform any final checks on the package:
cd $PKG
slackhlinks     # search for any hard links
