%define debug_package %{nil}
%global crate zram-generator

Name:         rust-%{crate}
Version:      1.1.2
Release:      2
Summary:      Systemd unit generator for zram swap devices

License:        MIT
URL:            https://crates.io/crates/zram-generator
Source:         %{crate}-%{version}.crate
Source1:        zram-generator.conf
Source2:        zram-generator-vendor.tar.gz

BuildRequires:  rust-packaging rubygem-ronn

%global _description %{expand:
This is a systemd unit generator that enables swap on zram.
(With zram, there is no physical swap device. Part of the avaialable RAM
is used to store compressed pages, essentially trading CPU cycles for memory.)
}

%description %{_description}

%package     -n %{crate}
Summary:     %{summary}
Recommends:  %{_sbindir}/zramctl
License:     MIT

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

%package     -n %{crate}-defaults
Summary:        Default configuration for %{crate}
Requires:       %{crate} = %{version}-%{release}
Obsoletes:      zram < 0.4-2
BuildArch:      noarch

%description -n %{crate}-defaults
%{summary}.

%prep
%autosetup -n %{crate}-%{version} -p1
cp -a %{S:1} .
tar zxvf %{SOURCE2}
%cargo_prep

mkdir -p .cargo
cat >.cargo/config << EOF

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

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

%build
export SYSTEMD_UTIL_DIR=%{_systemd_util_dir}
export LC_ALL=C.UTF-8
%cargo_build
%make_build SYSTEMD_SYSTEM_UNIT_DIR=%{_unitdir} SYSTEMD_SYSTEM_GENERATOR_DIR=%{_systemdgeneratordir} \
  systemd-service man

%install
export SYSTEMD_UTIL_DIR=%{_systemd_util_dir}
%cargo_install

mkdir -p %{buildroot}%{?_systemdgeneratordir}
cp -a $PWD/.cargo/bin %{buildroot}/usr/
mv -v %{buildroot}%{_bindir}/zram-generator %{buildroot}%{?_systemdgeneratordir}/
install -Dpm0644 -t %{buildroot}%{?_unitdir} units/systemd-zram-setup@.service
install -Dpm0644 -t %{buildroot}%{_prefix}/lib/systemd %{SOURCE1}
install -Dpm0644 -t %{buildroot}%{_mandir}/man8 man/zram-generator.8
install -Dpm0644 -t %{buildroot}%{_mandir}/man5 man/zram-generator.conf.5
rm -rf  %{buildroot}%{cargo_registry}/%{crate}-%{version}

%files       -n %{crate}
%license LICENSE
%doc zram-generator.conf.example
%doc README.md
%{?_systemdgeneratordir}/zram-generator
%{?_unitdir}/systemd-zram-setup@.service
%{_mandir}/man8/zram-generator.8*
%{_mandir}/man5/zram-generator.conf.5*

%files       -n %{crate}-defaults
%{_prefix}/lib/systemd/zram-generator.conf

%changelog
* Thu Sep 8 2022 duyiwei <duyiwei@kylinos.cn> - 1.1.2-2
- remove useless subpackage zram-generator-devel

* Tue Jul 19 2022 duyiwei <duyiwei@kylinos.cn> - 1.1.2-1
- upgrade to 1.1.2

* Tue May 10 2022 duyiwei <duyiwei@kylinos.cn> - 0.3.2-1
- Package init
