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

%bcond_without check

%global has_go_rpm_macros (0%{?fedora})

# must be before %%gometa
Version:                0.2.2

# https://github.com/RedHatInsights/rhc-worker-playbook
%global goipath         github.com/redhatinsights/rhc-worker-playbook
%global commit          9ec035921c76e31ee59f69450a89e41c90590ace
%global shortcommit     %(c=%{commit}; echo ${c:0:7})
%global date            %(date "+%Y%m%d")
%global archivename     rhc-worker-playbook-%{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}/%{name}-%{version}.tar.gz
%global gocompilerflags "-buildmode pie -compiler gc"
%global scm git
%forgemeta
%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
%else
%global setup_flags -Dvendor=True
%endif

%global common_description %{expand:
A worker for yggdrasil that receives Ansible playbooks and executes them against
the local host.}

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

Name:               rhc-worker-playbook
Release:            0%{?dist}
Summary:            Ansible playbook yggdrasil worker

License:            GPL-2.0-or-later
URL:                %{gourl}
Source:             %{gosource}

BuildRequires:      ansible-core
BuildRequires:      golang >= 1.18
BuildRequires:      jq
BuildRequires:      meson
BuildRequires:      pkgconfig(dbus-1)
BuildRequires:      pkgconfig(systemd)
BuildRequires:      pkgconfig(yggdrasil) >= 0.4.2
BuildRequires:      python3dist(pip)
BuildRequires:      systemd-rpm-macros
BuildRequires:      python3-rpm-macros

%if %{with check}
BuildRequires:      insights-client
%endif

Requires:           ansible-core

%description        %{common_description}

%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

%install
%meson_install
%py3_shebang_fix \
    %{buildroot}%{_datadir}/rhc-worker-playbook/ansible/collections/ansible_collections/ansible/posix/.azure-pipelines/scripts/*.py \
    %{buildroot}%{_datadir}/rhc-worker-playbook/ansible/collections/ansible_collections/ansible/posix/tests/utils/shippable/*.py \
    %{buildroot}%{_datadir}/rhc-worker-playbook/ansible/collections/ansible_collections/community/general/.azure-pipelines/scripts/*.py \
    %{buildroot}%{_datadir}/rhc-worker-playbook/ansible/collections/ansible_collections/community/general/tests/*/extra/*.py \
    %{buildroot}%{_datadir}/rhc-worker-playbook/ansible/collections/ansible_collections/community/general/tests/integration/targets/django_manage/files/base_test/1045-single-app-project/single_app_project/*.py \
    %{buildroot}%{_datadir}/rhc-worker-playbook/ansible/collections/ansible_collections/community/general/tests/integration/targets/django_manage/files/base_test/simple_project/p1/*.py \
    %{nil}
%{__install} --directory %{buildroot}%{_localstatedir}/lib/%{name}

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

%post
%systemd_post com.redhat.Yggdrasil1.Worker1.rhc_worker_playbook.service

%preun
%systemd_preun com.redhat.Yggdrasil1.Worker1.rhc_worker_playbook.service

%postun
%systemd_postun_with_restart com.redhat.Yggdrasil1.Worker1.rhc_worker_playbook.service

%files
%license LICENSE
%doc CONTRIBUTING.md README.md
%{_libexecdir}/*
%config(noreplace) %{_sysconfdir}/%{name}/%{name}.toml
%{_unitdir}/*
%{_datadir}/dbus-1/{interfaces,system-services,system.d}/*
%{_datadir}/%{name}
%{_libdir}/%{name}
%dir %attr(700, root, yggdrasil-worker) %{_localstatedir}/lib/%{name}