%global _hardened_build 1
#%global complete_test_arches x86-64
%global patches_touch_autotools %{nil}

Name:           nbdkit
Version:        1.29.11
Release:        1
Summary:        NBD server
License:        BSD
URL:            https://github.com/libguestfs/nbdkit
Source0:        https://download.libguestfs.org/nbdkit/1.29-development/nbdkit-%{version}.tar.gz
Source1:        https://download.libguestfs.org/nbdkit/1.29-development/nbdkit-%{version}.tar.gz.sig
Source2:        libguestfs.keyring
%if 0%{patches_touch_autotools}
BuildRequires:  autoconf, automake, libtool
%endif
%ifnarch %{complete_test_arches}
BuildRequires:  autoconf, automake, libtool
%endif
BuildRequires:  gnutls gnutls-devel libselinux-devel python3-devel gnupg2 lua-devel socat iproute
BuildRequires:  libguestfs-devel libvirt-devel xz-devel zlib-devel libcurl-devel e2fsprogs-devel
BuildRequires:  bash-completion perl-devel perl(ExtUtils::Embed) ocaml >= 4.02.2
BuildRequires:  ruby-devel tcl-devel perl-podlators qemu-img
Requires:       nbdkit-server nbdkit-basic-plugins nbdkit-basic-filters

%description
NBD (Network Block Device) is a protocol for accessing Block Devices (hard disks and disk-like things)
over a Network. nbdkit is a toolkit for creating NBD servers.
The key features are:
 * Multithreaded NBD server written in C with good performance.
 * Minimal dependencies for the basic server.
 * Liberal license (BSD) allows nbdkit to be linked to proprietary libraries or included in proprietary code.
 * Well-documented, simple plugin API with a stable ABI guarantee. Lets you export “unconventional”
   block devices easily.
 * You can write plugins in C, Lua, Perl, Python, OCaml, Ruby, Rust, shell script or Tcl.
 * Filters can be stacked in front of plugins to transform the output.

%package        server
Summary:        The nbdkit server
License:        BSD

%description    server
This package contains the nbdkit server with no plugins or filters.

%package        basic-plugins
Summary:        Basic plugins for nbdkit
License:        BSD
Requires:       nbdkit-server = %{version}-%{release}
Obsoletes:      nbdkit-plugin-file < 1.1.19-1 nbdkit-plugin-nbd < 1.1.19-1 nbdkit-plugin-streaming < 1.1.19-1

%description    basic-plugins
This package contains some basic plugins for nbdkit which have only
trivial dependencies.
* nbdkit-data-plugin
  Serve small files directly from the command line.
* nbdkit-file-plugin
  A file serving plugin.
* nbdkit-memory-plugin
  A virtual memory plugin.
* nbdkit-nbd-plugin
  An NBD forwarding plugin.
  It provides an NBD server that forwards all traffic as a client to
  another existing NBD server.  A primary usage of this setup is to
  alter the set of features available to the ultimate end client,
  without having to change the original server (for example, to
  convert between oldstyle and newtyle, or to add TLS support where
  the original server lacks it).
* nbdkit-null-plugin
  A null (bitbucket) plugin.
* nbdkit-pattern-plugin
  Fixed test pattern.
* nbdkit-random-plugin
  Random content plugin for testing.
* nbdkit-split-plugin
  Concatenate one or more files into a single virtual disk.
* nbdkit-streaming-plugin
  A streaming file serving plugin.
* nbdkit-zero-plugin
  Zero-length plugin for testing.

