pkgname=docbook-xsl-nons
pkgver=1.79.2
_lfsver=12.4
pkgdesc='XML stylesheets for Docbook-xml transformations'
homepage='https://docbook.org/'
arch=(any)
license=(LicenseRef-docbook-XSL)
sources=("${pkgname}-${pkgver}.tar.bz2"
  "${pkgname}-${pkgver}-stack_fix-1.patch"
)
urls=("https://github.com/docbook/xslt10-stylesheets/releases/download/release/${pkgver}/${sources[0]}"
  "https://www.linuxfromscratch.org/patches/blfs/${_lfsver}/${sources[1]}"
)
md5sums=("2666d1488d6ced1551d15f31d7ed8c38"
  "8457bd6ee6b06fbc6dc540e1b096f766"
)


src_prepare() {
  tar -xf ${distdir}/${sources[0]} --strip-components=1
  patch -p1 -i ${distdir}/${sources[1]}
}

src_install() {
  install -v -m755 -d ${pkgdir}/usr/share/xml/docbook/xsl-stylesheets-nons-${pkgver}

  cp -v -R VERSION assembly common eclipse epub epub3 extensions fo        \
           highlighting html htmlhelp images javahelp lib manpages params  \
           profiling roundtrip slides template tests tools webhelp website \
           xhtml xhtml-1_1 xhtml5                                          \
      ${pkgdir}/usr/share/xml/docbook/xsl-stylesheets-nons-${pkgver}

  ln -s VERSION ${pkgdir}/usr/share/xml/docbook/xsl-stylesheets-nons-${pkgver}/VERSION.xsl

  install -v -m644 -D README \
                      ${pkgdir}/usr/share/doc/docbook-xsl-nons-${pkgver}/README.txt

  install -v -m644    RELEASE-NOTES* NEWS* \
                      ${pkgdir}/usr/share/doc/docbook-xsl-nons-${pkgver}
}

src_postinstall() {
  if [ ! -d /etc/xml ]; then install -v -m755 -d /etc/xml; fi &&
  if [ ! -f /etc/xml/catalog ]; then
      xmlcatalog --noout --create /etc/xml/catalog
  fi &&

  xmlcatalog --noout --add "rewriteSystem"                           \
             "http://cdn.docbook.org/release/xsl-nons/${pkgver}"     \
             "/usr/share/xml/docbook/xsl-stylesheets-nons-${pkgver}" \
             /etc/xml/catalog &&

  xmlcatalog --noout --add "rewriteSystem"                        \
             "https://cdn.docbook.org/release/xsl-nons/${pkgver}"    \
             "/usr/share/xml/docbook/xsl-stylesheets-nons-${pkgver}" \
             /etc/xml/catalog &&

  xmlcatalog --noout --add "rewriteURI"                           \
             "http://cdn.docbook.org/release/xsl-nons/${pkgver}"     \
             "/usr/share/xml/docbook/xsl-stylesheets-nons-${pkgver}" \
             /etc/xml/catalog &&

  xmlcatalog --noout --add "rewriteURI"                           \
             "https://cdn.docbook.org/release/xsl-nons/${pkgver}"    \
             "/usr/share/xml/docbook/xsl-stylesheets-nons-${pkgver}" \
             /etc/xml/catalog &&

  xmlcatalog --noout --add "rewriteSystem"                        \
             "http://cdn.docbook.org/release/xsl-nons/current"    \
             "/usr/share/xml/docbook/xsl-stylesheets-nons-${pkgver}" \
             /etc/xml/catalog &&

  xmlcatalog --noout --add "rewriteSystem"                        \
             "https://cdn.docbook.org/release/xsl-nons/current"   \
             "/usr/share/xml/docbook/xsl-stylesheets-nons-${pkgver}" \
             /etc/xml/catalog &&

  xmlcatalog --noout --add "rewriteURI"                           \
             "http://cdn.docbook.org/release/xsl-nons/current"    \
             "/usr/share/xml/docbook/xsl-stylesheets-nons-${pkgver}" \
             /etc/xml/catalog &&

  xmlcatalog --noout --add "rewriteURI"                           \
             "https://cdn.docbook.org/release/xsl-nons/current"   \
             "/usr/share/xml/docbook/xsl-stylesheets-nons-${pkgver}" \
             /etc/xml/catalog &&

  xmlcatalog --noout --add "rewriteSystem"                        \
             "http://docbook.sourceforge.net/release/xsl/current" \
             "/usr/share/xml/docbook/xsl-stylesheets-nons-${pkgver}" \
             /etc/xml/catalog &&

  xmlcatalog --noout --add "rewriteURI"                           \
             "http://docbook.sourceforge.net/release/xsl/current" \
             "/usr/share/xml/docbook/xsl-stylesheets-nons-${pkgver}" \
             /etc/xml/catalog
}

src_postremove() {
  if [ -e /etc/xml/catalog ]; then
    xmlcatalog --noout --del "/usr/share/xml/docbook/xsl-stylesheets-${pkgver}-nons" /etc/xml/catalog
  fi
}
