%{?!WITH_QT5:     %global WITH_QT5    1}
%{?!WITH_PYTHON:  %global WITH_PYTHON 0}

Name:             avahi
Version:          0.8
Release:          3
Summary:          Avahi is a local network service discovery
License:          LGPLv2+
URL:              http://avahi.org
Source0:          http://avahi.org/download/%{name}-%{version}.tar.gz

## upstream patches
Patch0:           0000-avahi-dnsconfd.service-Drop-Also-avahi-daemon.socket.patch
Patch1:           0001-man-add-missing-bshell.1-symlink.patch
Patch2:           0002-Ship-avahi-discover-1-bssh-1-and-bvnc-1-also-for-GTK.patch
Patch3:           0003-fix-requires-in-pc-file.patch
Patch4:           0004-fix-bytestring-decoding-for-proper-display.patch
Patch5:           0005-avahi_dns_packet_consume_uint32-fix-potential-undefi.patch

BuildRequires:    gcc automake libtool desktop-file-utils gtk2-devel glib2-devel
BuildRequires:    libcap-devel expat-devel gdbm-devel
BuildRequires:    intltool perl-XML-Parser xmltoman systemd qt5-qtbase-devel libevent-devel
BuildRequires:    dbus-devel >= 0.90 dbus-glib-devel >= 0.70 gtk3-devel >= 2.99.0 libdaemon-devel >= 0.11
BuildRequires:    pkgconfig(pygobject-3.0) pkgconfig(libevent) >= 2.0.21 
%if %{WITH_PYTHON}
BuildRequires:    python2-dbus python2-libxml2
BuildRequires:    python2-devel
BuildRequires:    python3-devel
%else
Obsoletes:        python2-avahi < %{version}-%{release}
Obsoletes:        python3-avahi < %{version}-%{release}
Obsoletes:        avahi-ui-tools < %{version}-%{release}
%endif

Requires:         dbus expat libdaemon >= 0.11 %{name}-libs = %{version}-%{release}
Requires(pre):    shadow-utils coreutils
Requires(post):   dbus systemd
Requires(preun):  systemd
Requires(postun): systemd

Provides:         avahi-autoipd = %{version}-%{release}
Obsoletes:        avahi-autoipd < %{version}-%{release}

%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.

%package tools
Summary:          CMD tools for mDNS browsing and publishing
Requires:         %{name} = %{version}-%{release}
Requires:         %{name}-libs = %{version}-%{release}

%description tools
Command line tools that use avahi to browse and publish mDNS services.

%package ui-tools
Summary:          ui-tools for mDNS
Requires:         %{name} = %{version}-%{release}
Requires:         %{name}-libs = %{version}-%{release} 
Requires:         %{name}-glib = %{version}-%{release}
Requires:         %{name}-ui-gtk3 = %{version}-%{release}
Requires:         tigervnc openssh-clients pygtk2 pygtk2-libglade gdbm
%if %{WITH_PYTHON}
Requires:         python2-avahi = %{version}-%{release}
Requires:         python2-dbus
Requires:         python2-gobject-base
%endif
%description ui-tools
Avahi Graphical user interface tools for mDNS services.

%package ui
Summary:          Gtk uesr interface library for Avahi (Gtk2)
Requires:         %{name}-libs = %{version}-%{release}
Requires:         %{name}-glib = %{version}-%{release} 
Requires:         gtk2

%description ui
This package contains a Gtk 2.x widget for browsing services.

%package dnsconfd
Summary:          This is useful for configuring unicast DNS servers in a DHCP-like fashion with mDNS
Requires:         %{name} = %{version}-%{release}
Requires:         %{name}-libs = %{version}-%{release}

%description dnsconfd
This is useful for configuring unicast DNS servers in a DHCP-like fashion with mDNS.

%package compat-howl
Summary:          Libraries for compat-howl
Requires:         %{name}-libs = %{version}-%{release}
Obsoletes:        howl-libs < %{version}-%{release}
Provides:         howl-libs = %{version}-%{release}

%description compat-howl
Libraries for compat-howl.

