#!/bin/sh
#
# source: http://ftp.gnome.org/pub/gnome/sources/gtk+/3.2/gtk+-3.2.4.tar.xz
#
# maintainer: gmg (g.monpontet@gmail.com)
#

#------------------Set Some Variables

soft='gtk+3'
softversion='3.2.4'
packageversion='72.1'

arch='i486'
cpu='i686'

buildir=$(pwd)
srcpkg="$buildir/gtk+-$softversion.tar.xz"
src="$buildir/gtk+-$softversion"
package="$soft-$softversion-$arch-$packageversion"
dest="$buildir/$package"

cd $buildir
rm -rf $src
tar -xvf $srcpkg
rm -f $dest.txz
rm -rf $dest

#------------------Set correct permissions

cd $src
chown -R root:root .
find . \
 \( -perm 777 -o -perm 775 -o -perm 711 -o -perm 555 -o -perm 511 \) \
 -exec chmod 755 {} \; -o \
 \( -perm 666 -o -perm 664 -o -perm 600 -o -perm 444 -o -perm 440 -o -perm 400 \) \
 -exec chmod 644 {} \;
export CFLAGS="-O2 -march=$arch -mtune=$cpu"
export CXXFLAGS="-O2 -march=$arch -mtune=$cpu"
./configure \
  --prefix=/usr \
  --sysconfdir=/etc \
  --localstatedir=/var \
  --mandir=/usr/man \
  --enable-gtk2-dependency \
  --disable-schemas-compile \
  --with-html-dir=/usr/doc/gtk+-$softversion

#https://bugzilla.gnome.org/show_bug.cgi?id=655517
sed -i -e 's/ -shared / -Wl,-O1,--as-needed\0/g' libtool
make -j6
make install DESTDIR=$dest

find $dest | xargs file | grep -e "executable" -e "shared object" \
  | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null

mv $dest/etc/gtk-3.0/im-multipress.conf $dest/etc/gtk-3.0/im-multipress.conf.new

# Compress and if needed symlink the man pages:
if [ -d $dest/usr/man ]; then
  ( cd $dest/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

#-------------------Prepare the documentation

cp -a \
  AUTHORS COPYING HACKING INSTALL NEWS README \
  $dest/usr/doc/gtk+-$softversion

mkdir -p $dest/install
cat << EODESC > $dest/install/slack-desc
# HOW TO EDIT THIS FILE:
# The "handy ruler" below makes it easier to edit a package description.  Line
# up the first '|' above the ':' following the base package name, and the '|'
# on the right side marks the last column you can put a character in.  You must
# make exactly 11 lines for the formatting to be correct.  It's also
# customary to leave one space after the ':'.

     |-----handy-ruler----------------------------------------------------------|
gtk+3: gtk+3 - The Gimp Toolkit, Version 3
gtk+3:
gtk+3: GTK+3 is a multi-platform toolkit for creating graphical user  interfaces
gtk+3: Offering a complete set of widgets, GTK+ is suitable for projects ranging
gtk+3: from small one-off projects to complete application suites.
gtk+3: 
gtk+3:
gtk+3: 
gtk+3: 
gtk+3:
gtk+3:
EODESC

cat <<"DOINST" > $dest/install/doinst.sh
config() {
  NEW="$1"
  OLD="`dirname $NEW`/`basename $NEW .new`"
  # If there's no config file by that name, mv it over:
  if [ ! -r $OLD ]; then
    mv $NEW $OLD
  elif [ "`cat $OLD | md5sum`" = "`cat $NEW | md5sum`" ]; then # toss the redundant copy
    rm $NEW
  fi
  # Otherwise, we leave the .new copy for the admin to consider...
}

config etc/gtk-3.0/im-multipress.conf.new

# update glib schemas
if [ -x usr/bin/glib-compile-schemas ]; then
  usr/bin/glib-compile-schemas usr/share/glib-2.0/schemas 1> /dev/null 2> /dev/null
fi
DOINST

mkdir -p $dest/usr/src/$soft-$softversion
cp -a $buildir/build-$soft.sh $dest/usr/src/$soft-$softversion

#-------------------Make the packages and the md5sum

cd $dest
makepkg -p -l y -c n $dest.txz

cd $buildir
md5sum $package.txz > $package.md5