%package        plugins
Summary:        Plugins set
License:        BSD
Provides:       nbdkit-example-plugins nbdkit-curl-plugin nbdkit-plugin-curl = %{version}-%{release}
Provides:       nbdkit-ruby-plugin nbdkit-plugin-ruby = %{version}-%{release}
Provides:       nbdkit-tar-plugin nbdkit-plugin-tar = %{version}-%{release}
Provides:       nbdkit-tcl-plugin nbdkit-plugin-tcl = %{version}-%{release}
Provides:       nbdkit-xz-plugin nbdkit-plugin-xz = %{version}-%{release}
Obsoletes:      nbdkit-example-plugins nbdkit-plugin-examples < 1.1.19-1
Obsoletes:      nbdkit-curl-plugin nbdkit-plugin-curl <= %{version}-%{release}
Obsoletes:      nbdkit-ruby-plugin nbdkit-plugin-ruby <= %{version}-%{release}
Obsoletes:      nbdkit-tar-plugin nbdkit-plugin-tar <= %{version}-%{release}
Obsoletes:      nbdkit-tcl-plugin nbdkit-plugin-tcl <= %{version}-%{release}
Obsoletes:      nbdkit-xz-plugin nbdkit-plugin-xz <= %{version}-%{release}
Requires:       nbdkit-server = %{version}-%{release} nbdkit-perl-plugin
Provides:       %{name}-S3-plugin              %{name}-S3-plugin = %{version}-%{release}
Provides:       %{name}-cc-plugin              %{name}-cc-plugin = %{version}-%{release}
Provides:       %{name}-cdi-plugin             %{name}-cdi-plugin = %{version}-%{release}
Provides:       %{name}-linuxdisk-plugin       %{name}-linuxdisk-plugin = %{version}-%{release}
Provides:       %{name}-tmpdisk-plugin         %{name}-tmpdisk-plugin = %{version}-%{release}
Provides:       %{name}-eval-plugin            %{name}-eval-plugin = %{version}-%{release}	
Provides:       %{name}-floppy-plugin          %{name}-floppy-plugin = %{version}-%{release}	
Provides:       %{name}-full-plugin            %{name}-full-plugin = %{version}-%{release}	
Provides:       %{name}-info-plugin            %{name}-info-plugin = %{version}-%{release}
Provides:       %{name}-ondemand-plugin        %{name}-ondemand-plugin = %{version}-%{release}
Provides:       %{name}-partitioning-plugin    %{name}-partitioning-plugin = %{version}-%{release}
Provides:       %{name}-sh-plugin              %{name}-sh-plugin = %{version}-%{release}
Provides:       %{name}-sparse-random-plugin   %{name}-sparse-random-plugin = %{version}-%{release} 
Obsoletes:      %{name}-S3-plugin              %{name}-S3-plugin <= %{version}-%{release}  
Obsoletes:      %{name}-cc-plugin              %{name}-cc-plugin <= %{version}-%{release}    
Obsoletes:      %{name}-cdi-plugin             %{name}-cdi-plugin <= %{version}-%{release}   
Obsoletes:      %{name}-linuxdisk-plugin       %{name}-linuxdisk-plugin <= %{version}-%{release}   
Obsoletes:      %{name}-tmpdisk-plugin         %{name}-tmpdisk-plugin <= %{version}-%{release}

%description    plugins
This package contains a set of plugins for nbdkit.

%package        guestfs-plugin
Summary:        libguestfs plugin for nbdkit
License:        BSD
Requires:       nbdkit-server = %{version}-%{release}
Provides:       nbdkit-plugin-guestfs = %{version}-%{release}
Obsoletes:      nbdkit-plugin-guestfs <= %{version}-%{release}

%description    guestfs-plugin
This package is a libguestfs plugin for nbdkit.

%package        libvirt-plugin
Summary:        Libvirt plugin for nbdkit
License:        BSD
Requires:       nbdkit-server = %{version}-%{release}
Provides:       nbdkit-plugin-libvirt = %{version}-%{release}
Obsoletes:      nbdkit-plugin-libvirt <= %{version}-%{release}

%description    libvirt-plugin
This package is a libvirt plugin for nbdkit.  It lets you access
libvirt guest disks readonly.  It is implemented using the libvirt
virDomainBlockPeek API.

%package        ocaml-plugin
Summary:        OCaml plugin for nbdkit
License:        BSD
Requires:       nbdkit-server = %{version}-%{release}
Provides:       nbdkit-plugin-ocaml = %{version}-%{release}
Obsoletes:      nbdkit-plugin-ocaml <= %{version}-%{release}

