# This RPM spec file exists primarily to enable CI/CD pipelines and automatic
# testing of built RPMs. This RPM can be used as a suitable baseline for a
# proper distribution package, but changes should be made to suit the needs of
# the package.
#
# See https://src.fedoraproject.org/rpms/yggdrasil/blob/rawhide/f/yggdrasil.spec
# for a better example of a downstream distribution packaging example.

%bcond_without check

%global has_go_rpm_macros (0%{?fedora})

# must be before %%gometa
Version:        0.4.4

# https://github.com/redhatinsights/yggdrasil
%global goipath         github.com/redhatinsights/yggdrasil
%global commit          e9161f860f566120daa11c25d3dd3c722d4bd8cb
%global shortcommit     %(c=%{commit}; echo ${c:0:7})
%global builddate       %(date "+%Y%m%d")
%global archivename     yggdrasil-%{version}

%if %{has_go_rpm_macros}
%gometa %{?fedora:-f}
%else
%global gourl https://github.com/RedHatInsights/%{name}
%global gomodulesmode GO111MODULES=off
%global gosource %{gourl}/releases/download/%{version}/yggdrasil-%{version}.tar.gz
%global gocompilerflags "-buildmode pie -compiler gc"
%endif

# Manually redefine %%dist to work around an issue in COPR where the build root
# that creates the srpm does not define a value for %%dist. This should *NOT* be
# carried in downstream; this is strictly an upstream/COPR/CI workaround.
%if "%{dist}" == ""
%global dist %{distprefix}.fc%{fedora}
%endif

%if 0%{?fedora}
%global setup_flags -Dvendor=False -Dexamples=True
%else
%global setup_flags -Dvendor=True -Dexamples=True
%endif

%global common_description %{expand:
yggdrasil is a system daemon that subscribes to topics on an MQTT broker and
routes any data received on the topics to an appropriate child "worker" process,
exchanging data with its worker processes through a D-Bus message broker.}

%global golicenses      LICENSE
%global godocs          CONTRIBUTING.md README.md

Name:           yggdrasil
Release:        %{?autorelease}%{!?autorelease:1.%{builddate}git%{shortcommit}%{?dist}}
Summary:        Remote data transmission and processing client

License:        GPL-3.0-only
URL:            %{gourl}
Source:         %{gosource}
Source1:        yggdrasil.sysuser

BuildRequires:  systemd-rpm-macros
BuildRequires:  meson
BuildRequires:  pkgconfig(dbus-1)
BuildRequires:  pkgconfig(systemd)
BuildRequires:  pkgconfig(bash-completion)
BuildRequires:  golang >= 1.18
%{?sysusers_requires_compat}

%description %{common_description}

%package devel
Summary:        %{name} development files

%description devel
%{common_description}

Contains files needed for yggdrasil worker development.

%if %{has_go_rpm_macros}
%gopkg
%endif

%prep
%if %{has_go_rpm_macros}
%goprep %{?rhel:-k}
%else
%autosetup
%endif

%if 0%{?fedora}
%generate_buildrequires
%go_generate_buildrequires
%endif

%build
%undefine _auto_set_build_flags
export %gomodulesmode
%{?gobuilddir:export GOPATH="%{gobuilddir}:${GOPATH:+${GOPATH}:}%{?gopath}"}
%meson %setup_flags "-Dgobuildflags=[%(echo %{expand:%gocompilerflags} | sed -e s/"^"/"'"/ -e s/" "/"', '"/g -e s/"$"/"'"/), '-tags', '"rpm_crashtraceback\ ${BUILDTAGS:-}"', '-a', '-v', '-x']" -Dgoldflags='%{?currentgoldflags} -B 0x%(head -c20 /dev/urandom|od -An -tx1|tr -d " \n") -compressdwarf=false -linkmode=external -extldflags "%{build_ldflags} %{?__golang_extldflags}"'
%meson_build

%global gosupfiles ./ipc/com.redhat.Yggdrasil1.Dispatcher1.xml ./ipc/com.redhat.Yggdrasil1.Worker1.xml
%install
%meson_install
install -p -D -m 0644 %{SOURCE1} %{buildroot}%{_sysusersdir}/%{name}.conf
install -d -m 0755 %{buildroot}%{_localstatedir}/lib/yggdrasil
%if %{has_go_rpm_macros}
%gopkginstall
%endif

%if %{with check}
%check
%if %{has_go_rpm_macros}
%gocheck
%else
%meson_test
%endif
%endif

%pre
%sysusers_create_compat %{SOURCE1}

%post
%systemd_post %{name}.service
%systemd_user_post %{name}.service

%preun
%systemd_preun %{name}.service
%systemd_user_preun %{name}.service

%postun
%systemd_postun_with_restart %{name}.service
%systemd_user_postun_with_restart %{name}.service

%files
%license LICENSE
%doc CONTRIBUTING.md README.md
%{_bindir}/*
%{_libexecdir}/%{name}/*
%config(noreplace) %{_sysconfdir}/%{name}
%{_unitdir}/*
%{_userunitdir}/*
%{_sysusersdir}/*
%{_datadir}/bash-completion/completions/*
%{_datadir}/dbus-1/{interfaces,system-services,system.d}/*
%{_datadir}/doc/%{name}/*
%{_mandir}/man1/*
%attr(0755, yggdrasil, yggdrasil) %{_localstatedir}/lib/yggdrasil

%if %{has_go_rpm_macros}
%gopkgfiles
%endif

%files devel
%{_libdir}/pkgconfig/*.pc