%package compat-howl-devel
Summary:          Header files and libs for howl compatibility libraries
Requires:         %{name}-compat-howl = %{version}-%{release}
Requires:         %{name}-devel = %{version}-%{release}
Obsoletes:        howl-devel < %{version}-%{release}
Provides:         howl-devel = %{version}-%{release}

%description compat-howl-devel
Header files and libs for howl compatibility libraries

%package compat-libdns_sd
Summary:          Libraries for compat-libdns_sd
Requires:         %{name}-libs = %{version}-%{release}

%description compat-libdns_sd
Libraries for compat-libdns_sd.

%package compat-libdns_sd-devel
Summary:          Header files and libs for compat-libdns_sd-devel
Requires:         %{name}-compat-libdns_sd = %{version}-%{release}
Requires:         %{name}-devel = %{version}-%{release}

%description compat-libdns_sd-devel
Header files and libs for compat-libdns_sd-devel

%package devel
Summary:           Libraries and header files for avahi development
Requires:          %{name}-libs = %{version}-%{release}
Requires:          %{name} = %{version}-%{release}
Requires:          %{name}-ui = %{version}-%{release}
Requires:          %{name}-ui-gtk3 = %{version}-%{release}
Requires:          glib2-devel
Provides:          %{name}-glib-devel = %{version}-%{release} %{name}-gobject-devel = %{version}-%{release} %{name}-ui-devel = %{version}-%{release}
Obsoletes:         %{name}-glib-devel = %{version}-%{release} %{name}-gobject-devel < %{version}-%{release} %{name}-ui-devel < %{version}-%{release}

%description devel
Header files for using the avahi libraries.

%if %{WITH_PYTHON}
%package -n python2-avahi
Summary:          Python2 Avahi api
Obsoletes:        python-avahi < 0.7
Provides:         python-avahi = %{version}-%{release}
Requires:         %{name} = %{version}-%{release}
Requires:         %{name}-libs = %{version}-%{release}

%description -n python2-avahi
Python2 Avahi api.

%package -n python3-avahi
Summary:          Python3 Avahi api
Requires:         %{name} = %{version}-%{release}
Requires:         %{name}-libs = %{version}-%{release}

%description -n python3-avahi
Python3 Avahi api.
%endif

%package glib
Summary:          Glib libraries for avahi
Requires:         %{name}-libs = %{version}-%{release}

%description glib
Libraries for easy use of avahi from glib applications.

%package gobject
Summary:          GObject wrapper library for Avahi
Requires:         %{name}-libs = %{version}-%{release}
Requires:         %{name}-glib = %{version}-%{release}

%description gobject
This library contains a GObject wrapper for the Avahi API

%package ui-gtk3
Summary:          Gtk user interface library for Avahi (Gtk+ 3 version)
Requires:         %{name}-libs = %{version}-%{release}
Requires:         %{name}-glib = %{version}-%{release}
Requires:         gtk3

%description ui-gtk3
This library contains a Gtk 3.x widget for browsing services.

%package libs
Summary:          Libraries for avahi run-time use

%description libs
The avahi-libs package contains the libraries needed
to run programs that use avahi.

%if %{WITH_QT5}
%package qt5
Summary:         Qt5 libraries for avahi
Requires:        %{name}-libs = %{version}-%{release}

%description qt5
Libraries for easy use of avahi from Qt5 applications.

%package qt5-devel
Summary:         Libraries and header files for avahi Qt5 development
Requires:        %{name}-devel = %{version}-%{release}
Requires:        %{name}-qt5 = %{version}-%{release}

%description qt5-devel
The avahi-qt5-devel package contains the header files and libraries
necessary for developing programs using avahi with Qt5.
%endif

%package_help

%prep
%autosetup -n %{name}-%{version} -p1

rm -fv docs/INSTALL


%build
NOCONFIGURE=1 ./autogen.sh

%configure --disable-monodoc --with-avahi-user=avahi \
        --with-avahi-group=avahi --with-avahi-priv-access-group=avahi \
        --with-autoipd-user=avahi-autoipd  --with-autoipd-group=avahi-autoipd \
        --with-systemdsystemunitdir=%{_unitdir}  --enable-introspection=no \
        --enable-shared=yes --enable-static=no  --disable-silent-rules \
        --enable-compat-libdns_sd --enable-compat-howl --disable-qt3 \
        --disable-qt4 --disable-mono --enable-gtk\
        --with-distro=none\
