%{!?_pkgdocdir: %global _pkgdocdir %{_docdir}/%{name}-%{version}}
%global all_catalog "{3,4}.{0,1}-sgml 4.1.2-xml 4.{2,3,4,5}-{sgml,xml}"

Name:     docbook-dtds
Version:  1.0
Release:  78
Summary:  The DocBook Document Type Definition.
License:  Copyright only
URL:      http://www.oasis-open.org/docbook
Source0:  http://www.oasis-open.org/docbook/sgml/3.0/docbk30.zip
Source1:  http://www.oasis-open.org/docbook/sgml/3.1/docbk31.zip
Source2:  http://www.oasis-open.org/docbook/sgml/4.0/docbk40.zip
Source3:  http://www.oasis-open.org/docbook/sgml/4.1/docbk41.zip
Source4:  http://www.oasis-open.org/docbook/xml/4.1.2/docbkx412.zip
Source5:  http://www.oasis-open.org/docbook/sgml/4.2/docbook-4.2.zip
Source6:  http://www.oasis-open.org/docbook/xml/4.2/docbook-xml-4.2.zip
Source7:  http://www.docbook.org/sgml/4.3/docbook-4.3.zip
Source8:  http://www.docbook.org/xml/4.3/docbook-xml-4.3.zip
Source9:  http://www.docbook.org/sgml/4.4/docbook-4.4.zip
Source10: http://www.docbook.org/xml/4.4/docbook-xml-4.4.zip
Source11: http://www.docbook.org/sgml/4.5/docbook-4.5.zip
Source12: http://www.docbook.org/xml/4.5/docbook-xml-4.5.zip
Source13: http://www.docbook.org/rng/4.2/docbook-rng-4.2.zip
Source14: http://www.docbook.org/rng/4.3/docbook-rng-4.3.zip
Source15: http://www.docbook.org/rng/4.4/docbook-rng-4.4.zip
#Source16 is compressed from http://www.docbook.org/rng/4.5/
Source16: docbook-rng-4.5.zip
Source17: http://www.docbook.org/xsd/4.2/docbook-xsd-4.2.zip
Source18: http://www.docbook.org/xsd/4.3/docbook-xsd-4.3.zip
Source19: http://www.docbook.org/xsd/4.4/docbook-xsd-4.4.zip
#Source20 is compressed from http://www.docbook.org/xsd/4.5/
Source20: docbook-xsd-4.5.zip

Patch0: docbook-dtd30-sgml-1.0.catalog.patch
Patch1: docbook-dtd31-sgml-1.0.catalog.patch
Patch2: docbook-dtd40-sgml-1.0.catalog.patch
Patch3: docbook-dtd41-sgml-1.0.catalog.patch
Patch4: docbook-dtd42-sgml-1.0.catalog.patch
Patch5: docbook-4.2-euro.patch
Patch6: docbook-dtds-ents.patch
Patch7: docbook-sgml-systemrewrite.patch
Patch8: docbook-dtd412-entities.patch

BuildRequires: unzip

Requires: sgml-common sed libxml2 coreutils

Obsoletes: docbook-dtd30-sgml < %{version}-%{release}
Obsoletes: docbook-dtd31-sgml < %{version}-%{release}
Obsoletes: docbook-dtd40-sgml < %{version}-%{release}
Obsoletes: docbook-dtd41-sgml < %{version}-%{release}
Obsoletes: docbook-dtd412-xml < %{version}-%{release}

Provides:     docbook-dtd-xml = %{version}-%{release}
Provides:    docbook-dtd-sgml = %{version}-%{release}
Provides:  docbook-dtd30-sgml = %{version}-%{release}
Provides:  docbook-dtd31-sgml = %{version}-%{release}
Provides:  docbook-dtd40-sgml = %{version}-%{release}
Provides:  docbook-dtd41-sgml = %{version}-%{release}
Provides:  docbook-dtd412-xml = %{version}-%{release}
Provides:  docbook-dtd42-sgml = %{version}-%{release}
Provides:   docbook-dtd42-xml = %{version}-%{release}
Provides:  docbook-dtd43-sgml = %{version}-%{release}
Provides:   docbook-dtd43-xml = %{version}-%{release}
Provides:  docbook-dtd44-sgml = %{version}-%{release}
Provides:   docbook-dtd44-xml = %{version}-%{release}
Provides:  docbook-dtd45-sgml = %{version}-%{release}
Provides:   docbook-dtd45-xml = %{version}-%{release}


