#!/bin/bash
################################################################################
# Script Name: version/2.1.22/UPDATE.sh
# Description: Route CalDAV through the front end, and rebuild the webmail.
# Company: hitechcloud.org
# Copyright (c) hitechcloud.org
################################################################################
#
# This release adds a calendar to the webmail, which needs two things on an
# existing server:
#
#   1. The front end has to route /caldav/ and /.well-known/caldav to the
#      webmail. Without them a phone's PROPFIND falls into the single-page-app
#      rule and is answered with index.html, which reads to the client as
#      "this is not a CalDAV server".
#
#   2. The webmail itself has to be rebuilt, because the calendar is new code
#      on both sides. The database tables are created by the migration the
#      webmail runs at startup; nothing here touches the database.
#
# A server with no webmail installed does nothing.
################################################################################

set -uo pipefail

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

[[ $EUID -eq 0 ]] || { echo "[2.1.22] 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; then
    say "no webmail on this server; nothing to do"
    exit 0
fi

# 1. The vhost, re-rendered from the template this release ships.
if bash /usr/local/hitechcloudcli/native/webmail.sh configure >/dev/null 2>&1; then
    if grep -q 'location /caldav/' /etc/hitechcloud/native/frontend/conf.d/00-webmail.conf 2>/dev/null; then
        ok "Calendars and contacts are reachable from a phone"
    else
        warn "The webmail vhost was rewritten but has no /caldav/ route - re-run: hitechcloudcli native-webmail configure"
    fi
else
    warn "Could not rewrite the webmail vhost - run: hitechcloudcli native-webmail configure"
fi

# 2. The rebuild, out of this update's way: npm ci and a frontend build take
#    longer than an update is allowed to spend, and one killed half way leaves
#    a webmail that does not start.
if command -v systemd-run >/dev/null 2>&1; then
    systemd-run --collect --unit=hitechcloud-webmail-rebuild \
        --description="HiTechCloud webmail rebuild (2.1.22)" \
        --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.22: the calendar is now available, and syncs to phones over CalDAV." >/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.22 update complete"
exit 0