%description    ocaml-plugin
This package lets you run OCaml plugins for nbdkit.
To compile OCaml plugins you will also need to install
nbdkit-ocaml-plugin-devel.

%package        ocaml-plugin-devel
Summary:        OCaml development environment for nbdkit
License:        BSD
Requires:       nbdkit-server = %{version}-%{release} nbdkit-ocaml-plugin = %{version}-%{release}
Provides:       nbdkit-plugin-ocaml-devel = %{version}-%{release}
Obsoletes:      nbdkit-plugin-ocaml-devel <= %{version}-%{release}

%description    ocaml-plugin-devel
This package lets you write OCaml plugins for nbdkit.

%package        perl-plugin
Summary:        Perl plugin for nbdkit
License:        BSD
Requires:       nbdkit-server = %{version}-%{release}
Provides:       nbdkit-plugin-perl = %{version}-%{release}
Obsoletes:      nbdkit-plugin-perl <= %{version}-%{release}

%description    perl-plugin
This package lets you write Perl plugins for nbdkit.

%package        python3-plugin
Summary:        Python 3 plugin for nbdkit
License:        BSD
Requires:       nbdkit-server = %{version}-%{release}
Provides:       nbdkit-plugin-python3 = %{version}-%{release}
Obsoletes:      nbdkit-plugin-python3 <= %{version}-%{release}
Provides:       nbdkit-python-plugin-common = %{version}-%{release}
Obsoletes:      nbdkit-python-plugin-common <= %{version}-%{release}

%description    python3-plugin
This package lets you write Python 3 plugins for nbdkit.

%ifarch x86_64
%package        vddk-plugin
Summary:        VMware VDDK plugin for nbdkit
License:        BSD
Requires:       nbdkit-server = %{version}-%{release}
Provides:       nbdkit-plugin-vddk = %{version}-%{release}
Obsoletes:      nbdkit-plugin-vddk <= %{version}-%{release}

%description    vddk-plugin
This package is a plugin for nbdkit which connects to
VMware VDDK for accessing VMware disks and servers.
%endif

%package        basic-filters
Summary:        Basic filters for nbdkit
License:        BSD
Requires:       nbdkit-server = %{version}-%{release}
Provides:       %{name}-blocksize-filter = %{version}-%{release}
Provides:       %{name}-cache-filter = %{version}-%{release}
Provides:       %{name}-cacheextents-filter = %{version}-%{release}
Provides:       %{name}-checkwrite-filter = %{version}-%{release}
Provides:       %{name}-cow-filter = %{version}-%{release}
Provides:       %{name}-ddrescue-filter = %{version}-%{release}
Provides:       %{name}-protect-filter = %{version}-%{release}
Provides:       %{name}-delay-filter = %{version}-%{release}
Provides:       %{name}-error-filter = %{version}-%{release}
Provides:       %{name}-exitlast-filter = %{version}-%{release}
Provides:       %{name}-exitwhen-filter = %{version}-%{release}
Provides:       %{name}-exportname-filter = %{version}-%{release}
Provides:       %{name}-extentlist-filter = %{version}-%{release}
Provides:       %{name}-fua-filter = %{version}-%{release}
Provides:       %{name}-ip-filter = %{version}-%{release}
Provides:       %{name}-limit-filter = %{version}-%{release}
Provides:       %{name}-log-filter = %{version}-%{release}
Provides:       %{name}-multi-conn-filter = %{version}-%{release}
Provides:       %{name}-nocache-filter = %{version}-%{release}
Provides:       %{name}-noextents-filter = %{version}-%{release}
Provides:       %{name}-nofilter-filter = %{version}-%{release}
Provides:       %{name}-noparallel-filter = %{version}-%{release}
Provides:       %{name}-nozero-filter = %{version}-%{release}
Provides:       %{name}-offset-filter = %{version}-%{release}
Provides:       %{name}-partition-filter = %{version}-%{release}
Provides:       %{name}-pause-filter = %{version}-%{release}
Provides:       %{name}-rate-filter = %{version}-%{release}
Provides:       %{name}-readahead-filter = %{version}-%{release}
Provides:       %{name}-retry-filter = %{version}-%{release}	
Provides:       %{name}-stats-filter = %{version}-%{release}
Provides:       %{name}-swab-filter = %{version}-%{release}
Provides:       %{name}-tls-fallback-filter = %{version}-%{release}	
Provides:       %{name}-truncate-filter = %{version}-%{release}
Provides:       %{name}-ext2-filter = %{version}-%{release}
Provides:       %{name}-gzip-filter = %{version}-%{release}
Provides:       %{name}-lua-filter = %{version}-%{release}
Provides:       %{name}-retry-request-filter = %{version}-%{release}
Provides:       %{name}-tar-filter = %{version}-%{release} 
%description    basic-filters
This package contains some basic filters for nbdkit which have only
trivial dependencies.
* nbdkit-blocksize-filter
  Adjust block size of requests sent to plugins.
* nbdkit-cache-filter
  Server-side cache.
* nbdkig-cow-filter
  Copy-on-write overlay for read-only plugins.
* nbdkit-delay-filter
  Inject read and write delays.
* nbdkit-error-filter
  Inject errors.
* nbdkit-fua-filter
  Modify flush behaviour in plugins.
* nbdkit-log-filter
  Log all transactions to a file.
* nbdkit-nozero-filter
  Adjust handling of zero requests by plugins.
* nbdkit-offset-filter
  Serve an offset and range.
* nbdkit-partition-filter
  Serve a single partition.
* nbdkit-truncate-filter
  Truncate, expand, round up or round down a plugin size.

%package        devel
Summary:        Development files and documentation for nbdkit
License:        BSD
Requires:       nbdkit-server = %{version}-%{release} pkgconfig

%description    devel
This package contains development files and documentation
for nbdkit.  Install this package if you want to develop
plugins for nbdkit.

%package        bash-completion
Summary:        Bash tab-completion for nbdkit
BuildArch:      noarch
Requires:       bash-completion >= 2.0 nbdkit-server = %{version}-%{release}

%description    bash-completion
Install this package if you want intelligent bash tab-completion
for nbdkit.

%package        help
Summary:        Documents for nbdkit
Buildarch:      noarch
Requires:       man info

%description    help
Man pages and other related documents for nbdkit

%prep
tmphome="$(mktemp -d)"
gpgv2 --homedir "$tmphome" --keyring %{SOURCE2} %{SOURCE1} %{SOURCE0}
%autosetup -p1
%if 0%{patches_touch_autotools}
autoreconf -i
%endif

%ifnarch %{complete_test_arches}
sed -i -e '/^if HAVE_LIBGUESTFS/,/^endif HAVE_LIBGUESTFS/d' tests/Makefile.am
sed -i -e '/^if HAVE_GUESTFISH/,/^endif HAVE_GUESTFISH/d' tests/Makefile.am
autoreconf -i
%endif

%build
export LDFLAGS="$LDFLAGS -Wl,-z,now"
copy="$(mktemp -d)"
cp -a . "$copy"
mv "$copy" python3
%configure --disable-static --with-libguestfs --with-libvirt --with-tls-priority=@NBDKIT,SYSTEM
%make_build


pushd python3
export PYTHON=%{_bindir}/python3
%configure --disable-static --disable-lua --disable-tcl \
           --without-curl --without-liblzma --without-zlib
grep '^PYTHON_VERSION = 3' Makefile
%make_build
unset PYTHON
popd

%install
pushd python3
%make_install
popd

pushd $RPM_BUILD_ROOT%{_libdir}/nbdkit/plugins/
mv nbdkit-python-plugin.so nbdkit-python3-plugin.so
popd

find $RPM_BUILD_ROOT -name '*.la' -delete

%check
function skip_test ()
	
{
	
    for f in "$@"; do
	
        rm -f "$f"
	
        echo 'exit 77' > "$f"
	
        chmod +x "$f"
	
    done
	
}
skip_test tests/test-shebang-cc.sh tests/test-floppy.sh tests/test-eval-file.sh
skip_test tests/test-linuxdisk.sh tests/test-ondemand.sh tests/test-partitioning2.sh
skip_test tests/test-partitioning3.sh tests/test-partitioning5.sh tests/test-tar.sh
skip_test tests/test-old-plugins-x86_64-Linux-v1.18.2.sh
skip_test tests/test-old-plugins-x86_64-Linux-v1.8.4.sh
skip_test tests/test-old-plugins-x86_64-Linux-v1.12.8.sh
skip_test tests/test-old-plugins-x86_64-Linux-v1.2.8.sh
skip_test tests/test-old-plugins-x86_64-Linux-v1.0.0.sh
mkdir -p $HOME/.cache/libvirt
export LIBGUESTFS_DEBUG=1
export LIBGUESTFS_TRACE=1
find -name *.log
make check -j1 || {
    cat tests/test-suite.log
    exit 1
  }

%ifarch %{complete_test_arches}
pushd python3
make check -j1 -C tests TESTS=test-python || {
    cat tests/test-suite.log
    exit 1
  }
popd
%endif

%post ocaml-plugin -p /sbin/ldconfig
%postun ocaml-plugin -p /sbin/ldconfig

%files

%files server
%doc README LICENSE
%{_sbindir}/nbdkit
%dir %{_libdir}/nbdkit/{plugins,filters}

%files basic-plugins
%doc README LICENSE
%{_libdir}/nbdkit/plugins/{nbdkit-data-plugin.so,nbdkit-file-plugin.so,nbdkit-memory-plugin.so}
%{_libdir}/nbdkit/plugins/{nbdkit-nbd-plugin.so,nbdkit-null-plugin.so,nbdkit-pattern-plugin.so}
%{_libdir}/nbdkit/plugins/{nbdkit-random-plugin.so,nbdkit-split-plugin.so}
%{_libdir}/nbdkit/plugins/{nbdkit-streaming-plugin.so,nbdkit-zero-plugin.so}

%files plugins
%doc README LICENSE
%{_libdir}/nbdkit/plugins/{nbdkit-example*-plugin.so,nbdkit-example4-plugin,nbdkit-curl-plugin.so}	
%{_libdir}/nbdkit/plugins/{nbdkit-ruby-plugin.so,nbdkit-tar-plugin,nbdkit-tcl-plugin.so,nbdkit-xz-plugin.so}
%{_libdir}/nbdkit/plugins/{nbdkit-S3-plugin,nbdkit-cc-plugin.so,nbdkit-cdi-plugin.so,nbdkit-eval-plugin.so}
%{_libdir}/nbdkit/plugins/{nbdkit-floppy-plugin.so,nbdkit-full-plugin.so,nbdkit-info-plugin.so}
%{_libdir}/nbdkit/plugins/{nbdkit-linuxdisk-plugin.so,nbdkit-ondemand-plugin.so,nbdkit-partitioning-plugin.so}
%{_libdir}/nbdkit/plugins/{nbdkit-sh-plugin.so,nbdkit-sparse-random-plugin.so,nbdkit-tmpdisk-plugin.so}

%files guestfs-plugin
%doc README LICENSE
%{_libdir}/nbdkit/plugins/nbdkit-guestfs-plugin.so

%files libvirt-plugin
%doc README LICENSE
%{_libdir}/nbdkit/plugins/nbdkit-libvirt-plugin.so

%files ocaml-plugin
%doc README LICENSE
%{_libdir}/libnbdkitocaml.so.*

%files ocaml-plugin-devel
%{_libdir}/libnbdkitocaml.so
%{_libdir}/ocaml/NBDKit.*

%files perl-plugin
%doc README LICENSE
%{_libdir}/nbdkit/plugins/nbdkit-perl-plugin.so

%files python3-plugin
%doc README LICENSE
%{_libdir}/nbdkit/plugins/nbdkit-python3-plugin.so

%ifarch x86_64
%files vddk-plugin
%doc README LICENSE
%{_libdir}/nbdkit/plugins/nbdkit-vddk-plugin.so
%endif

