# This package contains the assets for carbonio ui components (aka zapp)

# the package uses commits paths to reduce caching issues as much as possible
# but it doesn't support multiple versions installed at the same time
# this could lead to a loading issue if a user is loading the page exactly during the
# upgrade, but so far there is nothing we can do about it (we would need to coordinate multiple nginx).

pkgname="carbonio-admin-ui"
pkgver="0.9.36"
pkgrel="1"
pkgdesc="The Zextras Carbonio web admin"
maintainer="Zextras (packages@zextras.com)"
arch=("x86_64")
license=("AGPL-3.0-only")
copyright=("2022, Zextras &lt;https://www.zextras.com&gt;")
section="admin"
priority="optional"
url="https://github.com/zextras"
depends=(
  "carbonio-nginx"
  "carbonio-webui-i18n"
  "jq"
)

package() {
  cd "${srcdir}"
  mkdir -p "${pkgdir}/opt/zextras/admin/iris/${pkgname}/9b76f68e3ffabee287d8978fd5554ec4b702cd80"
  cp -a  ../../dist/* "${pkgdir}/opt/zextras/admin/iris/${pkgname}/9b76f68e3ffabee287d8978fd5554ec4b702cd80"
  chown root:root -R "${pkgdir}/opt/zextras/admin/iris/${pkgname}/9b76f68e3ffabee287d8978fd5554ec4b702cd80"
  chmod 644 -R "${pkgdir}/opt/zextras/admin/iris/${pkgname}/9b76f68e3ffabee287d8978fd5554ec4b702cd80"
  find "${pkgdir}/opt/zextras/admin/iris/${pkgname}/9b76f68e3ffabee287d8978fd5554ec4b702cd80" -type d -exec chmod a+x "{}" \;
  ln -sf /opt/zextras/admin/iris/${pkgname}/i18n "${pkgdir}/opt/zextras/admin/iris/${pkgname}/9b76f68e3ffabee287d8978fd5554ec4b702cd80/i18n"
}

postinst() {
  # copy the index.html to the current directory, no redirect is needed
  mkdir -p "/opt/zextras/admin/iris/carbonio-admin-ui/current"

  # not every package has a index.html
  cd "/opt/zextras/admin/iris/carbonio-admin-ui/9b76f68e3ffabee287d8978fd5554ec4b702cd80"
  find . -name "*.html" -exec cp --parents "{}" /opt/zextras/admin/iris/carbonio-admin-ui/current/ \;

  # re-generate the component list, for every component
  # depth should be 3 since the path should be iris/NAME/COMMIT/component.json
  find /opt/zextras/admin/iris/ -maxdepth 3 -mindepth 3 -type f -name component.json -print |\
    xargs jq -s '{"components":.}' > /opt/zextras/admin/iris/components.json
}
