#!/bin/bash
################################################################################
# Script Name: version/3.0.6/UPDATE.sh
# Description: Apply version 3.0.6 migrations: install the meeting service
#              where the webmail runs, with the fixed unit.
# Company: hitechcloud.org
# Copyright (c) hitechcloud.org
################################################################################
set -uo pipefail
GREEN='\033[0;32m'; YELLOW='\033[1;33m'; NC='\033[0m'
ok()   { echo -e "[3.0.6] ${GREEN}✓${NC} $*"; }
warn() { echo -e "[3.0.6] ${YELLOW}!${NC} $*" >&2; }
[[ $EUID -eq 0 ]] || { echo "[3.0.6] This update must run as root." >&2; exit 1; }
[ -f /etc/hitechcloud/native/native.conf ] || { ok "3.0.6 update steps completed (container architecture)"; exit 0; }

# The meeting service behind the webmail's Meetings page. 3.0.4 shipped it
# and nothing installed it on an updated server; its unit also lacked
# AF_NETLINK, so where it was installed by hand it could not bind its port.
if systemctl list-unit-files 2>/dev/null | grep -q '^hitechcloud-webmail\.service' && [ -x /usr/local/hitechcloudcli/native/meet.sh ]; then
    HITECHCLOUD_REPO_DIR=/opt/hitechcloud hitechcloudcli native-meet install >/dev/null 2>&1 \
        && ok "Meeting service installed (hitechcloud-meet, UDP 8894)" \
        || warn "The meeting service did not install; run: HITECHCLOUD_REPO_DIR=/opt/hitechcloud hitechcloudcli native-meet install"
fi

ok "3.0.6 update steps completed successfully"
exit 0
