%global __cargo_skip_build 0
%global _debugsource_packages 1
%global _debuginfo_subpackages 1
%define _unpackaged_files_terminate_build 0
%global sysmaster_install_source target/release
%global sysmaster_install_target %{buildroot}/usr/lib/sysmaster
%global unit_install_source units
%global unit_install_target %{sysmaster_install_target}/system
%global conf_install_source config/conf
%global devmaster_install_source target/release
%global devmaster_install_target %{buildroot}/usr/lib/devmaster
%global devmaster_conf_install_source exts/devmaster/config
%global devmaster_conf_install_target %{buildroot}/etc/devmaster
%global __cargo_common_opts %{?__cargo_common_opts} --all
%global _cargo_build /usr/bin/env CARGO_HOME=.cargo RUSTC_BOOTSTRAP=1 %{_bindir}/cargo build %__cargo_common_opts

Name:           sysmaster 
Version:        0.5.1
Release:        6
Summary:        redesign and reimplement process1.

License:        Mulan PSL v2
URL:            https://gitee.com/openeuler/sysmaster
Source0:        %{name}-%{version}.tar.xz

Patch0: backport-fix-getty-generator-delelte-double-quote-in-the-conf.patch
Patch1: backport-fix-solve-two-sysmaster-problem-exit-after-do_execut.patch 
Patch2: backport-fix-solve-reboot-time-consuming.patch
Patch3: backport-feature-add-scsi_id.patch
Patch4: backport-feature-devctl-add-devctl-info.patch
Patch5: backport-fix-devmaster-keep-the-same-name-with-udev-control-s.patch
Patch6: backport-fix-devmaster-adjust-temporary-file-permissions.patch
Patch7: backport-fix-devmaster-append-zero-to-the-file-name.patch
Patch8: backport-fix-devmaster-fix-potential-integer-overflow-in-scsi.patch
Patch9: backport-fix-devmaster-fix-out-of-bounds-in-net_id.patch
Patch10: backport-fix-devmaster-fix-misspelt-sysattr-sas_address.patch
Patch11: backport-fix-devmaster-check-the-validity-of-kmod-context-dur.patch
Patch12: backport-feature-devmaster-Add-subcommands-for-devctl-trigger.patch
Patch13: backport-fix-return-Ok-when-the-.hwdb-and-hwdb.bin-files-cann.patch
Patch14: backport-fix-Failed-to-devctl-info-dev-or-sys.patch
Patch15: backport-fix-basic-complete-feature-dependencies.patch
Patch16: backport-fix-device-replenish-feature-dependency-of-uuid-on-b.patch
Patch17: backport-test-device-refactor-and-increase-test-line-coverage.patch
Patch18: backport-fix-basic-use-feature-to-control-procfs-compilation-.patch
Patch19: backport-test-device-compress-closures-and-add-some-unit-case.patch
Patch20: backport-fix-devmaster-drop-unnecessary-debug-trait-implement.patch
Patch21: backport-fix-device-fix-UT-privilege-error-in-ci.patch
Patch22: backport-fix-device-set-driver-subsystem-after-generating-dev.patch
Patch23: backport-fix-device-drop-unnecessary-error-throwing-out.patch
Patch24: backport-fix-device-cleanup-temporary-tag-files.patch
Patch25: backport-test-device-add-UT-for-device-enumerator.patch
Patch26: backport-test-device-add-UT-for-device-monitor.patch
Patch27: backport-feature-devmaster-add-devctl-control-subcommand-to-l.patch
Patch28: backport-refactor-devmaster-compress-unnecessary-code.patch
Patch29: backport-test-devmaster-optimize-UT-for-testing-framework-and.patch
Patch30: backport-test-devmaster-test-exception-scenarios-on-parsing-r.patch
Patch31: backport-fix-devmaster-cache-the-parsed-user-and-group.patch
Patch32: backport-fix-devmaster-drop-incorrect-preventation-on-removal.patch
Patch33: backport-fix-devmaster-CONST-can-only-take-arch-or-virt-as-at.patch
Patch34: backport-test-devmaster-add-UT-cases-for-rules-load.patch
Patch35: backport-test-devmaster-trigger-add-and-remove-uevent-to-cove.patch
Patch36: backport-fix-device-use-feature-to-avoid-loopdev-being-compil.patch
Patch37: backport-fix-devmaster-avoid-potential-test-case-failures.patch
Patch38: backport-refactor-devmaster-rename-the-create_tmp_rules-funct.patch
Patch39: backport-fix-device-keep-consistent-on-the-base-path-for-db-c.patch
Patch40: backport-test-devmaster-add-UT-for-rules-applying.patch
Patch41: backport-fix-device-avoid-panic-on-shallow-clone-when-subsyst.patch
Patch42: backport-test-devmaster-use-unwrap-instead-of-asserting-is-ok.patch
Patch43: backport-test-devmaster-add-UT-for-execute-unit.patch
Patch44: backport-test-devmaster-add-UT-for-rules-module.patch
Patch45: backport-test-devmaster-add-UT-for-node-and-modify-the-displa.patch