BuildArch: noarch

%description
this package contains SGML, XML, RNG and XSD versions of DocBook Document Type Definition.

%prep
%setup -c -T
mkdir 3.0-sgml && cd 3.0-sgml && unzip %{SOURCE0}
%patch0 -p0 -b docbook.cat
cd ..
mkdir 3.1-sgml && cd 3.1-sgml && unzip %{SOURCE1}
%patch1 -p0 -b docbook.cat
cd ..
mkdir 4.0-sgml && cd 4.0-sgml && unzip %{SOURCE2}
%patch2 -p0 -b docbook.cat
cd ..
mkdir 4.1-sgml && cd 4.1-sgml && unzip %{SOURCE3}
%patch3 -p0 -b docbook.cat
cd ..
mkdir 4.1.2-xml && cd 4.1.2-xml && unzip %{SOURCE4} && cd ..
mkdir 4.2-sgml && cd 4.2-sgml && unzip %{SOURCE5}
%patch4 -p0 -b docbook.cat
cd ..
mkdir 4.2-xml  && cd 4.2-xml  && unzip %{SOURCE6}  && cd ..
mkdir 4.3-sgml && cd 4.3-sgml && unzip %{SOURCE7}  && cd ..
mkdir 4.3-xml  && cd 4.3-xml  && unzip %{SOURCE8}  && cd ..
mkdir 4.4-sgml && cd 4.4-sgml && unzip %{SOURCE9}  && cd ..
mkdir 4.4-xml  && cd 4.4-xml  && unzip %{SOURCE10} && cd ..
mkdir 4.5-sgml && cd 4.5-sgml && unzip %{SOURCE11} && cd ..
mkdir 4.5-xml && cd 4.5-xml && unzip %{SOURCE12} && cd ..
mkdir 4.2-rng && cd 4.2-rng && unzip %{SOURCE13} && cd ..
mkdir 4.3-rng && cd 4.3-rng && unzip %{SOURCE14} && cd ..
mkdir 4.4-rng && cd 4.4-rng && unzip %{SOURCE15} && cd ..
mkdir 4.5-rng && cd 4.5-rng && unzip %{SOURCE16} && cd ..
mkdir 4.2-xsd && cd 4.2-xsd && unzip %{SOURCE17} && cd ..
mkdir 4.3-xsd && cd 4.3-xsd && unzip %{SOURCE18} && cd ..
mkdir 4.4-xsd && cd 4.4-xsd && unzip %{SOURCE19} && cd ..
mkdir 4.5-xsd && cd 4.5-xsd && unzip %{SOURCE20} && cd ..
%patch5 -p1
%patch6 -p1
%patch7 -p1
%patch8 -p1
sed -e's,\(NAMELEN\s\+\)44\(\s\*\)\?,\1256,' -i.namelen */docbook.dcl
sed -i 's/\r//' */*.txt
if [ `id -u` -eq 0 ]; then
  chown -R root:root .
  chmod -R a+rX,g-w,o-w .
fi

%build

%install
mkdir -p %{buildroot}/etc/sgml
for fmt in sgml xml; do
  ln -s $fmt-docbook-4.5.cat %{buildroot}/etc/sgml/$fmt-docbook.cat
done

set 3.0-sgml 3.1-sgml 4.0-sgml 4.1-sgml 4.1.2-xml 4.2-sgml 4.2-xml 4.3-sgml 4.3-xml 4.4-sgml \
4.4-xml 4.5-sgml 4.5-xml 4.2-rng 4.3-rng 4.4-rng 4.5-rng 4.2-xsd 4.3-xsd 4.4-xsd 4.5-xsd
for dir
do
  cd $dir
  fmt=${dir#*-} ver=${dir%%-*}
  case $fmt in
    sgml)   DESTDIR=%{buildroot}/usr/share/sgml/docbook/$fmt-dtd-$ver ;;
    xml)    DESTDIR=%{buildroot}/usr/share/sgml/docbook/$fmt-dtd-$ver ;;
    rng)    DESTDIR=%{buildroot}/usr/share/sgml/docbook/$fmt-$ver ;;
    xsd)    DESTDIR=%{buildroot}/usr/share/sgml/docbook/$fmt-$ver ;;
  esac
  case $fmt in
    sgml)   mkdir -p $DESTDIR ;     cp *.dcl $DESTDIR ;;
    xml)    mkdir -p $DESTDIR/ent ; cp ent/* $DESTDIR/ent ;;
    rng)    mkdir -p $DESTDIR ;     cp *.r* $DESTDIR ;;
    xsd)    mkdir -p $DESTDIR ;     cp *.xsd $DESTDIR;;
  esac
  cd ..
done

eval set %{all_catalog}
for dir
do
  cd $dir
  fmt=${dir#*-} ver=${dir%%-*}
  DESTDIR=%{buildroot}/usr/share/sgml/docbook/$fmt-dtd-$ver
  cp *.dtd *.mod $DESTDIR
  cp docbook.cat $DESTDIR/catalog
  cd ..
  touch %{buildroot}/etc/sgml/$fmt-docbook-$ver.cat
done

mkdir -p %{buildroot}%{_pkgdocdir}
for i in */*.txt */ChangeLog */README
do
  cp -pr --parents $i %{buildroot}%{_pkgdocdir}
