# Build recipe for tqt3 (trinity).
#
# Copyright (c) 2019-2021 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=tqt3-trinity
pkgname=tqt3
version=14.0.9
release=1

# Define a category for the output of the package name
pkgcategory=tde

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

# Remote source(s)
fetch=https://mirror.ppa.trinitydesktop.org/trinity/releases/R${version}/main/dependencies/$tarname

description="
Qt is a multiplatform C++ GUI application framework.

Qt 3.3 introduces new features and many improvements over the 3.2.x
series. See the changes file for details.

The Qt version 3.3 series is binary compatible with the 3.2.x series -
applications compiled for 3.2 will continue to run with 3.3.
"

homepage=https://www.trinitydesktop.org/
license="GPLv2+, GPLv3+ | The Q Public License"

# Source documentation
docs="FAQ LICENSE* README*"
docsdir="${docdir}/${pkgname}-${version}"

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

    cd "$srcdir"

    # Remove unneeded specs for targets other than GNU/Linux
    ( cd mkspecs && find . -maxdepth 1 \( -type d -a ! -name 'linux-g++' \) -exec rm -rf -- '{}' \; )

    # Do not build the HTML documentation in order to save disk space
    #sed -i '/htmldocs/d' src/qt_install.pri

    # Set custom compilation flags from Qi
    sed -i \
     -e "s:QMAKE_CFLAGS_RELEASE.*=:QMAKE_CFLAGS_RELEASE=${QICXXFLAGS} :" \
      mkspecs/linux*/qmake.conf

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

    SAVED_QTDIR="$QTDIR"
    QTDIR="$PWD"
    LD_LIBRARY_PATH="${PWD}/lib:${LD_LIBRARY_PATH}"
    SAVED_PATH="$PATH"
    PATH="${PWD}/bin:${PATH}"

    echo ""
    echo "New QTDIR: $QTDIR"
    echo "New LD_LIBRARY_PATH: $LD_LIBRARY_PATH"
    echo "New PATH: $PATH"
    echo ""

    export QTDIR LD_LIBRARY_PATH PATH

    echo yes | ./configure -L/usr/lib${libSuffix} -I/usr/include/tirpc \
     -prefix /opt/trinity/lib${libSuffix}/${pkgname}-${version} \
     -plugindir /opt/trinity/lib${libSuffix}/${pkgname}-${version}/plugins \
     -sysconfdir /opt/etc \
     -sysshare /usr/share \
     -docdir $docsdir \
     -no-exceptions \
     -stl \
     -largefile \
     -qt-gif \
     -system-zlib \
     -system-libpng \
     -system-libjpeg \
     -system-libmng \
     -plugin-imgfmt-png \
     -plugin-imgfmt-jpeg \
     -plugin-imgfmt-mng \
     -thread \
     -xinerama \
     -xcursor \
     -xrandr \
     -xrender \
     -xft \
     -tablet \
     -xkb \
     -plugin-style-cde \
     -plugin-style-compact \
     -plugin-style-motif \
     -plugin-style-motifplus \
     -plugin-style-platinum \
     -plugin-style-sgi \
     -plugin-style-windows \
     -release

    make -i -j${jobs} sub-tools
    make -j${jobs} INSTALL_ROOT="$destdir" install

    PATH=$SAVED_PATH
    QTDIR=$SAVED_QTDIR
    unset -v SAVED_QTDIR SAVED_PATH LD_LIBRARY_PATH
    export PATH QTDIR

    # Strip remaining binaries and libraries
    find "$destdir" -type f | xargs file | \
     awk '/ELF/ && /executable/ || /shared object/' | \
      cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null

    # Make symlinks in order to have canonical path(s)

    mkdir -p "${destdir}/var/lib/qi"
    cat << EOF > "${destdir}/var/lib/qi/${full_pkgname}.sh"

# Make symlink for canonical path at /opt/trinity/lib

# Link using \$libSuffix if it was present (recipe)
test -n "$libSuffix" && ( cd opt/trinity && ln -sf lib${libSuffix} lib );

# Link package name
cd opt/trinity/lib && ln -sf ${pkgname}-${version} $pkgname

echo "Updating desktop database: update-desktop-database -q usr/share/applications"
update-desktop-database -q usr/share/applications

EOF

    cd "${destdir}/opt/trinity" || exit 1

    # Link tqt3's pkg-config files at /opt/trinity/lib/pkgconfig

    mkdir -p "${destdir}/opt/trinity/lib${libSuffix}/pkgconfig"
    cd "${destdir}/opt/trinity/lib${libSuffix}/${pkgname}-${version}"/lib/pkgconfig/
    for file in *.pc
    do
        ln -sf \
         "/opt/trinity/lib${libSuffix}/${pkgname}-${version}/lib/pkgconfig/${file}" \
         "${destdir}/opt/trinity/lib${libSuffix}/pkgconfig/${file}"
    done

    # Link tqt3's programs at /opt/trinity/bin

    mkdir -p "${destdir}/opt/trinity/bin"
    cd "${destdir}/opt/trinity/lib${libSuffix}/${pkgname}-${version}"/bin/
    for file in *
    do
        test -x "$file" || continue;

        ln -sf \
         "/opt/trinity/lib${libSuffix}/${pkgname}-${version}/bin/${file}" \
         "${destdir}/opt/trinity/bin/${file}"
    done

    cd -- "${TMPDIR}/$srcdir"

    # Include profile file
    mkdir -p "${destdir}/etc/profile.d"
    cp -p "${worktree}/archive/tqt3/etc/profile.d/tqt3.sh" \
           "${destdir}/etc/profile.d/"
    chmod 644 "${destdir}/etc/profile.d/tqt3.sh"

    # To handle config file(s)
    touch "${destdir}/etc/profile.d/.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}"
}

