#!/bin/bash
################################################################################
# Script Name: version/3.7.6/UPDATE.sh
# Description: Apply version 3.7.6:
#              - the CLI's errors go to stderr, and --help answers the same way
#                whatever the binary is invoked as
#              - four commands stop advertising flags nothing implements
#              - breakout rooms are reachable from the meeting room
#              - the meeting settings panel stops advertising a recording
#                destination that does not exist
#              Checks the two things whose absence is invisible: whether the
#              compiled CLI is the entry point (the CLI half of this release is
#              in that binary) and whether the webmail was rebuilt (the meet
#              half is in the frontend bundle).
# Company/Brand: HiTechCloud by Pho Tue SoftWare Solutions JSC
################################################################################

set -uo pipefail
GREEN='\033[0;32m'; YELLOW='\033[1;33m'; RED='\033[0;31m'; NC='\033[0m'
ok()   { echo -e "[3.7.6] ${GREEN}✓${NC} $*"; }
warn() { echo -e "[3.7.6] ${YELLOW}!${NC} $*" >&2; }
FAILURES=0
bad()  { echo -e "[3.7.6] ${RED}✗${NC} $*" >&2; FAILURES=$((FAILURES + 1)); }
[[ $EUID -eq 0 ]] || { echo "[3.7.6] This update must run as root." >&2; exit 1; }

VERSION="3.7.6"
BIN=/usr/local/bin
CRON=/etc/cron.d/hitechcloud
WEBMAIL_SRC="${HITECHCLOUD_WEBMAIL_SRC:-/opt/hitechcloud/hitechcloudmail/webmail}"
DRY_RUN="${HITECHCLOUD_UPDATE_DRY_RUN:-0}"
run() { if [[ "$DRY_RUN" == 1 ]]; then echo "[3.7.6] would: $*"; else "$@"; fi; }

# A dry run writes nothing, including this - see version/README.md.
if [[ "$DRY_RUN" == 1 ]]; then
    echo "[3.7.6] would: record version ${VERSION} in /usr/local/hitechcloud/version and /etc/hitechcloud/version"
else
    mkdir -p /usr/local/hitechcloud /etc/hitechcloud 2>/dev/null || true
    echo "$VERSION" > /usr/local/hitechcloud/version 2>/dev/null || true
    echo "$VERSION" > /etc/hitechcloud/version 2>/dev/null || true
fi

is_elf() { [[ -f "$1" ]] && [[ "$(od -An -t x1 -N 4 "$1" 2>/dev/null | tr -d ' \n')" == "7f454c46" ]]; }

# 1. The CLI half of this release lives in the compiled binary: errors on
#    stderr, one answer to `<command> --help` whatever the program is invoked
#    as, and four commands that no longer advertise flags they ignore. A server
#    still answering from the shell dispatcher has none of it.
if is_elf "${BIN}/hitechcloudcli"; then
    ok "hitechcloudcli is the compiled entry point"
elif [[ -e "${BIN}/hitechcloudcli" ]]; then
    bad "${BIN}/hitechcloudcli is the shell dispatcher, so this release's CLI fixes are not installed.
  Put it right with:  hitechcloudcli update"
else
    bad "there is no ${BIN}/hitechcloudcli on this server."
fi

# 2. The meeting-room half is in the built frontend, so it arrives only if the
#    webmail was rebuilt. Checked by looking for a file only this release has.
if systemctl list-unit-files 2>/dev/null | grep -q '^hitechcloud-webmail\.service'; then
    marker="${WEBMAIL_SRC}/frontend/src/plugins/meet/RoomPanels.breakouts.render.test.js"
    if [[ -f "$marker" ]]; then
        ok "webmail source is this release"
    else
        run "${BIN}/hitechcloudcli" native-webmail update >/dev/null 2>&1 \
            && ok "webmail rebuilt from this release" \
            || warn "the webmail is still the previous release, so the meeting room has no breakout rooms yet; run: hitechcloudcli native-webmail update"
    fi
fi

# 3. The scheduled jobs, reported rather than repaired: 3.7.5's script runs on
#    the way here and repairs them, so finding them missing now means that did
#    not happen and an operator should know which step to look at.
if [[ -f "$CRON" ]] && grep -q 'hitechcloudcli sentinel' "$CRON" 2>/dev/null; then
    ok "scheduled jobs in place ($(grep -c 'hitechcloudcli' "$CRON" 2>/dev/null || echo 0) job(s))"
else
    bad "no scheduled jobs in ${CRON}. Nothing is monitoring this server, and 3.7.5's
  migration should have put them back. Run it and read the output:
    hitechcloudcli native-install --scheduled-jobs-only"
fi

if [[ "$FAILURES" -eq 0 ]]; then
    ok "Version ${VERSION} applied."
else
    bad "Version ${VERSION} applied, with ${FAILURES} thing(s) above left undone."
    exit 1
fi
