#!/bin/bash

# Flip Nextcloud's maintenance mode. With maintenance on, all users are locked
# out and Nextcloud only serves a notice — required before running occ upgrades.

appNextcloudToggleMaintenance() {
    local args="$1"
    local enable
    enable="$(authToolArg "$args" enable)"

    local target=--off
    [[ "$enable" == "true" ]] && target=--on

    local out
    out=$(runFileOp docker exec -u www-data nextcloud-service php occ maintenance:mode $target 2>&1)
    if echo "$out" | grep -qi "maintenance mode (enabled\|already enabled\|disabled\|already disabled)"; then
        isSuccessful "Nextcloud maintenance mode $target."
        return 0
    fi
    isError "Nextcloud maintenance toggle failed: $out"; return 1
}
