Update element to the latest version

New Matrix synapse/element vulnerability has been found, it’s convenient to update the element automatically, here is the bash scrpit generated by gemini:

#!/bin/sh
#
# Fully Automated Element Web Update Script V2.4 (FINAL)
#
# V2.4 Fix: 将 'ln -sf' 替换为 'rm -f' + 'ln -s',以支持
# 那些原子覆盖操作会静默失败的非标准文件系统 (如 NFS/CIFS)。
#
# V2.3 Fix: 移除了所有 'sudo' 命令 (因为脚本由 root 运行)。
# V2.2 Fix: 移除了 'set -o pipefail' 以兼容 /bin/sh (dash)。
# V2.1 Fix: 修正了更新检查逻辑,使其检查 'current' 软链接的目标。
#

# 如果命令失败 (set -e) 或变量未设置 (set -u),立即退出
set -eu

### CONFIGURATION ###
# Element 安装的根目录
INSTALL_LOCATION="/var/www/element"

# 配置文件的名称
CONFIG_FILENAME="config.json"

# Web 服务器运行的用户 (例如 Debian/Ubuntu 上的 'www-data')
WEB_USER="www-data"
### END CONFIGURATION ###


# --- Do not edit below this line ---
CONFIG_PATH="${INSTALL_LOCATION}/${CONFIG_FILENAME}"

echo "--- Starting Element Task ---"

### 智能检测与自动迁移 (未改变) ###
if [ -f "$CONFIG_PATH" ] && [ ! -d "${INSTALL_LOCATION}/archive" ]; then
    echo "Legacy manual installation detected. Performing one-time auto-migration..."
    BACKUP_PATH="/root/${CONFIG_FILENAME}.backup-$(date +%F-%H%M%S)"
    echo "Creating secure config backup at: ${BACKUP_PATH}"
    cp "$CONFIG_PATH" "$BACKUP_PATH"
    mv "$CONFIG_PATH" "/tmp/${CONFIG_FILENAME}.temp"
    mkdir -p "${INSTALL_LOCATION}/archive"
    echo "Archiving old application files..."
    ARCHIVE_BACKUP_DIR="${INSTALL_LOCATION}/archive/old_install_backup_$(date +%F)"
    mkdir -p "$ARCHIVE_BACKUP_DIR"
    find "${INSTALL_LOCATION}" -mindepth 1 -maxdepth 1 -not -name "archive" -exec mv {} "$ARCHIVE_BACKUP_DIR/" \; || true
    mv "/tmp/${CONFIG_FILENAME}.temp" "$CONFIG_PATH"
    echo "Auto-migration complete! Starting update process..."
fi
### END AUTO-MIGRATION ###


### 标准更新流程 ###
echo "Fetching latest version tag..."
LATEST_TAG="$(curl -s https://api.github.com/repos/element-hq/element-web/releases/latest | jq -r .tag_name)"

if [ -z "$LATEST_TAG" ]; then
    echo "ERROR: Could not fetch the latest version tag."
    exit 1
fi
echo "Latest version available: ${LATEST_TAG}"

cd "$INSTALL_LOCATION"

# 2. 检查 'current' 软链接是否 *已经指向* 最新版本 (V2.1 修正)
echo "Checking current version..."
EXPECTED_TARGET_PATH="${INSTALL_LOCATION}/archive/element-${LATEST_TAG}"
CURRENT_TARGET_PATH=""

if [ -L "${INSTALL_LOCATION}/current" ]; then
    # readlink -f 会解析软链接的绝对路径
    CURRENT_TARGET_PATH=$(readlink -f "${INSTALL_LOCATION}/current")
fi

if [ "$CURRENT_TARGET_PATH" = "$EXPECTED_TARGET_PATH" ]; then
    echo "You are already running the latest version (${LATEST_TAG}). No update needed."
    echo "--- Element Update Task Finished ---"
    exit 0
fi

echo "New version found or symlink mismatch. Starting download/update..."

# 3. 下载并解压新版本 (如果它尚不存在)
if [ ! -d "archive/element-${LATEST_TAG}" ]; then
    echo "Downloading new version..."
    wget -q --show-progress "https://github.com/element-hq/element-web/releases/download/${LATEST_TAG}/element-${LATEST_TAG}.tar.gz" -P "archive"
    echo "Download complete. Extracting..."
    tar xf "archive/element-${LATEST_TAG}.tar.gz" -C "archive"
    rm "archive/element-${LATEST_TAG}.tar.gz"
else
    echo "Archive directory for ${LATEST_TAG} already exists. Skipping download."
fi

# 4. 切换 'current' 软链接 (V2.4 修正)
echo "Switching 'current' symlink (using rm + ln)..."
# 使用 'rm -f' (强制) 来避免在 'current' 不存在时报错
rm -f "${INSTALL_LOCATION}/current"
ln -s "${INSTALL_LOCATION}/archive/element-${LATEST_TAG}" "${INSTALL_LOCATION}/current"

# 5. 链接持久化的 config.json 到新目录中
if [ -f "$CONFIG_PATH" ]; then
    echo "Linking ${CONFIG_FILENAME}..."
    # 'ln -sf' 对于链接的目标文件是安全的,我们保留它
    ln -sf "$CONFIG_PATH" "${INSTALL_LOCATION}/current/${CONFIG_FILENAME}"
else
    echo "WARNING: ${CONFIG_PATH} not found. Please create or restore it manually."
fi

# 6. 设置正确的文件权限 (V2.3 修正,移除 sudo)
echo "Setting file permissions..."
chown -R "${WEB_USER}:${WEB_USER}" "${INSTALL_LOCATION}/archive/element-${LATEST_TAG}"
chown -h "${WEB_USER}:${WEB_USER}" "${INSTALL_LOCATION}/current"
chown -h "${WEB_USER}:${WEB_USER}" "${INSTALL_LOCATION}/current/${CONFIG_FILENAME}"

echo "Update successful! Element is now at version ${LATEST_TAG}"
echo "--- Element Update Task Finished ---"

The root folder of Element must be changed to /var/www/element/current