#!/bin/bash

# Re-index Nextcloud's file metadata — useful after files are added or removed
# on disk outside of Nextcloud (rsync, restore, manual copy).

appNextcloudRescanFiles() {
    local args="$1"
    local username
    username="$(authToolArg "$args" username)"

    local target=--all
    [[ -n "$username" ]] && target="$username"

    local out
    out=$(runFileOp docker exec -u www-data nextcloud-service php occ files:scan $target 2>&1)
    if echo "$out" | grep -qi "scanned\|files found"; then
        isSuccessful "Nextcloud file rescan completed${username:+ for $username}."
        return 0
    fi
    isError "Nextcloud file rescan failed: $out"; return 1
}
