MINGW_INSTALL=ucrt64
MINGW_XML_CATALOG=${MINGW_INSTALL}/etc/xml
# export MSYS2_ARG_CONV_EXCL="-//OASIS"
_vers_4x=(4.{2..5})

post_install() {
  if [ -e ${MINGW_XML_CATALOG}/catalog.preserve ]; then
    mv ${MINGW_XML_CATALOG}/catalog.preserve ${MINGW_XML_CATALOG}/catalog
  elif [ ! -e ${MINGW_INSTALL}/etc/xml/catalog ]; then
    ${MINGW_INSTALL}/bin/xmlcatalog.exe --noout --create ${MINGW_XML_CATALOG}/catalog
  fi
  ${MINGW_INSTALL}/bin/xmlcatalog.exe --noout --add "delegatePublic" \
    "-//OASIS//ENTITIES DocBook XML" \
    "./docbook-xml" \
    ${MINGW_XML_CATALOG}/catalog
  ${MINGW_INSTALL}/bin/xmlcatalog.exe --noout --add "delegatePublic" \
    "-//OASIS//DTD DocBook XML" \
    "./docbook-xml" \
    ${MINGW_XML_CATALOG}/catalog
  ${MINGW_INSTALL}/bin/xmlcatalog.exe --noout --add "delegateSystem" \
    "http://www.oasis-open.org/docbook/" \
    "./docbook-xml" \
    ${MINGW_XML_CATALOG}/catalog
  ${MINGW_INSTALL}/bin/xmlcatalog.exe --noout --add "delegateURI" \
    "http://www.oasis-open.org/docbook/" \
    "./docbook-xml" \
    ${MINGW_XML_CATALOG}/catalog
}

# arg 1:  the new package version
# arg 2:  the old package version
pre_upgrade() {
  if [ $(vercmp $2 4.5) -lt 0 ]; then
    ${MINGW_INSTALL}/bin/xmlcatalog.exe --del "${MINGW_XML_CATALOG}/docbook" ${MINGW_XML_CATALOG}/catalog > ${MINGW_XML_CATALOG}/catalog.preserve
    echo ":: docbook5 is now in docbook5-xml"
  fi
}

post_upgrade() {
  if [ $(vercmp $2 4.5) -ge 0 ]; then
    post_remove
  fi
  post_install
}

post_remove() {
  ${MINGW_INSTALL}/bin/xmlcatalog.exe --noout --del ./docbook-xml ${MINGW_XML_CATALOG}/catalog
}
