%global	mainver		1.10.5
%global	mainrel		1
%global	prerpmver	%(echo "%{?prever}" | sed -e 's|\\.||g')
%global	gem_name	nokogiri
%undefine       __brp_mangle_shebangs
%undefine	_changelog_trimtime
Summary:	An HTML, XML, SAX, and Reader parser
Name:		rubygem-%{gem_name}
Version:	%{mainver}
Release:	5
License:	MIT
URL:		https://nokogiri.org
Source0:	https://rubygems.org/gems/%{gem_name}-%{mainver}%{?prever}.gem
Source1:        https://github.com/sparklemotion/%{gem_name}/archive/v%{mainver}.tar.gz
# Shut down libxml2 version unmatching warning
Patch0:	        %{name}-1.6.6.4-shutdown-libxml2-warning.patch
Patch1:         CVE-2020-26247-pre.patch
Patch2:         CVE-2020-26247.patch
Patch3:         CVE-2021-41098-1.patch
Patch4:         CVE-2021-41098-2.patch
Patch5:		fix-test-push-parser-fail.patch
# refer https://github.com/sparklemotion/nokogiri/commit/e444525
Patch6:         CVE-2022-24836.patch
BuildRequires:  ruby(release) 	ruby(rubygems) 	rubygem(minitest) 	rubygems-devel
Obsoletes:      ruby-%{gem_name} <= 1.5.2-2
BuildRequires:  gcc 	rubygem(pkg-config) 	libxml2-devel 	libxslt-devel 	ruby-devel
%description
Nokogiri parses and searches XML/HTML very quickly, and also has
correctly implemented CSS3 selector support as well as XPath support.
Nokogiri also features an Hpricot compatibility layer to help ease the change
to using correct CSS and XPath.
%if 0

%package	jruby
Summary:	JRuby support for %{name}
Requires:       %{name} = %{version}-%{release}
%description	jruby
This package contains JRuby support for %{name}.
%endif

%package	doc
Summary:	Documentation for %{name}
Requires:       %{name} = %{version}-%{release}
%description	doc
This package contains documentation for %{name}.

%package	-n ruby-%{gem_name}
Summary:	Non-Gem support package for %{gem_name}
Requires:       %{name} = %{version}-%{release}
Provides:	ruby(%{gem_name}) = %{version}-%{release}
%description	-n ruby-%{gem_name}
This package provides non-Gem support for %{gem_name}.
%global	version	%{mainver}%{?prever}

%prep
%setup -q -T -c -a 1
TOPDIR=$(pwd)
mkdir tmpunpackdir
pushd tmpunpackdir
gem unpack %{SOURCE0}
cd %{gem_name}-%{version}
%patch0 -p1
%patch1 -p1
%patch2 -p1

#The file exists only in the source code directory,not in the GEM.
cd $TOPDIR/%{gem_name}-%{version}
%patch3 -p1
%patch4 -p1
%patch5 -p1
%patch6 -p1
cd -

gem specification -l --ruby %{SOURCE0} > %{gem_name}.gemspec
sed -i \
	-e 's|, "ports/archives/[^"][^"]*"||g' \
	-e 's|, "ports/patches/[^"][^"]*"||g' \
	%{gem_name}.gemspec
sed -i -e '\@mini_portile@d' %{gem_name}.gemspec
LANG=C.UTF-8 gem build %{gem_name}.gemspec
mv %{gem_name}-%{version}.gem $TOPDIR
popd
rm -rf tmpunpackdir

%build
mkdir -p ./%{gem_dir}
export NOKOGIRI_USE_SYSTEM_LIBRARIES=yes
%gem_install
chmod 0644 .%{gem_dir}/cache/%{gem_name}-%{mainver}%{?prever}.gem
rm -f .%{gem_instdir}/lib/*.jar
rm -rf .%{gem_instdir}/ext/java

%install
mkdir -p %{buildroot}%{gem_dir}
cp -a ./%{gem_dir}/* %{buildroot}%{gem_dir}
find %{buildroot} -name \*.orig_\* | xargs rm -vf
mkdir -p %{buildroot}%{gem_extdir_mri}
cp -a ./%{gem_extdir_mri}/* %{buildroot}%{gem_extdir_mri}/
pushd %{buildroot}
rm -f .%{gem_extdir_mri}/{gem_make.out,mkmf.log}
popd
mkdir -p %{buildroot}%{_bindir}
cp -pa .%{_bindir}/* \
        %{buildroot}%{_bindir}/
for f in $(find %{buildroot}%{gem_instdir} -name \*.rb)
do
	sed -i -e '/^#!/d' $f
	chmod 0644 $f
done
cp -p %{gem_name}-%{version}/[A-Z]* %{buildroot}%{gem_instdir}/
rm -rf %{buildroot}%{gem_instdir}/ext/%{gem_name}/
rm -rf %{buildroot}%{gem_instdir}/tmp/
rm -f %{buildroot}%{gem_instdir}/{.autotest,.require_paths,.gemtest,.travis.yml}
rm -f %{buildroot}%{gem_instdir}/appveyor.yml
rm -f %{buildroot}%{gem_instdir}/.cross_rubies
rm -f %{buildroot}%{gem_instdir}/{build_all,dependencies.yml,test_all}
rm -f %{buildroot}%{gem_instdir}/.editorconfig
rm -rf %{buildroot}%{gem_instdir}/suppressions/
rm -rf %{buildroot}%{gem_instdir}/patches/
rm -f %{buildroot}%{gem_instdir}/{Rakefile,Gemfile*}
rm -f %{buildroot}%{gem_instdir}/Manifest.txt

%check
export TZ="Asia/Tokyo"
LANG=C.UTF-8
cp -a %{gem_name}-%{version}/test/ ./%{gem_instdir}
pushd ./%{gem_instdir}
sed -i test/helper.rb \
	-e '\@require.*simplecov@,\@^end$@d'
ruby \
	-I.:lib:test:ext \
	-e \
	"require 'test/helper' ; Dir.glob('test/**/test_*.rb'){|f| require f}" || \
	exit 1
	echo "Please investigate this"
for f in $SKIPTEST
do
	mv $f.skip $f
done
popd

%files
%defattr(-,root, root,-)
%{_bindir}/%{gem_name}
%{gem_extdir_mri}/
%dir	%{gem_instdir}/
%doc	%{gem_instdir}/[A-Z]*
%{gem_instdir}/bin/
%{gem_instdir}/lib/
%exclude	%{gem_dir}/cache/%{gem_name}-%{mainver}%{?prever}.gem
%{gem_dir}/specifications/%{gem_name}-%{mainver}%{?prever}.gemspec
%if 0

%files	jruby
%defattr(-,root,root,-)
%{gem_instdir}/ext/java/
%endif

%files	doc
%defattr(-,root,root,-)
%exclude %{gem_instdir}/tasks/
%exclude %{gem_instdir}/test/
%{gem_dir}/doc/%{gem_name}-%{mainver}%{?prever}/

%changelog
* Sun Apr 24 2022 wangkai <wangkai385@huawei.com> - 1.10.5-5
- Fix CVE-2022-24836

* Thu Dec 9 2021 baizhonggui <baizhonggui@huawei.com> - 1.10.5-4
- fix compile failed

* Tue Oct 12 2021 yaoxin <yaoxin30@huawei.com> - 1.10.5-3
- fix CVE-2021-41098

* Wed Mar 17 2021 zhanghua <zhanghua40@huawei.com> - 1.10.5-2
- fix CVE-2020-26247

* Wed Aug 19 2020 luoshengwei <luoshengwei@huawei.com> - 1.10.5-1
- package init