done

%check

%pre

%preun

%post
if [ -f /etc/sgml/sgml-docbook-3.0.cat ]
then
  /usr/bin/xmlcatalog --noout --sgml --del /etc/sgml/sgml-docbook-3.0.cat \
  /usr/share/sgml/openjade-1.3.1/catalog 2>/dev/null
fi
if [ -f /etc/sgml/sgml-docbook-3.1.cat ]
then
  /usr/bin/xmlcatalog --noout --sgml --del /etc/sgml/sgml-docbook-3.1.cat \
  /usr/share/sgml/openjade-1.3.1/catalog 2>/dev/null
fi
if [ -f /etc/sgml/sgml-docbook-4.0.cat ]
then
  /usr/bin/xmlcatalog --noout --sgml --del /etc/sgml/sgml-docbook-4.0.cat \
  /usr/share/sgml/openjade-1.3.1/catalog 2>/dev/null
fi
if [ -f /etc/sgml/sgml-docbook-4.1.cat ]
then
  /usr/bin/xmlcatalog --noout --sgml --del /etc/sgml/sgml-docbook-4.1.cat \
  /usr/share/sgml/openjade-1.3.1/catalog 2>/dev/null
fi
if [ -f /etc/sgml/sgml-docbook-4.2.cat ]
then
  /usr/bin/xmlcatalog --noout --sgml --del /etc/sgml/sgml-docbook-4.2.cat \
  /usr/share/sgml/openjade-1.3.1/catalog 2>/dev/null
fi

