# Build recipe for cups-filters.
#
# Copyright (c) 2022 Matias Fonzo, <selk@dragora.rog>.
#
# 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=cups-filters
version=1.28.16
release=2

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

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

# Remote source(s)
fetch=https://www.openprinting.org/download/cups-filters/$tarname

homepage=https://www.openprinting.org

description="
Backends, filters, and other software for CUPS.

This package contains backends, filters, and other software that
was once part of the core CUPS distribution but is no longer
maintained by Apple Inc.  In addition it contains additional
filters and software developed independently of Apple, especially
filters for the PDF-centric printing workflow introduced by
OpenPrinting and a daemon to browse broadcasts of remote CUPS
printers and IPP printers to make them available locally.
"

license=Custom

# Source documentation
docs="AUTHORS COPYING ChangeLog NEWS README"
docsdir="${docdir}/${program}-${version}"

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

    cd "$srcdir"

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

    ./configure CPPFLAGS="$QICPPFLAGS" \
    CFLAGS="$QICFLAGS" CXXFLAGS="$QICXXFLAGS" LDFLAGS="$QILDFLAGS" \
     $configure_args \
     --libdir=/usr/lib${libSuffix} \
     --mandir=$mandir \
     --docdir=$docsdir \
     --enable-static=no \
     --enable-shared=yes \
     --disable-avahi \
     --disable-mutool \
     --with-browseremoteprotocols=cups \
     --with-test-font-path=/usr/share/fonts/X11/TTF/DejaVuSans.ttf \
     --without-php \
     --without-rcdir \
     --build="$(gcc -dumpmachine)"

    make -j${jobs} V=1
    make -j${jobs} check
    make -j${jobs} DESTDIR="$destdir" install

    # Install CUPS-BROWSED perp service

    mkdir -p "${destdir}/etc/perp/cups-browsed"

    cp -p "${worktree}/archive/cups-filters/rc.log" \
          "${worktree}/archive/cups-filters/rc.main" \
          "${destdir}/etc/perp/cups-browsed/"

    chmod 755 "${destdir}"/etc/perp/cups-browsed/rc.*

    # Do not enable cups-browsed by default
    chmod -t "${destdir}/etc/perp/cups-browsed"

    # To handle (dot) .new files via graft(1)

    touch "${destdir}/etc/perp/cups-browsed/.graft-config" \
          "${destdir}/etc/cups/.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"
}

