# Build recipe for GTK+3.
#
# Copyright (C) 2018, MMPG <mmpg@vp.pl>
# Copyright (C) 2018-2019, 2021-2023 Matias Fonzo, <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Exit immediately on any error
set -e

program=gtk+
version=3.24.36
release=2

# Define a category for the output of the package name
pkgcategory=x-libs

# Difference package name with other series
pkgname=gtk3

tarname=${program}-${version}.tar.xz

# Remote source(s)
fetch="
  https://ftp.gnome.org/pub/gnome/sources/gtk+/${version%.*}/$tarname
  ftp://ftp.acc.umu.se/pub/gnome/sources/gtk+/${version%.*}/$tarname
"

description="
The version 3 of the GTK+ toolkit.

The GTK+3 package contains libraries used for creating graphical
user interfaces for applications.
"

homepage=https://www.gtk.org
license=GPLv2+

# Copy documentation
docs="CONTRIBUTING.md COPYING NEWS README.md"
docsdir="${docdir}/${pkgname}-${version}"

build()
{
    unpack "${tardir}/$tarname"

    cd "$srcdir"

    # Set sane permissions
    chmod -R u+w,go-w,a+rX-s .

    # Set default backends, detect CUPS
    print_backends=file,lpr

    if type cupsd
    then
        print_backends=file,lpr,cups
    fi

    rm -rf BUILD
    mkdir BUILD
    cd BUILD

    CPPFLAGS="$QICPPFLAGS" CFLAGS="$QICFLAGS" LDFLAGS="$QILDFLAGS" \
    meson setup $configure_args \
     --libdir=lib${libSuffix} \
     --mandir=$mandir \
     --buildtype=release \
     --strip \
     -Dcolord=no \
     -Dgtk_doc=false \
     -Dman=true \
     -Dx11_backend=true \
     -Dbroadway_backend=true \
     -Dwayland_backend=false \
     -Dxinerama=yes \
     -Dprint_backends="$print_backends" \
     ..

    unset -v print_backends
    ninja -j${jobs}
    DESTDIR="$destdir" ninja -j${jobs} install

    cd ..

    # Add a basic configuration
cat << EOF > "${destdir}/etc/gtk-3.0/gtkrc"
gtk-theme-name = "gnome"

EOF

    # Manage dot new file(s) via graft(8)
    touch "${destdir}/etc/gtk-3.0/.graft-config"

    # Compress and link man pages (if needed)
    if test -d "${destdir}/$mandir"
    then
        (
            cd "${destdir}/$mandir"
            find . -type f -exec lzip -9 {} +
            find . -type l | while read -r file
            do
                ln -sf "$(readlink -- "$file").lz" "${file}.lz"
                rm -- "$file"
            done
        )
    fi

    # Copy documentation
    mkdir -p "${destdir}/$docsdir"
    cp -p $docs "${destdir}/$docsdir"
}

