#!/bin/bash
################################################################################
# Script Name: version/2.1.21/UPDATE.sh
# Description: Publish the branding the webmail can read, and repair ClamAV.
# Company: hitechcloud.org
# Copyright (c) hitechcloud.org
################################################################################
#
# Two things this release fixes need one run on an existing server.
#
# 1. The webmail's branding. It read the panel's own configuration file
#    directly, and that file holds database credentials, so its directory is
#    0750 root:root while the webmail runs as its own unprivileged user. Every
#    read failed and every failure fell back to the product's default name -
#    so an operator who set a brand months ago still greeted their customers as
#    MailFlow. The panel now publishes a name, a logo URL and the same two per
#    reseller to a file the webmail may read.
#
# 2. ClamAV, on servers where it was installed by an earlier release. RHEL
#    ships /etc/clamd.d/scan.conf with every line commented out, so clamd
#    starts, prints "Please define server type (local and/or TCP)", exits, and
#    restart-loops until systemd gives up - while the panel reported it
#    enabled. It gets a socket, a runtime directory, and its failed-restart
#    counter cleared.
#
# Both are safe to re-run and do nothing on a server that does not have the
# service in question.
################################################################################

set -uo pipefail

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

[[ $EUID -eq 0 ]] || { echo "[2.1.21] 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; }

# 1. The branding the webmail is allowed to read.
if [[ -d /etc/hitechcloud/mail ]]; then
    if bash /usr/local/hitechcloudcli/native/mail.sh branding >/dev/null 2>&1; then
        ok "The webmail can now read the operator's brand"
    else
        warn "Could not publish the branding file - run: hitechcloudcli native-mail branding"
    fi
fi

# 2. ClamAV, only where it is actually installed.
if [[ -f /etc/clamd.d/scan.conf ]] || systemctl list-unit-files 'clamav-daemon.service' >/dev/null 2>&1; then
    if bash /usr/local/hitechcloudcli/native/install.sh --with clamav >/dev/null 2>&1; then
        # Asked of the daemon, not of the installer: "installed" and "running"
        # are the two things this release exists to stop conflating.
        for unit in clamd@scan clamav-daemon clamd; do
            if systemctl is-active --quiet "$unit" 2>/dev/null; then
                ok "ClamAV is running (${unit})"
                break
            fi
        done
    else
        warn "ClamAV could not be repaired - check: journalctl -u clamd@scan -n 30"
    fi
fi

ok "2.1.21 update complete"
exit 0
