#!/bin/bash

copyFile()
{
    local silent_flag="$1"
    local file="$2"
    local file_name=$(basename "$file")
    local save_dir="$3"
    local save_dir_file=$(basename "$save_dir")
    local user_name="$4"   # advisory — the destination path determines the owner
    local flags="$5"
    local flags_full=""
    [[ "$flags" == "overwrite" ]] && flags_full="-f"

    # Write as the destination's owner — no root, no chown. Under
    # /docker/containers/<app> that's the docker install user (runFileOp);
    # the manager-owned control plane (configs/logs/etc.) is runInstallOp.
    # Mirrors createTouch's path-based ownership; $user_name is now advisory.
    local op="runInstallOp"
    [[ "$save_dir" == "$containers_dir"* || "$save_dir" == "${LP_CONTAINERS_DIR:-/libreportal-containers}"/* ]] && op="runFileOp"

    if [ "$silent_flag" == "loud" ]; then
        local result=$($op cp $flags_full "$file" "$save_dir")
        checkSuccess "Copying $file_name to $save_dir_file"
    else
        $op cp $flags_full "$file" "$save_dir" >/dev/null 2>&1
    fi
}
