Name:          kae
Summary:       Huawei Kunpeng Accelerator Engine Zip
Version:       2.0.0
Release:       3
License:       GPL-2.0
Source:        %{name}-%{version}.tar.gz
ExclusiveOS:   linux
BuildRoot:     %{_tmppath}/%{name}-%{version}-root
Conflicts:     %{name} < %{version}-%{release}
Provides:      %{name} = %{version}-%{release}
BuildRequires: gcc, make, kernel-devel, libtool, numactl-devel, openssl-devel
ExclusiveArch: aarch64
Autoreq: no
Autoprov: no

%define kernel_version %(rpm -q kernel-devel | sed 's/kernel-devel-//')
%define kae_build_path  %{_builddir}/%{name}-%{version}/%{name}-%{version}/kae_build
%define kae_path  %{_builddir}/%{name}-%{version}/%{name}-%{version}
%define kae_driver_path  %{_builddir}/%{name}-%{version}/%{name}-%{version}/KAEKernelDriver
%define kae_uadk_path  %{_builddir}/%{name}-%{version}/%{name}-%{version}/uadk
%define zlib_version 1.2.11
%define zstd_version 1.5.2

%description
This package contains the Huawei Hisilicon Zip Accelerator Engine.

%prep
%global debug_package %{nil}
%setup -c -n %{name}-%{version}

%build


#%install
mkdir -p ${RPM_BUILD_ROOT}/lib/modules/%{kernel_version}/extra
mkdir -p ${RPM_BUILD_ROOT}/etc/modprobe.d

mkdir -p ${RPM_BUILD_ROOT}/usr/lib64

mkdir -p ${RPM_BUILD_ROOT}/usr/include/uadk
mkdir -p ${RPM_BUILD_ROOT}/usr/include/uadk/drv

mkdir -p ${RPM_BUILD_ROOT}/usr/local/kaezip/lib
mkdir -p ${RPM_BUILD_ROOT}/usr/local/kaezip/include
mkdir -p ${RPM_BUILD_ROOT}/usr/local/kaezip/lib/pkgconfig
mkdir -p ${RPM_BUILD_ROOT}/usr/local/kaezip/share/man/man3

mkdir -p ${RPM_BUILD_ROOT}/usr/local/kaezstd/lib
mkdir -p ${RPM_BUILD_ROOT}/usr/local/kaezstd/bin
mkdir -p ${RPM_BUILD_ROOT}/usr/local/kaezstd/include
mkdir -p ${RPM_BUILD_ROOT}/usr/local/kaezstd/lib/pkgconfig
mkdir -p ${RPM_BUILD_ROOT}/usr/local/kaezstd/share/man/man3


mkdir -p ${RPM_BUILD_ROOT}/usr/local/lib/engines-1.1


%clean
rm -rf ${RPM_BUILD_ROOT}

%package driver
Summary: KAE Driver Package
Autoreq: no
Autoprov: no

%description driver
This package kae_driver library.

%files driver
%defattr(644,root,root)

%defattr(755,root,root)

%pre driver
if [ "$1" = "2" ] ; then  #2: update
    rm -rf /usr/lib64/libwd.so   > /dev/null 2>&1 || true
    rm -rf /usr/lib64/libwd.so.1   > /dev/null 2>&1 || true

    rm -rf /usr/lib64/libwd_comp.so   > /dev/null 2>&1 || true
    rm -rf /usr/lib64/libwd_comp.so.1   > /dev/null 2>&1 || true

    rm -rf /usr/lib64/libwd_crypto.so   > /dev/null 2>&1 || true
    rm -rf /usr/lib64/libwd_crypto.so.1   > /dev/null 2>&1 || true

    rm -rf /usr/lib64/libhisi_hpre.so   > /dev/null 2>&1 || true
    rm -rf /usr/lib64/libhisi_hpre.so.1   > /dev/null 2>&1 || true

    rm -rf /usr/lib64/libhisi_sec.so   > /dev/null 2>&1 || true
    rm -rf /usr/lib64/libhisi_sec.so.1   > /dev/null 2>&1 || true

    rm -rf /usr/lib64/libhisi_zip.so   > /dev/null 2>&1 || true
    rm -rf /usr/lib64/libhisi_zip.so.1   > /dev/null 2>&1 || true
fi

echo "checking installed modules"
if [[ "$1" = "1" || "$1" = "2" ]] ; then  #1: install 2: update
    echo "uacce modules start to install"
fi

echo "checking installed modules"
if [[ "$1" = "1" || "$1" = "2" ]] ; then  #1: install 2: update
    echo "hisi_sec2 modules start to install"
fi

echo "checking installed modules"
if [[ "$1" = "1" || "$1" = "2" ]] ; then  #1: install 2: update
    echo "hisi_hpre modules start to install"
fi

echo "checking installed modules"
if [[ "$1" = "1" || "$1" = "2" ]] ; then  #1: install 2: update
    echo "hisi_zip modules start to install"
fi

%post driver
echo "installing driver..."

/sbin/ldconfig

if [[ "$1" = "1" || "$1" = "2" ]] ; then  #1: install 2: update
    if [ -e /sbin/weak-modules ]; then
        echo "/lib/modules/%{kernel_version}/extra/uacce.ko" | /sbin/weak-modules --add-module --no-initramfs
        echo "/lib/modules/%{kernel_version}/extra/hisi_qm.ko" | /sbin/weak-modules --add-module --no-initramfs
    fi
    /sbin/depmod -a > /dev/null 2>&1 || true
fi
echo "uacce modules installed"

if [[ "$1" = "1" || "$1" = "2" ]] ; then  #1: install 2: update
    if [ -e /sbin/weak-modules ]; then
        echo "/lib/modules/%{kernel_version}/extra/hisi_sec2.ko" | /sbin/weak-modules --add-module --no-initramfs
    fi
    /sbin/depmod -a > /dev/null 2>&1 || true
fi
echo "hisi_sec2 modules installed"

if [[ "$1" = "1" || "$1" = "2" ]] ; then  #1: install 2: update
    if [ -e /sbin/weak-modules ]; then
        echo "/lib/modules/%{kernel_version}/extra/hisi_hpre.ko" | /sbin/weak-modules --add-module --no-initramfs
    fi
    /sbin/depmod -a > /dev/null 2>&1 || true
fi
echo "hisi_hpre modules installed"

if [[ "$1" = "1" || "$1" = "2" ]] ; then  #1: install 2: update
    if [ -e /sbin/weak-modules ]; then
        echo "/lib/modules/%{kernel_version}/extra/hisi_zip.ko" | /sbin/weak-modules --add-module --no-initramfs
    fi
    /sbin/depmod -a > /dev/null 2>&1 || true
fi
echo "hisi_zip modules installed"

%preun driver
if [ "$1" = "0" ] ; then  #0: uninstall
    rm -rf /usr/lib64/libwd.so    > /dev/null 2>&1 || true
    rm -rf /usr/lib64/libwd.so.1    > /dev/null 2>&1 || true

    rm -rf /usr/lib64/libwd_comp.so    > /dev/null 2>&1 || true
    rm -rf /usr/lib64/libwd_comp.so.1    > /dev/null 2>&1 || true

    rm -rf /usr/lib64/libwd_crypto.so    > /dev/null 2>&1 || true
    rm -rf /usr/lib64/libwd_crypto.so.1    > /dev/null 2>&1 || true

    rm -rf /usr/lib64/libhisi_hpre.so    > /dev/null 2>&1 || true
    rm -rf /usr/lib64/libhisi_hpre.so.1    > /dev/null 2>&1 || true

    rm -rf /usr/lib64/libhisi_sec.so    > /dev/null 2>&1 || true
    rm -rf /usr/lib64/libhisi_sec.so.1    > /dev/null 2>&1 || true

    rm -rf /usr/lib64/libhisi_zip.so    > /dev/null 2>&1 || true
    rm -rf /usr/lib64/libhisi_zip.so.1    > /dev/null 2>&1 || true
fi

if [ -e /sbin/weak-modules ]; then
    echo "/lib/modules/%{kernel_version}/extra/uacce.ko" | /sbin/weak-modules --remove-module --no-initramfs
    echo "/lib/modules/%{kernel_version}/extra/hisi_qm.ko" | /sbin/weak-modules --remove-module --no-initramfs
fi
/sbin/depmod -a > /dev/null 2>&1 || true
if [ "$1" = "0" ] ; then  #0: uninstall
    echo "uacce modules uninstalling"
fi

if [ -e /sbin/weak-modules ]; then
    echo "/lib/modules/%{kernel_version}/extra/hisi_sec2.ko" | /sbin/weak-modules --remove-module --no-initramfs
fi
if [ "$1" = "0" ] ; then  #0: uninstall
    echo "hisi_sec2 modules uninstalling"
fi

if [ -e /sbin/weak-modules ]; then
    echo "/lib/modules/%{kernel_version}/extra/hisi_hpre.ko" | /sbin/weak-modules --remove-module --no-initramfs
fi
if [ "$1" = "0" ] ; then  #0: uninstall
    echo "hisi_hpre modules uninstalling"
fi

if [ -e /sbin/weak-modules ]; then
    echo "/lib/modules/%{kernel_version}/extra/hisi_zip.ko" | /sbin/weak-modules --remove-module --no-initramfs
fi
if [ "$1" = "0" ] ; then  #0: uninstall
    echo "hisi_zip modules uninstalling"
fi

%postun driver
/sbin/ldconfig

if [ "$1" = "0" ] ; then  #0: uninstall
    /sbin/depmod -a > /dev/null 2>&1 || true
fi
echo "uacce modules uninstalled"

if [ "$1" = "0" ] ; then  #0: uninstall
    if [ -e /sbin/weak-modules ]; then
        echo "/lib/modules/%{kernel_version}/extra/hisi_sec2.ko" | /sbin/weak-modules --remove-module --no-initramfs
    fi
    /sbin/depmod -a > /dev/null 2>&1 || true
fi
echo "hisi_sec2 modules uninstalled"

if [ "$1" = "0" ] ; then  #0: uninstall
    if [ -e /sbin/weak-modules ]; then
        echo "/lib/modules/%{kernel_version}/extra/hisi_hpre.ko" | /sbin/weak-modules --remove-module --no-initramfs
    fi
    /sbin/depmod -a > /dev/null 2>&1 || true
fi
echo "hisi_hpre modules uninstalled"

if [ "$1" = "0" ]; then  #0: uninstall
    if [ -e /sbin/weak-modules ]; then
        echo "/lib/modules/%{kernel_version}/extra/hisi_zip.ko" | /sbin/weak-modules --remove-module --no-initramfs
    fi
    /sbin/depmod -a > /dev/null 2>&1 || true
fi
echo "hisi_zip modules uninstalled"


%package zip
Summary: KAE Zip Package
Requires:kae-driver
Autoreq: no
Autoprov: no

%description zip
This package kaezip library.

%files zip
%defattr(755,root,root)

%defattr(644,root,root)


%defattr(755,root,root)

%defattr(644,root,root)



%pre zip
echo "installing pre zip..."
if [ "$1" = "2" ] ; then  #2: update
    rm -rf /usr/local/kaezip/lib/libkaezip.so     > /dev/null 2>&1 || true
    rm -rf /usr/local/kaezip/lib/libkaezip.so.0    > /dev/null 2>&1 || true
    rm -rf /usr/local/kaezip/lib/libz.so     > /dev/null 2>&1 || true
    rm -rf /usr/local/kaezip/lib/libz.so.1     > /dev/null 2>&1 || true

    rm -rf /usr/local/kaezstd/lib/libkaezstd.so     > /dev/null 2>&1 || true
    rm -rf /usr/local/kaezstd/lib/libkaezstd.so.0    > /dev/null 2>&1 || true
    rm -rf /usr/local/kaezstd/lib/libzstd.so     > /dev/null 2>&1 || true
    rm -rf /usr/local/kaezstd/lib/libzstd.so.1     > /dev/null 2>&1 || true

    rm -rf /usr/local/kaezstd/bin/unzstd > /dev/null 2>&1 || true
    rm -rf /usr/local/kaezstd/bin/zstdcat > /dev/null 2>&1 || true
    rm -rf /usr/local/kaezstd/bin/zstdmt > /dev/null 2>&1 || true
