#!/bin/bash

set -e

PROGNAME=$(basename "$0")

UPGRADE_TRIGGER='/var/lib/grommunio-files/pending_upgrade'
FILES_BASEDIR='/usr/share/grommunio-files'
FILES_LOGDIR='/var/log/grommunio-files'

occ () {
  sudo -u grofiles ${FILES_BASEDIR}/occ "$@" --no-interaction >> ${FILES_LOGDIR}/upgrade.log 2>&1
}

occ_get () {
  sudo -u grofiles ${FILES_BASEDIR}/occ "$@" --no-interaction 2>/dev/null
}

# overwrite.cli.url must not contain overwritewebroot: in CLI context (cron,
# occ) grommunio Files prepends it to paths that already start with the webroot,
# yielding /files/files/... URLs. This broke the ONLYOFFICE background check
# (EditorsCheck), which then flagged the document server as unavailable.
# A StorageUrl of <cli-url>/ has no effect in web context and only adds the
# same doubling in CLI context, so drop it as well.
fix_cli_urls () {
  local WEBROOT CLI_URL STORAGE_URL
  WEBROOT=$(occ_get config:system:get overwritewebroot) || return 0
  WEBROOT="${WEBROOT%/}"
  [ -n "${WEBROOT}" ] || return 0
  CLI_URL=$(occ_get config:system:get overwrite.cli.url) || return 0
  CLI_URL="${CLI_URL%/}"
  case "${CLI_URL}" in
    *"${WEBROOT}")
      STORAGE_URL=$(occ_get config:app:get onlyoffice StorageUrl) ||:
      if [ "${STORAGE_URL%/}" = "${CLI_URL}" ] ; then
        occ config:app:delete onlyoffice StorageUrl ||:
      fi
      occ config:system:set overwrite.cli.url --value "${CLI_URL%"${WEBROOT}"}" ||:
      ;;
  esac
}

do_upgrade () {
  local OLD_UMASK=$(umask)
  umask 0077
  ! test -f "${FILES_LOGDIR}/upgrade.log" && touch "${FILES_LOGDIR}/upgrade.log" ||:
  umask "$OLD_UMASK"
  if [ -e "${UPGRADE_TRIGGER}" ] ; then
    # only upgrade instances that have actually been set up (occ maintenance:install)
    if [ -s ${FILES_BASEDIR}/config/config.php ] && grep -q "'installed' => true" ${FILES_BASEDIR}/config/config.php ; then
      occ maintenance:mode --on ||:
      occ upgrade ||:
      occ db:add-missing-columns ||:
      occ db:add-missing-primary-keys ||:
      occ db:add-missing-indices ||:
      occ db:convert-filecache-bigint ||:
      echo "ALTER DATABASE grofiles CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;" | mysql \
        >> ${FILES_LOGDIR}/upgrade.log 2>&1 ||:
      occ config:system:set mysql.utf8mb4 --type boolean --value true ||:
      occ maintenance:repair --include-expensive ||:
      occ maintenance:mode --off ||:
      occ config:system:set integrity.check.disabled --type boolean --value true ||:
      occ theming:config name 'grommunio Files' ||:
      occ theming:config logo ${FILES_BASEDIR}/logo.svg ||:
      occ theming:config logoheader ${FILES_BASEDIR}/logo.svg ||:
      occ theming:config favicon ${FILES_BASEDIR}/favicon.svg ||:
      occ theming:config background ${FILES_BASEDIR}/background.jpg ||:
      occ theming:config -r disable-user-theming ||:
      occ theming:config slogan 'filesync & sharing' ||:
      occ theming:config url 'https://grommunio.com' ||:
      occ theming:config primary_color '#2A2B30' ||:
      # firstrunwizard (intro video on first login) is not shipped anymore
      occ app:disable firstrunwizard ||:
      occ app:enable user_external ||:
      occ app:enable grommunio_brand ||:
      occ config:system:set maintenance_window_start --type=integer --value=1 ||:
      occ config:system:set check_for_working_wellknown_setup --type=integer --value=0 ||:
      occ config:app:set onlyoffice DocumentServerUrl --value "/office/" ||:
      fix_cli_urls ||:
      if ! sudo -u grofiles ${FILES_BASEDIR}/occ config:system:get default_phone_region >/dev/null 2>&1 ; then
        occ config:system:set default_phone_region --value='43' ||:
      fi
    fi
    rm -f "${UPGRADE_TRIGGER}"
  fi
}

trigger_upgrade () {
  touch "${UPGRADE_TRIGGER}"
}

case "$PROGNAME" in
	grommunio-files-upgrade)
		do_upgrade
		;;
	grommunio-files-prepare-upgrade)
		trigger_upgrade
		;;
esac

