#!/bin/bash
################################################################################
# Script Name: version/2.1.19/UPDATE.sh
# Description: Rebuild the webmail from this release, on servers where every
#              rebuild has been taking the source from installation day.
# Company: hitechcloud.org
# Copyright (c) hitechcloud.org
################################################################################
#
# `native-webmail update` looked for its source under the native root before
# the release bundle. /etc/hitechcloud/native/webmail-src is written by
# INSTALL.sh and by nothing else, so on a server installed once and updated
# since, every rebuild took the webmail from the day of the install, built it,
# and reported "rebuilt from the shipped source". 2.1.18's webmail - reading
# S/MIME mail, BIMI sender marks, the operator's own branding - could not reach
# any of them.
#
# The order is corrected in the CLI this update has already installed. Two
# things are left for here:
#
#   1. Refresh the installer's staging copy, so the stale directory stops
#      being stale whatever reads it.
#
#   2. Rebuild the webmail. Not inline: npm ci and a frontend build take longer
#      than an update is allowed to spend, and a build killed half way leaves a
#      webmail that does not start. It runs in a unit of its own and reports
#      what happened into the panel's notifications, because a rebuild nobody
#      is told about is one nobody knows to check.
#
# A server with no webmail installed does nothing here.
################################################################################

set -uo pipefail

GREEN='\033[0;32m'; YELLOW='\033[1;33m'; NC='\033[0m'
ok()   { echo -e "[2.1.19] ${GREEN}✓${NC} $*"; }
warn() { echo -e "[2.1.19] ${YELLOW}!${NC} $*" >&2; }
say()  { echo -e "[2.1.19] $*"; }

[[ $EUID -eq 0 ]] || { echo "[2.1.19] This update must run as root." >&2; exit 1; }

[[ -f /etc/hitechcloud/native/native.conf ]] || { say "not the native stack; nothing to do"; exit 0; }

if ! systemctl list-unit-files hitechcloud-webmail.service >/dev/null 2>&1 ||
   ! systemctl is-enabled hitechcloud-webmail >/dev/null 2>&1; then
    say "no webmail on this server; nothing to do"
    exit 0
fi

BUNDLE=/opt/hitechcloud/hitechcloudmail/webmail
STAGING=/etc/hitechcloud/native/webmail-src

# 1. The staging copy, brought up to what this release ships.
if [[ -f "${BUNDLE}/backend/package.json" && -d "$STAGING" ]]; then
    if command -v rsync >/dev/null 2>&1; then
        # node_modules is kept: it is the expensive half and rebuilding it for
        # no reason adds minutes to this.
        rsync -a --delete --exclude 'node_modules/' "${BUNDLE}/" "${STAGING}/" 2>/dev/null &&
            ok "The installer's staging copy now matches this release" ||
            warn "Could not refresh ${STAGING}"
    else
        warn "rsync is missing; ${STAGING} is left as it was"
    fi
fi

# 2. The rebuild, out of this update's way.
if command -v systemd-run >/dev/null 2>&1; then
    systemd-run --collect --unit=hitechcloud-webmail-rebuild \
        --description="HiTechCloud webmail rebuild (2.1.19)" \
        --property=TimeoutStartSec=3600 \
        /bin/bash -c '
            if hitechcloudcli native-webmail update >/var/log/hitechcloud/webmail-rebuild.log 2>&1; then
                hitechcloudcli sentinel --action=webmail_rebuild \
                    --title="Webmail rebuilt" \
                    --message="The webmail was rebuilt from release 2.1.19: S/MIME, sender marks and branding are now live." >/dev/null 2>&1
            else
                hitechcloudcli sentinel --action=webmail_rebuild \
                    --title="Webmail rebuild failed" \
                    --message="The webmail could not be rebuilt. The log is at /var/log/hitechcloud/webmail-rebuild.log; re-run: hitechcloudcli native-webmail update" >/dev/null 2>&1
            fi
        ' >/dev/null 2>&1 &&
        ok "Webmail rebuild started in the background; it takes a few minutes and reports into Notifications" ||
        warn "The webmail rebuild could not be started - run it by hand: hitechcloudcli native-webmail update"
else
    warn "systemd-run is missing; rebuild the webmail by hand: hitechcloudcli native-webmail update"
fi

ok "2.1.19 update complete"
exit 0
