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

_vers_4x=(4.{2..5})
_realname=docbook-xml
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=${_vers_4x[-1]}
pkgrel=3
epoch=1
pkgdesc="A widely used XML scheme for writing documentation and help"
url="https://www.oasis-open.org/docbook/"
arch=(any)
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
license=(MIT)
depends=("${MINGW_PACKAGE_PREFIX}-libxml2")
install=docbook-xml-${MSYSTEM}.install
source=(https://docbook.org/xml/4.1.2/docbkx412.zip
        LICENSE)
noextract=(docbkx412.zip)
for _ver in ${_vers_5x[@]}; do
  source+=("https://docbook.org/xml/$_ver/docbook-${_ver}.zip")
  noextract+=("docbook-${_ver}.zip")
done
noextract=(docbkx412.zip)

for _ver in ${_vers_4x[@]}; 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${MINGW_PREFIX}/etc/xml"
  ${MINGW_PREFIX}/bin/xmlcatalog --noout --create "$pkgdir${MINGW_PREFIX}/etc/xml/docbook-xml"

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

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

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

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

  install -Dt "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}" -m644 LICENSE

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