%files basic-filters
%doc README LICENSE
%{_libdir}/nbdkit/filters/{nbdkit-blocksize-filter.so,nbdkit-cache-filter.so,nbdkit-cow-filter.so}
%{_libdir}/nbdkit/filters/{nbdkit-delay-filter.so,nbdkit-error-filter.so,nbdkit-fua-filter.so}
%{_libdir}/nbdkit/filters/{nbdkit-log-filter.so,nbdkit-nozero-filter.so,nbdkit-offset-filter.so}
%{_libdir}/nbdkit/filters/{nbdkit-partition-filter.so,nbdkit-truncate-filter.so}
%{_libdir}/nbdkit/filters/{nbdkit-cacheextents-filter.so,nbdkit-checkwrite-filter.so,nbdkit-ddrescue-filter.so}
%{_libdir}/nbdkit/filters/{nbdkit-exitlast-filter.so,nbdkit-exitwhen-filter.so,nbdkit-exportname-filter.so}
%{_libdir}/nbdkit/filters/{nbdkit-extentlist-filter.so,nbdkit-ip-filter.so,nbdkit-limit-filter.so}
%{_libdir}/nbdkit/filters/{nbdkit-multi-conn-filter.so,nbdkit-nocache-filter.so,nbdkit-noextents-filter.so}
%{_libdir}/nbdkit/filters/{nbdkit-nofilter-filter.so,nbdkit-noparallel-filter.so,nbdkit-pause-filter.so}
%{_libdir}/nbdkit/filters/{nbdkit-rate-filter.so,nbdkit-readahead-filter.so,nbdkit-retry-filter.so}
%{_libdir}/nbdkit/filters/{nbdkit-retry-request-filter.so,nbdkit-stats-filter.so,nbdkit-swab-filter.so}
%{_libdir}/nbdkit/filters/{nbdkit-tar-filter.so,nbdkit-tls-fallback-filter.so,nbdkit-protect-filter.so}
%{_libdir}/nbdkit/filters/{nbdkit-ext2-filter.so,nbdkit-gzip-filter.so,nbdkit-lua-filter.so}


%files devel
%doc OTHER_PLUGINS README TODO LICENSE
%doc plugins/example*/*.c
%doc plugins/example4/nbdkit-example4-plugin
%doc plugins/lua/example.lua
%doc plugins/perl/example.pl
%doc plugins/python/examples/*.py
%doc plugins/ruby/example.rb
%doc plugins/tcl/example.tcl
%{_includedir}/nbdkit-common.h
%{_includedir}/nbdkit-filter.h
%{_includedir}/nbdkit-plugin.h
%{_includedir}/nbd-protocol.h
%{_includedir}/nbdkit-version.h
%{_libdir}/pkgconfig/nbdkit.pc

%files bash-completion
%doc LICENSE
%dir %{_datadir}/bash-completion/completions
%{_datadir}/bash-completion/completions/nbdkit

%files help
%{_mandir}/man1/nbdkit*.1*
%{_mandir}/man3/nbdkit-*.3*
%{_mandir}/man3/NBDKit.3.gz

%changelog
* Thu Dec 30 2021 liyanan <liyanan32@huawei.com> - 1.29.11-1
- update to 1.29.11

* Wed Oct 21 2020 chengzihan <chengzihan2@huawei.com> - 1.6.2-7
- Remove subpackage nbdkit-python2-plugin

* Wed Jun 24 2020 yaokai13 <yaokai13@huawei.com> - 1.6.2-6
- Fix the test error

* Mon Jun 1 2020 Jeffery.Gao <gaojianxing@huawei.com> - 1.6.2-5
- Remove the special check for x86-64 architecture, consist with aarch64.

* Fri Apr 3 2020 Ling Yang <lingyang2@huawei.com> - 1.6.2-4
- Rewrite description for nbdkit

* Fri Apr 3 2020 Ling Yang <lingyang2@huawei.com> - 1.6.2-3
- Fixed format for spec file

* Thu Dec 12 2019 openEuler Buildteam <buildteam@openeuler.org> - 1.6.2-2
- Package init
