# Build recipe for cups.
#
# 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
version=2.4.2
release=1

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

tarname=${program}-${version}-source.tar.gz

# Remote source(s)
fetch=https://github.com/OpenPrinting/cups/releases/download/v${version}/$tarname

homepage=https://www.cups.org

description="
Common UNIX printing system.

The Common UNIX Printing System (or CUPS) is a printing system and
general replacement for lpd and the like.  It supports the Internet
Printing Protocol (IPP), and has its own filtering driver model for
handling various document types.

Visit the web site of CUPS at $homepage
"

license="Apache License version 2.0"

# Source documentation
docs="CHANGES.md CONTRIBUTING.md CREDITS.md DEVELOPING.md LICENSE NOTICE README.md SECURITY.md"
docsdir="${docdir}/${program}-${version}"

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

    cd "$srcdir"

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

    # To look for passed 'libdir' below
    sed -e 's|$exec_prefix/lib/cups|$libdir/cups|g' -i configure

    ./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 \
     --enable-libpaper \
     --enable-tcp-wrappers \
     --enable-acl \
     --enable-relro \
     --disable-pam \
     --with-docdir=$docsdir \
     --with-pkgconfpath=/usr/lib${libSuffix}/pkgconfig \
     --with-dbusdir=/usr/share/dbus-1 \
     --with-logdir=/var/log/cups \
     --with-rcdir=/etc/cupsinit \
     --with-tls=gnutls \
     --with-ipp-port=631 \
     --build="$(gcc -dumpmachine)"

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

    # This "sysv stuff" is not suitable for us
    rm -rf "${destdir}/etc/cupsinit"

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

    find "${destdir}/etc" -type d -print | while read -r directory
    do
        ( cd -- "$directory" && touch .graft-config )
    done

    # Install CUPS perp service

    mkdir -p "${destdir}/etc/perp/cupsd"

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

    chmod 755 "${destdir}"/etc/perp/cupsd/rc.*

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

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