xmlcatalog=/usr/share/sgml/docbook/xmlcatalog
eval set %{all_catalog}
for dir
do
  fmt=${dir#*-} ver=${dir%%-*}
  sgmldir=/usr/share/sgml/docbook/$fmt-dtd-$ver

  for cat_dir in /usr/share/sgml/sgml-iso-entities-8879.1986 $sgmldir $STYLESHEETS; do
    /usr/bin/xmlcatalog --noout --sgml --add /etc/sgml/$fmt-docbook-$ver.cat $cat_dir/catalog
  done

  if [ $fmt = xml -a -w $xmlcatalog ]; then
    while read f desc; do
      case $ver in 4.[45]) f=${f/-/} ;; esac
      /usr/bin/xmlcatalog --noout --add public "$desc" $sgmldir/$f $xmlcatalog
    done <<ENDENT
      ent/iso-pub.ent	ISO 8879:1986//ENTITIES Publishing//EN
      ent/iso-grk1.ent	ISO 8879:1986//ENTITIES Greek Letters//EN
      dbpoolx.mod	-//OASIS//ELEMENTS DocBook XML Information Pool V$ver//EN
      ent/iso-box.ent	ISO 8879:1986//ENTITIES Box and Line Drawing//EN
      docbookx.dtd	-//OASIS//DTD DocBook XML V$ver//EN
      ent/iso-grk3.ent	ISO 8879:1986//ENTITIES Greek Symbols//EN
      ent/iso-amsn.ent	ISO 8879:1986//ENTITIES Added Math Symbols: Negated Relations//EN
      ent/iso-num.ent	ISO 8879:1986//ENTITIES Numeric and Special Graphic//EN
      dbcentx.mod	-//OASIS//ENTITIES DocBook XML Character Entities V$ver//EN
      ent/iso-grk4.ent	ISO 8879:1986//ENTITIES Alternative Greek Symbols//EN
      dbnotnx.mod	-//OASIS//ENTITIES DocBook XML Notations V$ver//EN
      ent/iso-dia.ent	ISO 8879:1986//ENTITIES Diacritical Marks//EN
      ent/iso-grk2.ent	ISO 8879:1986//ENTITIES Monotoniko Greek//EN
      dbgenent.mod	-//OASIS//ENTITIES DocBook XML Additional General Entities V$ver//EN
      dbhierx.mod	-//OASIS//ELEMENTS DocBook XML Document Hierarchy V$ver//EN
      ent/iso-amsa.ent	ISO 8879:1986//ENTITIES Added Math Symbols: Arrow Relations//EN
      ent/iso-amso.ent	ISO 8879:1986//ENTITIES Added Math Symbols: Ordinary//EN
      ent/iso-cyr1.ent	ISO 8879:1986//ENTITIES Russian Cyrillic//EN
      ent/iso-tech.ent	ISO 8879:1986//ENTITIES General Technical//EN
      ent/iso-amsc.ent	ISO 8879:1986//ENTITIES Added Math Symbols: Delimiters//EN
      soextblx.dtd	-//OASIS//DTD XML Exchange Table Model 19990315//EN
      calstblx.dtd	-//OASIS//DTD DocBook XML CALS Table Model V$ver//EN
      ent/iso-lat1.ent	ISO 8879:1986//ENTITIES Added Latin 1//EN
      ent/iso-amsb.ent	ISO 8879:1986//ENTITIES Added Math Symbols: Binary Operators//EN
      ent/iso-lat2.ent	ISO 8879:1986//ENTITIES Added Latin 2//EN
      ent/iso-amsr.ent	ISO 8879:1986//ENTITIES Added Math Symbols: Relations//EN
      ent/iso-cyr2.ent	ISO 8879:1986//ENTITIES Non-Russian Cyrillic//EN
ENDENT
    for f in System URI; do
    /usr/bin/xmlcatalog --noout --add rewrite$f "http://www.oasis-open.org/docbook/xml/$ver" \
	$sgmldir $xmlcatalog
    done
  fi
done

%postun
%triggerin -- openjade >= 1.3.2
eval set %{all_catalog}
for dir
do
  fmt=${dir#*-} ver=${dir%%-*}
  /usr/bin/xmlcatalog --sgml --noout --add /etc/sgml/$fmt-docbook-$ver.cat \
    /usr/share/sgml/openjade-1.3.2/catalog
done

%files
%config(noreplace) /etc/sgml/*ml-docbook.cat
%ghost %config(noreplace) /etc/sgml/*ml-docbook-*.cat
%{_pkgdocdir}
/usr/share/sgml/docbook/*ml-dtd-*
/usr/share/sgml/docbook/rng-*
/usr/share/sgml/docbook/xsd-*

%changelog
* Fri Nov 08 2019 openEuler Buildteam <buildteam@openeuler.org> - 1.0-78
- fix libkcapi build error.

* Fri Nov 01 2019 openEuler Buildteam <buildteam@openeuler.org> - 1.0-77
- fix wpa_supplicant build error.

* Tue Oct 29 2019 openEuler Buildteam <buildteam@openeuler.org> - 1.0-76
- optimize spec file.

* Fri Oct 18 2019 openEuler Buildteam <buildteam@openeuler.org> - 1.0-75
- fix xmlto I/O error.

* Sun Sep 29 2019 openEuler Buildteam <buildteam@openeuler.org> - 1.0-74
- fix %post scriptlet failed.

* Tue Sep 24 2019 openEuler Buildteam <buildteam@openeuler.org> - 1.0-73
- Package rebuild.

* Fri Sep 20 2019 openEuler Buildteam <buildteam@openeuler.org> - 1.0-72
- Package init.
