#!/bin/bash

copyFiles()
{
    local silent_flag="$1"
    local source="$2"
    local save_dir="$3"
    local user_name="$4"   # advisory — the destination path determines the owner

    # Write as the destination's owner (see copyFile).
    local op="runInstallOp"
    [[ "$save_dir" == "$containers_dir"* || "$save_dir" == "${LP_CONTAINERS_DIR:-/libreportal-containers}"/* ]] && op="runFileOp"

    local files=($($op find "$source" -type f))
    if [ ${#files[@]} -eq 0 ]; then
        echo "No files found in the source directory: $source"
    fi

    for file in "${files[@]}"; do
        local file_name=$(basename "$file")
        if [ "$silent_flag" == "loud" ]; then
            local result=$($op cp -f "$file" "$save_dir")
            checkSuccess "Copying $file_name to $save_dir"
        else
            $op cp -f "$file" "$save_dir" >/dev/null 2>&1
        fi
    done
}
