#!/bin/bash
ulimit -s unlimited
shopt -s extglob

# gtk+2.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter <mozes@slackware.com>
# 26-Aug-2004
#
# Copyright 2008, 2009, 2010, 2011, 2012, 2013, 2016  Patrick J. Volkerding, Sebeka, MN, USA
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

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

# Paths to skeleton port's source & real Slackware source tree:
slackset_var_cwds

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

# Extract source:
tar xvvf $CWD/gtk+-$VERSION.tar.!(*sign|*asc|*sig)
cd gtk+-$VERSION || exit 1
slackhousekeeping

# Include some patches from the 2.24 git branch
#for file in $CWD/patches/*.gz ; do
#  zcat $file | patch -p1 --verbose || exit 1
#done

# 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

# 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.am.diff.gz | patch -p1 --verbose || exit 1
zcat $CWD/gtk+-2.24.x.icon-compat.diff.gz | patch -p1 --verbose || exit 1

# Prevents build failures in the docs.  Found online.
# Only needed if autoreconf is used...  ?
zcat $CWD/gtk.gtk-faq.diff.gz | patch -p1 --verbose || exit 1
zcat $CWD/gtk.gtk-tut.diff.gz | patch -p1 --verbose || exit 1

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

# Autoconf changes linux to linux-gnu.
# Our host is ${SLK_ARCH_HOST} not ${SLK_ARCH_HOST}-gnu:
# For SA64 it's already correct.
if [ "$SLKPORTARCH" = "arm" ]; then
   sed -i -e 's#linux|linux-gnu|#linux|linux-gnueabi|#' config.sub
fi

# 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:
CFLAGS="$SLKCFLAGS" \
./configure \
   --prefix=/usr \
   --libdir=/usr/lib${LIBDIRSUFFIX} \
   --sysconfdir=/etc \
   --mandir=/usr/man \
   --with-xinput=yes \
   --enable-xkb \
   --enable-introspection \
   --build=${SLK_ARCH_BUILD} || failconfig

# Build:
make $NUMJOBS || make || failmake

# Check the host value that is passed to the compile to the one in this script:
host="${SLK_ARCH_HOST}"
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

# 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
# /etc/gtk-2.0/gtkrc overrides this  --rworkman
echo 'gtk-theme-name="GTK+"' > $PKG/usr/share/gtk-2.0/gtkrc
echo 'gtk-icon-theme-name="Adwaita"' >> $PKG/usr/share/gtk-2.0/gtkrc
echo 'gtk-fallback-icon-theme = "Tango"' >> $PKG/usr/share/gtk-2.0/gtkrc

# 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*|aarch64*)
   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 -vfs ../../share/gtk-doc/html/gail-libgail-util .
  ln -vfs ../../share/gtk-doc/html/gdk2 .
  ln -vfs ../../share/gtk-doc/html/gtk2 .
)

# note that we use "gtk+" here as the package name rather than $PKGNAM
# (which is gtk+2).  There is an inconsistency here between gtk+2 and gtk+3.
changelogliposuction ChangeLog gtk+ $VERSION

# Apply generic Slackware packaging policies:
cd $PKG
slackstripall   # strip all .a archives and all ELFs
slack_delete_lafiles # delete usr/lib{,64}/*.la
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
