#!/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=armv5te"
            export LIBDIRSUFFIX="" ;;
   *)       export SLKCFLAGS="-O2" ;;
esac

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

# Fix build errors
zcat $CWD/fix_build_issues_on_tutorial_and_faq.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 /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
#sed -i 's/sysconfdir)\\"/&      \\\n\t-DHOST=\\\"$(host_triplet)\\\"/g' gtk/Makefile.am
##sed -i 's/sysconfdir)\\"/&      \\\n\t-DHOST=\\\"$(host_triplet)\\\"/g' modules/input/Makefile.am

# Use /etc/gtk-2.0/$ARCH-slackware-linux instead of /etc/gtk-2.0/ for gtk.immodules
zcat $CWD/use_host_triplet_for_gtk_immodules.diff.gz | patch -p1 --verbose || exit 1

# Patch to restore commonly used icon names, otherwise everything breaks.
# I fail to see the point of this "cleanup"
zcat $CWD/gtk+-2.24.x.icon-compat.diff.gz | patch -p1 --verbose || exit 1
zcat $CWD/gtk+-2.24.x.icon-compat.am.diff.gz | patch -p1 --verbose || exit 1

# 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 \
  --enable-xkb \
  --enable-introspection \
  --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
mkdir -vpm755 $PKG/etc/gtk-2.0/$host
mv -fv $PKG/etc/gtk-2.0/gtk.immodules $PKG/etc/gtk-2.0/$host/

# Don't clobber im-multipress.conf
mv $PKG/etc/gtk-2.0/im-multipress.conf $PKG/etc/gtk-2.0/im-multipress.conf.new || exit 1

# Install a "starter" gtkrc
echo 'gtk-theme-name="GTK+"' > $PKG/etc/gtk-2.0/gtkrc.new

# 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}
   ( cd $PKG/usr/bin
     ln -sf gtk-query-immodules-2.0-64 gtk-query-immodules-2.0
   )
   ;;
  *)
   mv $PKG/usr/bin/gtk-query-immodules-2.0{,-32}
   ( cd $PKG/usr/bin
     ln -sf gtk-query-immodules-2.0-32 gtk-query-immodules-2.0
   )
   ;;
esac

# Install wrappers for the binaries:
cp -fav $CWD/update-gtk-immodules* $PKG/usr/bin/
chmod 0755 $PKG/usr/bin/update-*

# Add profile scripts to set up GTK+ environment:
mkdir -p $PKG/etc/profile.d
cp -fav $CWD/profile.d/* $PKG/etc/profile.d
chmod 0755 $PKG/etc/profile.d/*

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 ../../share/gtk-doc/html/gail-libgail-util .
  ln -s ../../share/gtk-doc/html/gdk .
  ln -s ../../share/gtk-doc/html/gtk .
)
changelogliposuction ChangeLog $PKGNAM $VERSION

# 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

# Fix the install script:
#sed -i 's?-slackware-linux?-slackware-linux-gnueabi?g' $PKG/install/doinst.sh 

slackmp         # run makepkg -l y -c n

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