ExclusiveArch:  x86_64 aarch64

BuildRequires:  rust cargo rust-packaging
BuildRequires:  gcc clang openssl-libs

%description
redesign and reimplement process1.

Summary:        %{summary}

%package -n devmaster
Summary:        Infrastructure of device management in userspace.
BuildRequires:  util-linux-devel kmod-devel
Requires:       %{name}%{?_isa} = %{version}-%{release}
Requires(post):   sysmaster
Requires(preun):  sysmaster
Requires(postun): sysmaster

%description -n devmaster
This package provides the infrastructure of device management in userspace.

%prep
%autosetup -p1

%cargo_generate_buildrequires

%build
cat << EOF >> ./.cargo/config

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

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


%{_cargo_build} --profile release

%install
# For binary files and .so files, the permission 750 in the install phase to prevent objcopy errors.
# In the files phase, the permission is set back to 550.
install -Dm0750 -t %{buildroot}/usr/bin %{sysmaster_install_source}/sctl
install -Dm0750 -t %{sysmaster_install_target} %{sysmaster_install_source}/init
install -Dm0750 -t %{sysmaster_install_target} %{sysmaster_install_source}/sysmaster
install -Dm0750 -t %{sysmaster_install_target} %{sysmaster_install_source}/fstab
install -Dm0750 -t %{sysmaster_install_target} %{sysmaster_install_source}/sysmonitor
install -Dm0750 -t %{sysmaster_install_target} %{sysmaster_install_source}/random_seed
install -Dm0750 -t %{sysmaster_install_target} %{sysmaster_install_source}/rc-local-generator
install -Dm0750 -t %{sysmaster_install_target} %{sysmaster_install_source}/hostname_setup
install -Dm0750 -t %{sysmaster_install_target}/system-generators %{sysmaster_install_source}/getty-generator

