%bcond_without check
%global __cargo_skip_build 0
%define debug_package %{nil}
%global crate zincati
%global zcuid 986

Name:           rust-%{crate}
Version:        0.0.24
Release:        2
Summary:        Update agent for NestOS
License:        ASL 2.0
URL:            https://crates.io/crates/%{crate}
Source0:        %{crate}-%{version}.crate
Source1:        https://github.com/coreos/%{crate}/releases/download/v%{version}/%{crate}-%{version}-vendor.tar.gz
Source2:        zincati-systemd-sysusers.conf
Patch1:         base-cincinnati-url-for-nestos.patch
Patch2:         modify-as-nestos-stream.patch

BuildRequires:  rust-packaging
BuildRequires:  systemd openssl-devel

Requires:       polkit

%global _description %{expand:
Update agent for NestOS.}

%description %{_description}

%package     -n %{crate}
Summary:        %{summary}
License:        ASL 2.0 and BSD and MIT and MPLv2.0 and zlib

%description -n %{crate} %{_description}

%files       -n %{crate}
%{_libexecdir}/zincati
%doc README.md
%license COPYRIGHT LICENSE
%dir %{_prefix}/lib/%{crate}
%dir %{_prefix}/lib/%{crate}/config.d
%{_prefix}/lib/%{crate}/config.d/*.toml
%attr(0775, zincati, zincati) %dir /run/%{crate}
%attr(0775, zincati, zincati) %dir /run/%{crate}/config.d
%attr(0770, zincati, zincati) %dir /run/%{crate}/private
%attr(0775, zincati, zincati) %dir /run/%{crate}/public
%verify(not size mtime md5) /run/%{crate}/public/metrics.promsock
%verify(not size mtime md5) /run/%{crate}/private/metrics.promsock
%dir %{_sysconfdir}/%{crate}
%dir %{_sysconfdir}/%{crate}/config.d
%{_unitdir}/zincati.service
%{_sysusersdir}/50-zincati.conf
%{_tmpfilesdir}/zincati.conf
%{_datadir}/polkit-1/rules.d/zincati.rules
%{_datadir}/polkit-1/actions/org.coreos.zincati.*
%{_sysusersdir}/zincati.conf

%pre -n %{crate}
%sysusers_create_package %{crate} %{SOURCE2}

%post        -n %{crate}
%systemd_post zincati.service

%preun       -n %{crate}
%systemd_preun zincati.service

%postun      -n %{crate}
%systemd_postun_with_restart zincati.service

%prep
%autosetup -n %{crate}-%{version} -p1
%cargo_prep
tar xvf %{SOURCE1}
mkdir -p .cargo
cat >.cargo/config << EOF

[build]
rustc = "%{__rustc}"
rustdoc = "%{__rustdoc}"
rustflags = %{__global_rustflags_toml}

[install]
root = "%{buildroot}%{_prefix}"

[term]
verbose = true

[source.crates-io]
replace-with = "vendored-sources"

[source.vendored-sources]
directory = "vendor"
EOF

%build
%cargo_build

%install
%cargo_install
# `zincati` should not be executed directly by users, so we move the binary
# out of `/usr/bin`. See: https://github.com/coreos/fedora-coreos-tracker/issues/244
mkdir -p %{buildroot}%{_libexecdir}
mv %{buildroot}%{_bindir}/zincati %{buildroot}%{_libexecdir}/zincati
install -Dpm0644 -t %{buildroot}%{_prefix}/lib/%{crate}/config.d \
  dist/config.d/*.toml
mkdir -p %{buildroot}/run/%{crate}/config.d
mkdir -p %{buildroot}/run/%{crate}/private
mkdir -p %{buildroot}/run/%{crate}/public
touch %{buildroot}/run/%{crate}/public/metrics.promsock
mkdir -p %{buildroot}%{_sysconfdir}/%{crate}/config.d
install -Dpm0644 -t %{buildroot}%{_unitdir} \
  dist/systemd/system/*.service
install -Dpm0644 -t %{buildroot}%{_sysusersdir} \
  dist/sysusers.d/*.conf
install -Dpm0644 -t %{buildroot}%{_tmpfilesdir} \
  dist/tmpfiles.d/*.conf
install -Dpm0644 -t %{buildroot}%{_datadir}/polkit-1/rules.d \
  dist/polkit-1/rules.d/*.rules
install -Dpm0644 -t %{buildroot}%{_datadir}/polkit-1/actions \
  dist/polkit-1/actions/org.coreos.zincati.*
ln -snf /run/%{crate}/public/metrics.promsock %{buildroot}/run/%{crate}/private/metrics.promsock

install -Dpm644 %{SOURCE2} $RPM_BUILD_ROOT%{_sysusersdir}/zincati.conf

%if %{with check}
%check
%cargo_test
%endif

%changelog
* Thu Nov 10 2022 duyiwei <duyiwei@kylinos.cn> - 0.0.24-2
- sync master and fix some bug

* Wed Jun 08 2022 fushanqing <fushanqing@kylinos.cn> - 0.0.24-1
- update to 0.0.24

* Mon May 16 2022 duyiwei <duyiwei@kylinos.cn> - 0.0.18-2
- add base-cincinnati-url-for-nestos.patch

* Mon Dec 27 2021 duyiwei <duyiwei@kylinos.cn> - 0.0.18-1
- Package init
