##
##  avahi.spec -- OpenPKG RPM Package Specification
##  Copyright (c) 2000-2025 OpenPKG Project <http://openpkg.org/>
##
##  Permission to use, copy, modify, and distribute this software for
##  any purpose with or without fee is hereby granted, provided that
##  the above copyright notice and this permission notice appear in all
##  copies.
##
##  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
##  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
##  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
##  IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
##  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
##  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
##  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
##  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
##  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
##  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
##  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
##  SUCH DAMAGE.
##

#   FIXME: rse: package still fully untested

#   package information
Name:         avahi
Summary:      Zeroconf/DNS-SD/mDNS Toolkit
URL:          http://avahi.org/
Vendor:       Lennart Poettering, Trent Lloyd, Sjoerd Simons
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        Networking
License:      GPL
Version:      0.7
Release:      20170710

#   package options
%option       with_dbus  no

#   list of sources
Source0:      http://avahi.org/download/avahi-%{version}.tar.gz
Source1:      rc.avahi
Source2:      avahi-daemon.conf
Patch0:       avahi.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101
PreReq:       OpenPKG, openpkg >= 20160101
BuildPreReq:  libdaemon, expat, libpcap, getopt, gdbm
PreReq:       libdaemon, expat, libpcap, getopt, gdbm
%if "%{with_dbus}" == "yes"
BuildPreReq:  dbus
PreReq:       dbus
%endif

%description
    Avahi is a system which facilitates service discovery on a local
    network via the mDNS/DNS-SD protocol suite. This enables you to plug
    your laptop or computer into a network and instantly be able to view
    other people who you can chat with, find printers to print to or
    find files being shared. Compatible technology is found in Apple
    MacOS X (branded Bonjour and sometimes ZeroConf).

%track
    prog avahi = {
        version   = %{version}
        url       = http://avahi.org/download/
        regex     = avahi-(__VER__)\.tar\.gz
    }

%prep
    %setup -q
    %patch -p0

%build
    #   configure program
    CC="%{l_cc}" \
    CFLAGS="%{l_cflags -O}" \
    CPPFLAGS="%{l_cppflags}" \
    LDFLAGS="%{l_ldflags}" \
    ./configure \
        --prefix=%{l_prefix} \
        --mandir=%{l_prefix}/man \
        --with-xml=expat \
        --with-avahi-user=%{l_nusr} \
        --with-avahi-group=%{l_ngrp} \
        --with-autoipd-user=%{l_nusr} \
        --with-autoipd-group=%{l_ngrp} \
        --with-avahi-priv-access-group=%{l_mgrp} \
        --disable-glib \
        --disable-gobject \
%if "%{with_dbus}" == "yes"
        --enable-dbus \
%else
        --disable-dbus \
%endif
        --disable-python \
        --disable-qt3 \
        --disable-qt4 \
        --disable-gtk \
        --disable-gtk3 \
        --disable-mono \
        --disable-monodoc \
        --disable-shared \
        --disable-nls

    #   build program
    %{l_make} %{l_mflags -O}

%install
    #   install program
    %{l_make} %{l_mflags} install AM_MAKEFLAGS="DESTDIR=$RPM_BUILD_ROOT"

    #   strip down installation
    rm -f $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d/avahi*
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/share/locale
%if "%{with_dbus}" != "yes"
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/share/dbus-1
%endif
    strip $RPM_BUILD_ROOT%{l_prefix}/bin/* >/dev/null 2>&1 || true
    strip $RPM_BUILD_ROOT%{l_prefix}/sbin/* >/dev/null 2>&1 || true

    #   install default configuration
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/etc/avahi
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        %{SOURCE avahi-daemon.conf} \
        $RPM_BUILD_ROOT%{l_prefix}/etc/avahi/

    #   install run-command script
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        %{SOURCE rc.avahi} $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d/

    #   make sure the database directory exists
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/var/avahi/run \
        $RPM_BUILD_ROOT%{l_prefix}/var/avahi/log

    #   determine the package files
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT \
        %{l_files_std} \
        '%config %{l_prefix}/etc/avahi/*' \
        '%attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/avahi' \
        '%attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/avahi/*'

%files -f files

%clean

%pre
    #   before upgrade, save status and stop service
    [ $1 -eq 2 ] || exit 0
    eval `%{l_rc} avahi status 2>/dev/null | tee %{l_tmpfile}`
    %{l_rc} avahi stop 2>/dev/null
    exit 0

%post
    if [ $1 -eq 2 ]; then
        #   after upgrade, restore status
        { eval `cat %{l_tmpfile}`; rm -f %{l_tmpfile}; true; } >/dev/null 2>&1
        [ ".$avahi_active" = .yes ] && %{l_rc} avahi start
    fi
    exit 0

%preun
    #   before erase, stop service and remove log files
    [ $1 -eq 0 ] || exit 0
    %{l_rc} avahi stop 2>/dev/null
    rm -f  $RPM_INSTALL_PREFIX/var/avahi/run/* >/dev/null 2>&1 || true
    rm -f  $RPM_INSTALL_PREFIX/var/avahi/log/* >/dev/null 2>&1 || true
    exit 0