fi

%post zip
echo "installing post zip..."

/sbin/ldconfig

%preun zip
if [ "$1" = "0" ] ; then  #0: uninstall
    rm -rf /usr/local/kaezip/lib/libz.so   > /dev/null 2>&1 || true
    rm -rf /usr/local/kaezip/lib/libz.so.1   > /dev/null 2>&1 || true
    rm -rf /usr/local/kaezip/lib/libkaezip.so   > /dev/null 2>&1 || true
    rm -rf /usr/local/kaezip/lib/libkaezip.so.0 > /dev/null 2>&1 || true

    rm -rf /usr/local/kaezstd/lib/libzstd.so   > /dev/null 2>&1 || true
    rm -rf /usr/local/kaezstd/lib/libzstd.so.1   > /dev/null 2>&1 || true
    rm -rf /usr/local/kaezstd/lib/libkaezstd.so   > /dev/null 2>&1 || true
    rm -rf /usr/local/kaezstd/lib/libkaezstd.so.0 > /dev/null 2>&1 || true

    rm -rf /usr/local/kaezstd/bin/unzstd > /dev/null 2>&1 || true
    rm -rf /usr/local/kaezstd/bin/zstdcat > /dev/null 2>&1 || true
    rm -rf /usr/local/kaezstd/bin/zstdmt > /dev/null 2>&1 || true

    rm -f /var/log/kaezip.log              > /dev/null 2>&1 || true
    rm -f /var/log/kaezip.log.old          > /dev/null 2>&1 || true
fi

%postun zip
/sbin/ldconfig

%package openssl
Summary: KAE Openssl Package
Requires:kae-driver, openssl-devel
Autoreq: no
Autoprov: no

%description openssl
This package kae_openssl library.

%files openssl
%defattr(755,root,root)

%pre openssl
if [ "$1" = "2" ] ; then  #2: update
    rm -rf $RPM_INSTALL_PREFIX/kae.so      > /dev/null 2>&1 || true
    rm -rf $RPM_INSTALL_PREFIX/kae.so.0    > /dev/null 2>&1 || true
fi

%post openssl
echo "installing openssl..."
if [[ "$1" = "1" || "$1" = "2" ]] ; then  #1: install 2: update
    ln -sf $RPM_INSTALL_PREFIX/kae.so.%{version}    $RPM_INSTALL_PREFIX/kae.so
    ln -sf $RPM_INSTALL_PREFIX/kae.so.%{version}    $RPM_INSTALL_PREFIX/kae.so.0
fi
/sbin/ldconfig

%preun openssl
if [ "$1" = "0" ] ; then  #0: uninstall
    rm -rf $RPM_INSTALL_PREFIX/kae.so   > /dev/null 2>&1 || true
    rm -rf $RPM_INSTALL_PREFIX/kae.so.0 > /dev/null 2>&1 || true
    rm -f /var/log/kae.log              > /dev/null 2>&1 || true
    rm -f /var/log/kae.log.old          > /dev/null 2>&1 || true
fi


%postun openssl
/sbin/ldconfig



%changelog
* Thu Jun 29 2023 liuyang <liuyang645@huawei.com> 2.0.0-3
- Update KAE version

* Tue Jan 07 2020 jinbinhua <jinbinhua@huawei.com> 1.2.7-1
- First Spec Version Include kunpeng accelerator engine Code
