%global	mainver		1.13.7
%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:	1
License:	MIT and ASL 2.0
URL:		https://nokogiri.org
Source0:	https://rubygems.org/gems/%{gem_name}-%{mainver}%{?prever}.gem
Source1:        rubygem-%{gem_name}-%{version}%{?prever}-full.tar.gz

BuildRequires:  ruby(release) 	ruby(rubygems) 	rubygem(minitest) 	rubygems-devel
Obsoletes:      ruby-%{gem_name} <= 1.5.2-2
BuildRequires:  gcc libxml2-devel libxslt-devel ruby-devel glibc-all-langpacks rubygem(racc)
Requires: rubygem(racc)

%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 -n %{gem_name}-%{version} -a 1
mv ../%{gem_name}-%{version}.gemspec .
sed -i \
	-e 's|, "ports/archives/[^"][^"]*"||g' \
	-e 's|, "ports/patches/[^"][^"]*"||g' \
	%{gem_name}-%{version}.gemspec
sed -i -e '\@mini_portile@d' %{gem_name}-%{version}.gemspec
sed -i \
	ext/nokogiri/extconf.rb \
	-e "s@^\(def process_recipe.*\)\$@\1 ; return true@" \
	-e "s@^\(append_cppflags\).*gumbo.*\$@\1(\"-I$(pwd)/gumbo-parser/src\")@" \
	-e "\@libs.*gumbo@s@File\.join.*@\"$(pwd)/gumbo-parser/src/libgumbo.a\"@" \
	-e "\@LIBPATH.*gumbo@s|^\(.*\)\$|# \1|" \
	%{nil}
sed -i \
	gumbo-parser/src/char_ref.c \
	-e '\@^#line [0-9]@s|^\(.*\)$|// \1|'
sed -i \
	gumbo-parser/src/Makefile \
	-e 's|^\(CFLAGS.*=.*\)$|\1 -fPIC|'
env LANG=C.UTF-8 gem build %{gem_name}-%{version}.gemspec

%build
export NOKOGIRI_USE_SYSTEM_LIBRARIES=yes
%set_build_flags
pushd gumbo-parser/src/
make libgumbo.a
popd
%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}
cp -a ./gumbo-parser  %{buildroot}%{gem_instdir}/
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}/
pushd %{buildroot}%{gem_instdir}
rm -rf \
	Gemfile* \
	dependencies.yml \
	ext \
	*gemspec \
	patches \
	ports \
	%{nil}
pushd gumbo-parser
rm \
	Makefile \
	%{nil}
find src -type f | \
	grep -v README.md | \
	xargs rm -f
popd
rm -f %{buildroot}%{gem_cache}

%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'
sed -i '/require..minitest.reporters./ s/^/#/' test/helper.rb
sed -i '/Minitest::Reporters/ s/^/#/' test/helper.rb
env \
	RUBYLIB=".:lib:test:ext" \
	ruby \
	-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
%{_bindir}/%{gem_name}
%{gem_extdir_mri}/
%dir	%{gem_instdir}/
%doc	%{gem_instdir}/[A-Z]*
%{gem_instdir}/bin/
%{gem_instdir}/lib/

%dir	%{gem_instdir}/gumbo-parser
%dir	%{gem_instdir}/gumbo-parser/src
%doc	%{gem_instdir}/gumbo-parser/[A-Z]*
%doc	%{gem_instdir}/gumbo-parser/src/README.md

%{gem_dir}/specifications/%{gem_name}-%{mainver}%{?prever}.gemspec

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

%changelog
* Thu Jul 14 2022 Ge Wang <wangge20@h-partners.com> - 1.13.7-1
- update to 1.13.7

* Sun Apr 24 2022 wangkai <wangkai385@h-partners.com> - 1.13.1-2
- Fix CVE-2022-24836

* Thu Mar 03 2022 jiangxinyu <jiangxinyu@kylinos.cn> - 1.13.1-1
- update to 1.13.1

* Fri Jan 07 2022 liyanan <liyanan32@huawei.com> - 1.10.5-5
- fix canonicalize already defined by glibc

* Fri Jan 07 2022 wangkai <wangkai385@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
