# Maintainer: Markus Fritsche <mfritsche@reauktion.de>
# Based on Arch Linux ARM's distcc PKGBUILD, rebuilt with --with-avahi
# so Zeroconf-announced compile servers are discoverable on the LAN.
#
# Why this exists: the ALARM aarch64 distcc binary package is built
# without --with-avahi, so the distccd --zeroconf flag is a no-op.
# The Arch x86_64 package is fine. This package replaces distcc on
# ALARM hosts with an avahi-aware build.

pkgname=distcc-avahi
_pkgname=distcc
pkgver=3.4
pkgrel=17
pkgdesc="Distributed compilation service for C, C++ and Objective-C (with Avahi/Zeroconf support)"
arch=('x86_64' 'aarch64')
url="https://github.com/distcc/distcc"
license=('GPL-2.0-only')
depends=('popt' 'python' 'avahi')
makedepends=('autoconf' 'automake' 'libtool' 'pkgconf' 'python-setuptools')
optdepends=('gtk3: for distccmon')
provides=("distcc=${pkgver}")
conflicts=('distcc')
replaces=('distcc')
backup=('etc/conf.d/distccd')
source=(
    "${_pkgname}-${pkgver}.tar.gz::https://github.com/distcc/distcc/archive/refs/tags/v${pkgver}.tar.gz"
    "distccd.conf"
    "distccd.service"
    "distcc.tmpfiles"
    "fix-gcc-rewrite-fqn-overflow.patch"
)
sha256sums=(
    '37a34c9555498a1168fea026b292ab07e7bb394715d87d8403e0c33b16d2d008'
    '38cb1912bfa15efd762dd868e049bdbcd58f1a46065255bc4648f821ba516d65'
    'a4f1d1bb21d61d41f22e918b448cfb852a6d95b0d3b922bd82805090cb2ce41a'
    'd8aee2eb895c02a39e0f2b76fd4a5c9dce91405f1c443286ca324628eadbf3f1'
    '7ff56af2ea505bfbf65ceeb0c8f752295f73ffb1173c26a6e978840fad04f651'
)

prepare() {
    cd "${_pkgname}-${pkgver}"
    patch -p1 -i "${srcdir}/fix-gcc-rewrite-fqn-overflow.patch"
    autoreconf -fiv
}

build() {
    cd "${_pkgname}-${pkgver}"
    # distcc 3.4 has a const-qualifier discard that gcc 15+ flags as error
    # under its own -Werror. Downgrade to a warning.
    export CFLAGS="${CFLAGS} -Wno-error"
    export CXXFLAGS="${CXXFLAGS} -Wno-error"
    ./configure \
        --prefix=/usr \
        --sbindir=/usr/bin \
        --sysconfdir=/etc \
        --mandir=/usr/share/man \
        --with-auth \
        --with-avahi \
        --without-libiberty \
        --enable-rfc2553 \
        --with-docdir="/usr/share/doc/${pkgname}"
    make
    # Archive tarball has no doc/ Makefile; skip man/html build.
    [ -f doc/Makefile ] && make -C doc || true
}

check() {
    cd "${_pkgname}-${pkgver}"
    # Upstream test suite is flaky under makepkg; skip unless CHECK=1.
    [ "${CHECK:-0}" = "1" ] && make check || true
}

package() {
    cd "${_pkgname}-${pkgver}"
    make DESTDIR="${pkgdir}" prefix=/usr install
    install -Dm644 ../distccd.conf    "${pkgdir}/etc/conf.d/distccd"
    install -Dm644 ../distccd.service "${pkgdir}/usr/lib/systemd/system/distccd.service"
    install -Dm644 ../distcc.tmpfiles "${pkgdir}/usr/lib/tmpfiles.d/distcc.conf"
}
