# Contributor: dorphell <dorphell@archlinux.org>
# Contributor: Judd Vinet <jvinet@zeroflux.org>

_vers=(4.{2..5})

pkgname=docbook-xml
pkgver=${_vers[-1]}
pkgrel=9
pkgdesc="A widely used XML scheme for writing documentation and help"
url="https://www.oasis-open.org/docbook/"
arch=(any)
license=(MIT)
depends=(libxml2)
install=docbook-xml.install
source=(https://docbook.org/xml/4.1.2/docbkx412.zip
        LICENSE)
noextract=(docbkx412.zip)

for _ver in ${_vers[@]}; do
  source+=("https://docbook.org/xml/$_ver/docbook-xml-$_ver.zip")
  noextract+=("docbook-xml-$_ver.zip")
done
  
sha512sums=('f700591a671694ca0ac51f5b5b7e825df5c0b3604b20baa6afd3aaafa7ce99470ca1c261781b105b42bfa5485c23217cf3db821b3fcf2ebdae9df07bb8ae4063'
            'd852ab8e1442af4a91ffc32b9bb37377d98171dbc379cfd9787a2e06fc5c9b8ed04c5cd156ff5b7799973250011389456a3a3584ed4ae99362420c15235fcbb5'
            '0c836346130d1e8f4e26e00959f6b4fd2c3c11269ba5cbf11cdc904724e189606f431c99cd5ab188877daa0eb44c58d0bc30556df3b51df480396818d61c4e0a'
            'f5090fb74884bae3d4fac8a3c5060bffff5d6a74272de183c181a7642e4b91f4ed32ad028537d198010782c3d98575ce679672f76a9749ed124432195886a7cb'
            '7df5af4df24e4618b09814e4e20c147c722962531f03a40c28cd60f1db16b4c330420adf96adb7d66ed6eda84046ee91b467fd6f6fbfac2201537e2080735d76'
            '1ee282fe86c9282610ee72c0e1d1acfc03f1afb9dc67166f438f2703109046479edb6329313ecb2949db27993077e077d111501c10b8769ebb20719eb6213d27')

package() {
  local ver xml

  mkdir -p "$pkgdir/etc/xml"
  xmlcatalog --noout --create "$pkgdir/etc/xml/docbook-xml"

  mkdir docbook-xml-4.1.2
  bsdtar -C docbook-xml-4.1.2 -xf docbkx412.zip
  for ver in ${_vers[@]}; do
    mkdir docbook-xml-$ver
    bsdtar -C docbook-xml-$ver -xf docbook-xml-$ver.zip
  done

  for ver in 4.1.2 ${_vers[@]}; do
    pushd docbook-xml-$ver
    mkdir -p "$pkgdir/usr/share/xml/docbook/xml-dtd-$ver"
    cp -dr docbook.cat *.dtd ent/ *.mod \
        "$pkgdir/usr/share/xml/docbook/xml-dtd-$ver"
    popd

    xml=
    case $ver in
      4.1.2) xml=' XML' ;;&
      *)
        xmlcatalog --noout --add "public" \
          "-//OASIS//DTD DocBook XML V$ver//EN" \
          "http://www.oasis-open.org/docbook/xml/$ver/docbookx.dtd" \
          "$pkgdir/etc/xml/docbook-xml"
        xmlcatalog --noout --add "public" \
          "-//OASIS//DTD DocBook$xml CALS Table Model V$ver//EN" \
          "http://www.oasis-open.org/docbook/xml/$ver/calstblx.dtd" \
          "$pkgdir/etc/xml/docbook-xml"
        xmlcatalog --noout --add "public" \
          "-//OASIS//DTD XML Exchange Table Model 19990315//EN" \
          "http://www.oasis-open.org/docbook/xml/$ver/soextblx.dtd" \
          "$pkgdir/etc/xml/docbook-xml"
        xmlcatalog --noout --add "public" \
          "-//OASIS//ELEMENTS DocBook$xml Information Pool V$ver//EN" \
          "http://www.oasis-open.org/docbook/xml/$ver/dbpoolx.mod" \
          "$pkgdir/etc/xml/docbook-xml"
        xmlcatalog --noout --add "public" \
          "-//OASIS//ELEMENTS DocBook$xml Document Hierarchy V$ver//EN" \
          "http://www.oasis-open.org/docbook/xml/$ver/dbhierx.mod" \
          "$pkgdir/etc/xml/docbook-xml"
        xmlcatalog --noout --add "public" \
          "-//OASIS//ENTITIES DocBook$xml Additional General Entities V$ver//EN" \
          "http://www.oasis-open.org/docbook/xml/$ver/dbgenent.mod" \
          "$pkgdir/etc/xml/docbook-xml"
        xmlcatalog --noout --add "public" \
          "-//OASIS//ENTITIES DocBook$xml Notations V$ver//EN" \
          "http://www.oasis-open.org/docbook/xml/$ver/dbnotnx.mod" \
          "$pkgdir/etc/xml/docbook-xml"
        xmlcatalog --noout --add "public" \
          "-//OASIS//ENTITIES DocBook$xml Character Entities V$ver//EN" \
          "http://www.oasis-open.org/docbook/xml/$ver/dbcentx.mod" \
          "$pkgdir/etc/xml/docbook-xml"
        ;;&
      4.[45])
        xmlcatalog --noout --add "public" \
          "-//OASIS//ELEMENTS DocBook XML HTML Tables V$ver//EN" \
          "http://www.oasis-open.org/docbook/xml/$ver/htmltblx.mod" \
          "$pkgdir/etc/xml/docbook-xml"
        ;;&
      *)
        xmlcatalog --noout --add "rewriteSystem" \
          "http://www.oasis-open.org/docbook/xml/$ver" \
          "/usr/share/xml/docbook/xml-dtd-$ver" \
          "$pkgdir/etc/xml/docbook-xml"
        xmlcatalog --noout --add "rewriteURI" \
          "http://www.oasis-open.org/docbook/xml/$ver" \
          "/usr/share/xml/docbook/xml-dtd-$ver" \
          "$pkgdir/etc/xml/docbook-xml"
        ;;&
    esac
  done

  install -Dt "$pkgdir/usr/share/licenses/$pkgname" -m644 LICENSE

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

# vim:set sw=2 et:
