pkgname=docbook-xml
pkgver=4.5
pkgdesc="A widely used XML scheme for writing documentation and help"
homepage="https://www.oasis-open.org/docbook/"
arch=(any)
license=(MIT)
sources=("${pkgname}-${pkgver}.zip")
urls=("https://www.docbook.org/xml/${pkgver}/${sources[0]}")
md5sums=("03083e288e87a7e829e437358da7ef9e")

_compat_4x=(4.1.2 4.2 4.3 4.4)

src_prepare() {
  local z="${distdir}/${sources[0]}"
  bsdtar -xf "$z"
}

src_install() {
  local dtd_dir="${pkgdir}/usr/share/xml/docbook/xml-dtd-${pkgver}"
  local etc_xml="${pkgdir}/etc/xml"
  local cat_docbook="${etc_xml}/docbook"

  install -v -d -m755 "$dtd_dir"
  cp -v -af --no-preserve=ownership docbook.cat *.dtd ent/ *.mod "$dtd_dir"

  install -v -d -m755 "$etc_xml"
  xmlcatalog --noout --create "$cat_docbook"

  xmlcatalog --noout --add "public" \
    "-//OASIS//DTD DocBook XML V${pkgver}//EN" \
    "http://www.oasis-open.org/docbook/xml/${pkgver}/docbookx.dtd" \
    "$cat_docbook"

  xmlcatalog --noout --add "public" \
    "-//OASIS//DTD DocBook XML CALS Table Model V${pkgver}//EN" \
    "file:///usr/share/xml/docbook/xml-dtd-${pkgver}/calstblx.dtd" \
    "$cat_docbook"

  xmlcatalog --noout --add "public" \
    "-//OASIS//DTD XML Exchange Table Model 19990315//EN" \
    "file:///usr/share/xml/docbook/xml-dtd-${pkgver}/soextblx.dtd" \
    "$cat_docbook"

  xmlcatalog --noout --add "public" \
    "-//OASIS//ELEMENTS DocBook XML Information Pool V${pkgver}//EN" \
    "file:///usr/share/xml/docbook/xml-dtd-${pkgver}/dbpoolx.mod" \
    "$cat_docbook"

  xmlcatalog --noout --add "public" \
    "-//OASIS//ELEMENTS DocBook XML Document Hierarchy V${pkgver}//EN" \
    "file:///usr/share/xml/docbook/xml-dtd-${pkgver}/dbhierx.mod" \
    "$cat_docbook"

  xmlcatalog --noout --add "public" \
    "-//OASIS//ELEMENTS DocBook XML HTML Tables V${pkgver}//EN" \
    "file:///usr/share/xml/docbook/xml-dtd-${pkgver}/htmltblx.mod" \
    "$cat_docbook"

  xmlcatalog --noout --add "public" \
    "-//OASIS//ENTITIES DocBook XML Notations V${pkgver}//EN" \
    "file:///usr/share/xml/docbook/xml-dtd-${pkgver}/dbnotnx.mod" \
    "$cat_docbook"

  xmlcatalog --noout --add "public" \
    "-//OASIS//ENTITIES DocBook XML Character Entities V${pkgver}//EN" \
    "file:///usr/share/xml/docbook/xml-dtd-${pkgver}/dbcentx.mod" \
    "$cat_docbook"

  xmlcatalog --noout --add "public" \
    "-//OASIS//ENTITIES DocBook XML Additional General Entities V${pkgver}//EN" \
    "file:///usr/share/xml/docbook/xml-dtd-${pkgver}/dbgenent.mod" \
    "$cat_docbook"

  xmlcatalog --noout --add "rewriteSystem" \
    "http://www.oasis-open.org/docbook/xml/${pkgver}" \
    "file:///usr/share/xml/docbook/xml-dtd-${pkgver}" \
    "$cat_docbook"

  xmlcatalog --noout --add "rewriteURI" \
    "http://www.oasis-open.org/docbook/xml/${pkgver}" \
    "file:///usr/share/xml/docbook/xml-dtd-${pkgver}" \
    "$cat_docbook"

  for DTDVERSION in "${_compat_4x[@]}"; do
    xmlcatalog --noout --add "public" \
      "-//OASIS//DTD DocBook XML V${DTDVERSION}//EN" \
      "http://www.oasis-open.org/docbook/xml/${DTDVERSION}/docbookx.dtd" \
      "$cat_docbook"

    xmlcatalog --noout --add "rewriteSystem" \
      "http://www.oasis-open.org/docbook/xml/${DTDVERSION}" \
      "file:///usr/share/xml/docbook/xml-dtd-${pkgver}" \
      "$cat_docbook"

    xmlcatalog --noout --add "rewriteURI" \
      "http://www.oasis-open.org/docbook/xml/${DTDVERSION}" \
      "file:///usr/share/xml/docbook/xml-dtd-${pkgver}" \
      "$cat_docbook"
  done

  # Fix permissions
  find "$pkgdir" -type f -exec chmod -c a-x {} +
  chmod -Rc u=rwX,go=rX "$pkgdir"
}

src_postinstall() {
  install -v -d -m755 /etc/xml

  if [ ! -e /etc/xml/catalog ]; then
    xmlcatalog --noout --create /etc/xml/catalog
  fi

  xmlcatalog --noout --add "delegatePublic" \
    "-//OASIS//ENTITIES DocBook XML" \
    "file:///etc/xml/docbook" \
    /etc/xml/catalog

  xmlcatalog --noout --add "delegatePublic" \
    "-//OASIS//DTD DocBook XML" \
    "file:///etc/xml/docbook" \
    /etc/xml/catalog

  xmlcatalog --noout --add "delegateSystem" \
    "http://www.oasis-open.org/docbook/" \
    "file:///etc/xml/docbook" \
    /etc/xml/catalog

  xmlcatalog --noout --add "delegateURI" \
    "http://www.oasis-open.org/docbook/" \
    "file:///etc/xml/docbook" \
    /etc/xml/catalog

  for DTDVERSION in "${_compat_4x[@]}"; do
    xmlcatalog --noout --add "delegateSystem" \
      "http://www.oasis-open.org/docbook/xml/${DTDVERSION}/" \
      "file:///etc/xml/docbook" \
      /etc/xml/catalog
    xmlcatalog --noout --add "delegateURI" \
      "http://www.oasis-open.org/docbook/xml/${DTDVERSION}/" \
      "file:///etc/xml/docbook" \
      /etc/xml/catalog
  done
}

src_postremove() {
  if [ -e /etc/xml/catalog ]; then
    xmlcatalog --noout --del "file:///etc/xml/docbook" /etc/xml/catalog
  fi
}
