%define debug_package %{nil}
%global oh_version OpenHarmony-v3.2-Release

%global distributedhardware_dir %{_builddir}/foundation/distributedhardware
%global build_opt /opt/distributed-middleware-build
%global systemabilitymgr_dir %{_builddir}/foundation/systemabilitymgr
%global third_party_dir %{_builddir}/third_party

Name:       distributedhardware_device_manager
Version:    1.0.0
Release:    3
Summary:    Distributed middleware used components.
License:    Apache-2.0
Url:        https://gitee.com/openharmony/
Source1: https://gitee.com/openharmony/distributedhardware_device_manager/repository/archive/openHarmony-v3.2-Release.tar.gz #/distributedhardware_device_manager-OpenHarmony-v3.2-Release.tar.gz
Source2: https://gitee.com/openharmony/third_party_json/repository/archive/openHarmony-v3.2-Release.tar.gz #/third_party_json-OpenHarmony-v3.2-Release.tar.gz
Source3: device_manager.bundle.json
Source4: device_manager.BUILD.gn
Source5: device_manager.xml

Patch1: 0001-remove-dependency-and-adapt-for-build-device_manager.patch
Patch2: 0002-device-manager-only-support-for-network.patch

BuildRequires:  gcc, make, hilog, kernel-devel, uname-build-checks
BuildRequires:  distributed-build, distributed-beget
BuildRequires:  commonlibrary_c_utils
BuildRequires:  notification_eventhandler
BuildRequires:  communication_ipc, communication_dsoftbus
BuildRequires:  security_device_auth, security_huks
BuildRequires:  systemabilitymgr_safwk, systemabilitymgr_samgr

Requires:  distributed-beget
Requires:  commonlibrary_c_utils
Requires:  notification_eventhandler
Requires:  communication_ipc, communication_dsoftbus
Requires:  security_device_auth, security_huks
Requires:  systemabilitymgr_safwk, systemabilitymgr_samgr
Requires: libboundscheck

%description
The DeviceManager component provides authentication networking capabilities for account independent distributed devices on OpenHarmony, and provides developers with a set of interfaces for monitoring, discovering, and authenticating between distributed devices.

# Decompress source code package, make patches to the source code.
%prep
rm -rf %{_builddir}/*

cp -rf %{build_opt} %{_builddir}/build
[ ! -L "%{_builddir}/build.sh" ] && ln -s %{_builddir}/build/build_scripts/build.sh %{_builddir}/build.sh
[ ! -L "%{_builddir}/.gn" ] && ln -s %{_builddir}/build/core/gn/dotfile.gn %{_builddir}/.gn
[ ! -L "%{_builddir}/build.py" ] && ln -s %{_builddir}/build/lite/build.py %{_builddir}/build.py
cp -rf %{_builddir}/build/openeuler/vendor %{_builddir}/
cp -rf %{_builddir}/build/openeuler/compiler_gn/* %{_builddir}

%setup -q -D -T -a 1 -c -n %{distributedhardware_dir}/
%patch -P1 -p1 -d %{distributedhardware_dir}/device_manager
%patch -P2 -p1 -d %{distributedhardware_dir}/device_manager

%setup -q -D -T -a 2 -c -n %{third_party_dir}/

%build

%ifarch x86_64
%{_builddir}/build.sh --product-name openeuler --target-cpu x86_64
%endif

%ifarch aarch64
%{_builddir}/build.sh --product-name openeuler --target-cpu arm64
%endif

%install
install -d -m 0755 %{buildroot}/%{_includedir}/device_manager
install -d -m 0755 %{buildroot}/%{_libdir}
install -d -m 0755 %{buildroot}%{_includedir}/nlohmann_json
install -d -m 0755 %{buildroot}%{_includedir}/nlohmann_json/single_include
install -d -m 0755 %{buildroot}%{build_opt}/openeuler/compiler_gn/foundation/distributedhardware/device_manager/interfaces/inner_kits/native_cpp
install -d -m 0755 %{buildroot}/system/lib64
install -d -m 0755 %{buildroot}/system/profile

%ifarch aarch64
%define header_out_path out/openeuler/innerkits/linux-arm64/
%define module_out_path out/openeuler/linux_clang_arm64
%endif
%ifarch x86_64
%define header_out_path out/openeuler/innerkits/linux-x86_64/
%define module_out_path out/openeuler/linux_clang_x86_64
%endif

# prepare so
install -m 0755 %{_builddir}/%{module_out_path}/distributedhardware/device_manager/*.so %{buildroot}/%{_libdir}
install -m 0755 %{_builddir}/%{module_out_path}/distributedhardware/device_manager/*.so %{buildroot}/system/lib64

# prepare head files
find %{_builddir}/%{header_out_path} -name *.h -print0 | xargs -0 -i cp -rf {} %{buildroot}/%{_includedir}/device_manager/

# copy nlohmann_json header file to includedir.
cp -rf %{third_party_dir}/json/include/nlohmann %{buildroot}/%{_includedir}/nlohmann_json
cp -rf %{third_party_dir}/json/single_include/nlohmann %{buildroot}/%{_includedir}/nlohmann_json/single_include

#cp json file
install -m 0755 %{SOURCE3} %{buildroot}/%{build_opt}/openeuler/compiler_gn/foundation/distributedhardware/device_manager/bundle.json

#cp gn file
install -m 0755 %{SOURCE4} %{buildroot}/%{build_opt}/openeuler/compiler_gn/foundation/distributedhardware/device_manager/interfaces/inner_kits/native_cpp/BUILD.gn

#cp sa profile
install -m 0755 %{SOURCE5} %{buildroot}/system/profile

#create soft link
ln -s /usr/include/device_manager %{buildroot}%{build_opt}/openeuler/compiler_gn/foundation/distributedhardware/device_manager/interfaces/inner_kits/native_cpp/include

%files
%{_includedir}/device_manager/*
%{_includedir}/nlohmann_json/*
%{_libdir}/*.so
%{build_opt}/*
/system/*

%changelog
* Tue Mar 26 2024 tianhang <tian_hang@hoperun.com> - 1.0.0-3
- the auto parameter in device manager may cause the softbus to be abnormal

* Fri Dec 15 2023 heppen <hepeng68@huawei.com> - 1.0.0-2
- Add SA profile

* Tue Oct 31 2023 Yuying Mu <muyuying1@huawei.com> - 1.0.0-1
* Tue Nov 21 2023 Jiaqi Zhao <zhaojiaqi18@huawei.com> - 1.0.0-1
- Init and adapt device manager to openEuler