install -Dm0640 -t %{unit_install_target} %{unit_install_source}/*

install -Dm0640 -t %{buildroot}/etc/sysmaster %{conf_install_source}/system.conf

install -Dm0750 -t %{buildroot}/usr/bin %{devmaster_install_source}/devctl
install -Dm0550 -t %{devmaster_install_target} tools/run_with_devmaster/simulate_udev.sh
install -Dm0640 -t %{devmaster_conf_install_target} %{devmaster_conf_install_source}/config.toml
install -Dm0640 -t %{devmaster_conf_install_target}/rules.d %{devmaster_conf_install_source}/rules.d/*
install -Dm0640 -t %{devmaster_conf_install_target}/network.d %{devmaster_conf_install_source}/network.d/*

mkdir -p %{buildroot}/etc/sysmaster/system/multi-user.target.wants
mkdir -p %{buildroot}/etc/sysmaster/system/sysinit.target.wants
mkdir -p %{devmaster_install_target}

ln -s /usr/bin/devctl %{devmaster_install_target}/devmaster
for unit in NetworkManager.service dbus.service dbus.socket fstab.service hostname-setup.service udevd-control.socket udevd-kernel.socket; do
    install -Dm0640 -t %{unit_install_target} tools/run_with_vm/$unit
    # enble service for booting
    if [[ "$unit" == *".service" ]]; then
        ln -s /usr/lib/sysmaster/system/$unit %{buildroot}/etc/sysmaster/system/multi-user.target.wants/$unit
    fi
done

for unit in udevd.service udev-trigger.service; do
    install -Dm0640 -t %{unit_install_target} tools/run_with_vm/$unit
    if [[ "$unit" == *".service" ]]; then
        ln -s /usr/lib/sysmaster/system/$unit %{buildroot}/etc/sysmaster/system/sysinit.target.wants/$unit
    fi
done

for unit in devmaster.service devctl-trigger.service; do
    install -Dm0640 -t %{unit_install_target} tools/run_with_devmaster/service/$unit
done

ln -s /usr/lib/sysmaster/system/devmaster.service %{buildroot}/etc/sysmaster/system/sysinit.target.wants/devmaster.service
ln -s /usr/lib/sysmaster/system/devctl-trigger.service %{buildroot}/etc/sysmaster/system/multi-user.target.wants/devctl-trigger.service

install -Dm0640 -t %{unit_install_target} tools/run_with_devmaster/service/*.service
install -Dm0640 -t %{unit_install_target} tools/run_with_vm/getty.target
ln -s /usr/lib/sysmaster/system//getty.target %{buildroot}/etc/sysmaster/system/multi-user.target.wants/getty.target
install -Dm0640 -t %{unit_install_target} tools/run_with_vm/getty@.service
install -Dm0640 -t %{unit_install_target} tools/run_with_vm/serial-getty@.service

# Install compatible rules for lvm
install -Dm444 -t %{buildroot}/usr/lib/udev/rules.d rules/99-sysmaster.rules

# Install configurations under /etc.
install -Dm0640 -t %{devmaster_conf_install_target} %{devmaster_conf_install_source}/config.toml
sed -i 's/\"\/lib\/devmaster\/rules.d\"/&, \"\/etc\/udev\/rules.d\", \"\/run\/udev\/rules.d\", \"\/lib\/udev\/rules.d\"/' %{devmaster_conf_install_target}/config.toml

# enable sshd service by default
ln -s /usr/lib/sysmaster/system/sshd.service %{buildroot}/etc/sysmaster/system/multi-user.target.wants/sshd.service

%files
%attr(0550,-,-) /usr/bin/sctl
%dir %attr(0550,-,-) /usr/lib/sysmaster
%dir %attr(0750,-,-) /usr/lib/sysmaster/system
/usr/lib/sysmaster/system/*
%attr(0550,-,-) /usr/lib/sysmaster/init
%attr(0550,-,-) /usr/lib/sysmaster/fstab
%attr(0550,-,-) /usr/lib/sysmaster/sysmonitor
%attr(0550,-,-) /usr/lib/sysmaster/random_seed
%attr(0550,-,-) /usr/lib/sysmaster/rc-local-generator
%attr(0550,-,-) /usr/lib/sysmaster/system-generators/getty-generator
%attr(0550,-,-) /usr/lib/sysmaster/hostname_setup
%attr(0550,-,-) /usr/lib/sysmaster/sysmaster
%dir %attr(0750,-,-) /etc/sysmaster
%dir %attr(0750,-,-) /etc/sysmaster/system
%dir %attr(0750,-,-) /etc/sysmaster/system/multi-user.target.wants
%dir %attr(0750,-,-) /etc/sysmaster/system/sysinit.target.wants
/etc/sysmaster/system/multi-user.target.wants/*
/etc/sysmaster/system/sysinit.target.wants/*
/etc/sysmaster/system.conf
/usr/lib/udev/rules.d/99-sysmaster.rules
%exclude /usr/lib/sysmaster/system/devctl-trigger.service
%exclude /usr/lib/sysmaster/system/devmaster-simu-udev.service
%exclude /usr/lib/sysmaster/system/devmaster.service
%exclude /etc/sysmaster/system/sysinit.target.wants/devmaster.service
%exclude /etc/sysmaster/system/multi-user.target.wants/devctl-trigger.service

%files -n devmaster
%dir %attr(0550,-,-) /usr/lib/devmaster
%dir %attr(0750,-,-) /etc/devmaster
/etc/devmaster/config.toml
%dir %attr(0750,-,-) /etc/devmaster/rules.d
/etc/devmaster/rules.d/99-default.rules
%dir %attr(0750,-,-) /etc/devmaster/network.d
/etc/devmaster/network.d/99-default.link
%attr(0550,-,-) /usr/bin/devctl
%attr(0550,-,-) /usr/lib/devmaster/devmaster
/usr/lib/sysmaster/system/devctl-trigger.service
/usr/lib/sysmaster/system/devmaster-simu-udev.service
/usr/lib/sysmaster/system/devmaster.service
/usr/lib/devmaster/simulate_udev.sh
/etc/sysmaster/system/sysinit.target.wants/devmaster.service
/etc/sysmaster/system/multi-user.target.wants/devctl-trigger.service

%post -n devmaster
test -f /etc/sysmaster/system/sysinit.target.wants/udevd.service && unlink /etc/sysmaster/system/sysinit.target.wants/udevd.service || :
test -f /etc/sysmaster/system/sysinit.target.wants/udev-trigger.service && unlink /etc/sysmaster/system/sysinit.target.wants/udev-trigger.service || :

%postun -n devmaster
test -f /usr/lib/sysmaster/system/udevd.service && ln -s /usr/lib/sysmaster/system/udevd.service /etc/sysmaster/system/sysinit.target.wants/udevd.service || :
test -f /usr/lib/sysmaster/system/udev-trigger.service && ln -s /usr/lib/sysmaster/system/udev-trigger.service /etc/sysmaster/system/sysinit.target.wants/udev-trigger.service || :

%changelog
* Mon Dec 11 2023 chenjiayi<chenjiayi22@huawei.com> - 0.5.1-6
- fix incorrect symbolic linkage location of devctl-trigger.service

* Thu Dec 7 2023 chenjiayi<chenjiayi22@huawei.com> - 0.5.1-5
- complement dependency from devmaster to sysmaster and avoid
  exit with failure in post and postun process

* Wed Dec 6 2023 chenjiayi<chenjiayi22@huawei.com> - 0.5.1-4
- sync patches from upstream

* Fri Nov 24 2023 zhangyao<zhangyao108@huawei.com> - 0.5.1-3
- sync patches from upstream

* Mon Oct 30 2023 zhangyao<zhangyao108@huawei.com> - 0.5.1-2
- add simulate_udev.sh file

* Mon Oct 30 2023 zhangyao<zhangyao108@huawei.com> - 0.5.1-1
- upgrade version to 0.5.1

* Fri Sep 01 2023 licunlong<licunlong1@huawei.com> - 0.2.5-4
- sync patchs from upstream

* Thu Aug 31 2023 licunlong<licunlong1@huawei.com> - 0.2.5-3
- log the status message to stdout

* Fri Aug 25 2023 licunlong<licunlong1@huawei.com> - 0.2.5-2
- sync patches from upstream

* Sat Aug 5 2023 shenyangyang<shenyangyang4@huawei.com> - 0.2.5-1
- update for compiling with rust 1.57

* Thu Jul 27 2023 huyubiao<huyubiao@huawei.com> - 0.2.4-5
- sync patches from upstream,
  change the path of the unit,
  modify permissions for some directories and files

* Wed Jul 19 2023 shenyangyang<shenyangyang4@huawei.com> - 0.2.4-4
- add compile option

* Thu Jul 06 2023 xujing<xujing125@huawei.com> - 0.2.4-3
- fix objcopy permission denied when rpmbuild

* Tue Jun 27 2023 shenyangyang<shenyangyang4@huawei.com> - 0.2.4-2
- modify the sshd units

* Tue Jun 20 2023 shenyangyang<shenyangyang4@huawei.com> - 0.2.4-1
- update version to 0.2.4 for docker use

* Fri Jun 16 2023 licunlong<licunlong1@huawei.com> - 0.2.3-3
- sync patches from upstream

* Tue May 30 2023 shenyangyang<shenyangyang4@huawei.com> - 0.2.3-2
- Support compatible compile with rust 1.60

* Sat May 6 2023 shenyangyang<shenyangyang4@huawei.com> - 0.2.3-1
- update version to 0.2.3

* Tue Sep 20 2022 licunlong<licunlong1@huawei.com> - 0.2.1-2
- rename process1 to sysmaster, and remove pctrl to /usr/bin

* Tue Sep 13 2022 licunlong<licunlong1@huawei.com> - 0.2.1-1
- sync patches from upstream

* Mon Aug 22 2022 He Xiaowen <hexiaowen@huawei.com> - 0.2.0-2
- strip the libraries

* Mon Aug 22 2022 He Xiaowen <hexiaowen@huawei.com> - 0.2.0-1
- initial package