%if ! %{WITH_PYTHON}
    --disable-python \
%endif
%if ! %{WITH_QT5}
    --disable-qt5 \
%endif
;

%make_build -k V=1 || make V=1

%install
%make_install

ln -s avahi-compat-howl.pc  %{buildroot}/%{_libdir}/pkgconfig/howl.pc
ln -s avahi-compat-libdns_sd.pc %{buildroot}/%{_libdir}/pkgconfig/libdns_sd.pc
ln -s avahi-compat-libdns_sd/dns_sd.h %{buildroot}/%{_includedir}/

install -d $RPM_BUILD_ROOT%{_localstatedir}/run/avahi-daemon
install -d $RPM_BUILD_ROOT%{_localstatedir}/lib/avahi-autoipd

mkdir -p $RPM_BUILD_ROOT/etc/avahi/etc
touch $RPM_BUILD_ROOT/etc/avahi/etc/localtime

%if %{WITH_PYTHON}
# Add python3 support
install -d %{buildroot}%{python3_sitelib}/avahi
cp -r $RPM_BUILD_ROOT%{python2_sitelib}/avahi/* $RPM_BUILD_ROOT%{python3_sitelib}/avahi/
rm -f $RPM_BUILD_ROOT%{python3_sitelib}/avahi/*.py{c,o}
sed -i 's!/usr/bin/python2!/usr/bin/python3!' $RPM_BUILD_ROOT%{python3_sitelib}/avahi/ServiceTypeDatabase.py

# avoid empty GenericName keys from .desktop files
for i in $RPM_BUILD_ROOT%{_datadir}/applications/*.desktop ; do
if [ -n "$(grep '^GenericName=$' $i)" ]; then
  desktop-file-edit --copy-name-to-generic-name $i
fi
done
%else
# unpackaged files
rm -fv $RPM_BUILD_ROOT%{_datadir}/applications/{bssh,bvnc}.desktop
rm -fv $RPM_BUILD_ROOT%{_datadir}/avahi/interfaces/avahi-discover.ui
%endif

%find_lang %{name}
%delete_la_and_a

%check
%if %{WITH_PYTHON}
for i in $RPM_BUILD_ROOT%{_datadir}/applications/*.desktop ; do
desktop-file-validate $i
done
%endif

%pre
%define gid_uid 70
if ! getent group avahi > /dev/null ; then
    groupadd -f -g %{gid_uid} -r avahi
fi

if ! getent passwd avahi > /dev/null ; then
  if ! getent passwd %{gid_uid} > /dev/null ; then
    useradd -r -l -u %{gid_uid} -g avahi -d %{_localstatedir}/run/avahi-daemon -s /sbin/nologin -c "Avahi mDNS/DNS-SD Stack" avahi
  else
    useradd -r -l -g avahi -d %{_localstatedir}/run/avahi-daemon -s /sbin/nologin -c "Avahi mDNS/DNS-SD Stack" avahi
  fi
fi

%define autoipd_gid_pid 170
if ! getent group avahi-autoipd >/dev/null ; then
    groupadd -f -g %{autoipd_gid_pid} -r avahi-autoipd
fi
if ! getent passwd avahi-autoipd > /dev/null ; then
  if ! getent passwd %{autoipd_gid_pid} > /dev/null; then
    useradd -r -u %{autoipd_gid_pid} -l -g avahi-autoipd -d %{_localstatedir}/lib/avahi-autoipd -s /sbin/nologin -c "Avahi IPv4LL Stack" avahi-autoipd
  else
    useradd -r -l -g avahi-autoipd -d %{_localstatedir}/lib/avahi-autoipd -s /sbin/nologin -c "Avahi IPv4LL Stack" avahi-autoipd
  fi
fi

exit 0

%preun
%systemd_preun avahi-daemon.socket avahi-daemon.service

%post
/sbin/ldconfig
/usr/bin/dbus-send --system --type=method_call --dest=org.freedesktop.DBus / org.freedesktop.DBus.ReloadConfig >/dev/null 2>&1 || :
if [ "$1" -eq 1 -a -s /etc/localtime ]; then
        /usr/bin/cp -cfp /etc/localtime /etc/avahi/etc/localtime >/dev/null 2>&1 || :
fi
%systemd_post avahi-daemon.socket avahi-daemon.service

%postun
/sbin/ldconfig
%systemd_postun_with_restart avahi-daemon.socket avahi-daemon.service

%post dnsconfd
%systemd_post avahi-dnsconfd.service

%preun dnsconfd
%systemd_preun avahi-dnsconfd.service

%postun dnsconfd
%systemd_postun_with_restart avahi-dnsconfd.service

%post   compat-howl -p /sbin/ldconfig

%postun compat-howl -p /sbin/ldconfig

%post   compat-libdns_sd -p /sbin/ldconfig

%postun compat-libdns_sd -p /sbin/ldconfig

%post   ui -p /sbin/ldconfig

%postun ui -p /sbin/ldconfig

%post   libs -p /sbin/ldconfig

%postun libs -p /sbin/ldconfig

%post   glib -p /sbin/ldconfig

%postun glib -p /sbin/ldconfig

%post   ui-gtk3 -p /sbin/ldconfig

%postun ui-gtk3 -p /sbin/ldconfig

%post   gobject -p /sbin/ldconfig

%postun gobject -p /sbin/ldconfig

%files -f %{name}.lang
%doc README
%license LICENSE

%{_sbindir}/avahi-daemon
%{_sbindir}/avahi-autoipd
%dir %{_libdir}/avahi
%if %{WITH_PYTHON}
%{_libdir}/avahi/service-types.db
%endif
%{_libdir}/libavahi-core.so.*

%dir %{_datadir}/avahi
%{_datadir}/avahi/*.dtd
%{_datadir}/dbus-1/interfaces/*.xml
%{_datadir}/dbus-1/system-services/org.freedesktop.Avahi.service

%dir %{_sysconfdir}/avahi
%dir %{_sysconfdir}/avahi/etc
%ghost %{_sysconfdir}/avahi/etc/localtime
%config(noreplace) %{_sysconfdir}/avahi/hosts
%config(noreplace) %{_sysconfdir}/avahi/avahi-daemon.conf
%config(noreplace) %{_sysconfdir}/dbus-1/system.d/avahi-dbus.conf
%config(noreplace) %{_sysconfdir}/avahi/avahi-autoipd.action

%dir %{_sysconfdir}/avahi/services
%ghost %dir %{_localstatedir}/run/avahi-daemon
%attr(1770,avahi-autoipd,avahi-autoipd) %dir %{_localstatedir}/lib/avahi-autoipd/
%{_unitdir}/avahi-daemon.service
%{_unitdir}/avahi-daemon.socket

%attr(1770,avahi-autoipd,avahi-autoipd) %dir %{_localstatedir}/lib/avahi-autoipd/
%exclude %{_sysconfdir}/rc.d/init.d/avahi-daemon
%exclude %{_sysconfdir}/rc.d/init.d/avahi-dnsconfd
%exclude %{_datadir}/%{name}-%{version}/*
%exclude %{_sysconfdir}/avahi/services/ssh.service
%exclude %{_sysconfdir}/avahi/services/sftp-ssh.service

%files libs
%doc README
%license LICENSE
%{_libdir}/libavahi-common.so.*
%{_libdir}/libavahi-client.so.*
%{_libdir}/libavahi-libevent.so.*

%files glib
%{_libdir}/libavahi-glib.so.*

%files gobject
%{_libdir}/libavahi-gobject.so.*

%files ui-gtk3
%{_libdir}/libavahi-ui-gtk3.so.*

%files  devel
%{_libdir}/libavahi-common.so
%{_libdir}/libavahi-core.so
%{_libdir}/libavahi-client.so
%{_libdir}/libavahi-glib.so
%{_libdir}/libavahi-gobject.so
%{_libdir}/libavahi-ui.so
%{_libdir}/libavahi-ui-gtk3.so
%{_libdir}/libavahi-libevent.so
%{_libdir}/pkgconfig/avahi-ui.pc
%{_libdir}/pkgconfig/avahi-ui-gtk3.pc
%{_libdir}/pkgconfig/avahi-gobject.pc
%{_libdir}/pkgconfig/avahi-glib.pc
%{_libdir}/pkgconfig/avahi-core.pc
%{_libdir}/pkgconfig/avahi-client.pc
%{_libdir}/pkgconfig/avahi-libevent.pc
%{_includedir}/*
%exclude %{_includedir}/avahi-compat-howl
%exclude %{_includedir}/avahi-compat-libdns_sd
%exclude %{_includedir}/dns_sd.h

%if %{WITH_PYTHON}
%files -n python2-avahi
%{python2_sitelib}/avahi/

%files -n python3-avahi
%{python3_sitelib}/avahi/
%endif

%files dnsconfd
%{_sbindir}/avahi-dnsconfd
%{_unitdir}/avahi-dnsconfd.service
%config(noreplace) %{_sysconfdir}/avahi/avahi-dnsconfd.action

%files tools
%{_bindir}/*
%if %{WITH_PYTHON}
%exclude %{_bindir}/b*
%exclude %{_bindir}/avahi-discover*
%exclude %{_bindir}/avahi-bookmarks

%files ui-tools
%{_bindir}/b*
%{_bindir}/avahi-discover
%{_bindir}/avahi-bookmarks
%{_datadir}/applications/b*.desktop
%{_datadir}/applications/avahi-discover.desktop
%{_datadir}/avahi/interfaces/
%{python2_sitelib}/avahi_discover/
%endif

%files ui
%{_libdir}/libavahi-ui.so.*

%files compat-howl
%{_libdir}/libhowl.so.*

%files compat-howl-devel
%{_libdir}/libhowl.so
%{_includedir}/avahi-compat-howl
%{_libdir}/pkgconfig/avahi-compat-howl.pc
%{_libdir}/pkgconfig/howl.pc


%files compat-libdns_sd
%{_libdir}/libdns_sd.so.*

%files compat-libdns_sd-devel
%{_libdir}/libdns_sd.so
%{_includedir}/avahi-compat-libdns_sd
%{_includedir}/dns_sd.h
%{_libdir}/pkgconfig/avahi-compat-libdns_sd.pc
%{_libdir}/pkgconfig/libdns_sd.pc

%if %{WITH_QT5}
%ldconfig_scriptlets qt5

%files qt5
%{_libdir}/libavahi-qt5.so.*

%files qt5-devel
%{_libdir}/libavahi-qt5.so
%{_includedir}/avahi-qt5/
%{_libdir}/pkgconfig/avahi-qt5.pc
%endif

%files help
%defattr(644,root,root)
%doc docs/* avahi-daemon/example.service avahi-daemon/sftp-ssh.service avahi-daemon/ssh.service
%{_mandir}/man1/*
%{_mandir}/man5/*
%{_mandir}/man8/*

%changelog
* Thu Oct 29 2020 jinzhimin <jinzhimin2@huawei.com> - 0.8-3
- Type:enhancement
- ID:NA
- SUG:NA
- DESC:remove pygtk2 in BuildRequires

* Fri Aug 7 2020 jinzhimin <jinzhimin2@huawei.com> - 0.8-2
- Type:bugfix
- ID:NA
- SUG:NA
- DESC:add provide glib2-devel to solve modify unresolvable err

* Fri Jul 31 2020 zhangqiumiao <zhangqiumiao1@huawei.com> - 0.8-1
- Type:enhancement
- ID:NA
- SUG:NA
- DESC:upgrade to version 0.8

* Sat Feb 29 2020 hexiujun <hexiujun1@huawei.com> - 0.7-21
- Type:NA
- ID:NA
- SUG:NA
- DESC:use python2 explicitly in build environment

* Tue Feb 25 2020 openEuler Buildteam <buildteam@openeuler.org> - 0.7-20
- Type:NA
- ID:NA
- SUG:NA
- DESC:modify fuzz test err

* Tue Dec 24 2019 openEuler Buildteam <buildteam@openeuler.org> - 0.7-19
- rename docs subpackage as help subpackage

* Wed Oct 30 2019 openEuler Buildteam <buildteam@openeuler.org> - 0.7-18
- Type:NA
- ID:NA
- SUG:NA
- DESC:Solve the problem of compilation failures

* Mon Sep 23 2019 hufeng <solar.hu@huawei.com> - 0.7-17
-Create spec
