# shellcheck shell=dash

___x_cmd_cosmo___infuse(){
    arg:init
    local osarch=""
    while [ $# -gt 0 ]; do
        case "$1" in
            --osarch)       osarch="$2";    arg:2:shift ;;
            *)              break
        esac
    done

    local bin_dir_path="$1";    shift

    ex:init
    while [ $# -gt 0 ]; do
        ___x_cmd_cosmo___infuse_one "$bin_dir_path" "$1" "$osarch";       ex:save;    M="Abort infuse binary -> $1" ex:ret:if-err;
        shift
    done
}

___x_cmd_cosmo___infuse_one(){
    local bin_dir_path="$1"
    local cosmobin="$2"
    local osarch="$3"

    [ -n "$cosmobin" ]                      || N=cosmo M="cosmo bin is required"        log:ret:1
    ___x_cmd_cosmo___hasbin "$cosmobin"     || N=cosmo M="Not found bin => $cosmobin "  log:ret:1

    [ -d "$bin_dir_path" ]                  || N=cosmo M="bin directory does not exist -> $bin_dir_path" log:ret:1

    local tgt="$___X_CMD_COSMO_ASSIMILATE_PATH/$cosmobin"

    cosmo:info "Assimilate the cosmobin -> $cosmobin to $tgt"
    [ -f "$tgt" ] ||  ___x_cmd_cosmo___assimilate "$cosmobin" "$tgt" "$osarch" || N=cosmo M="Failed to assimilate cosmobin -> $cosmobin" log:ret:1

    cosmo:info "Moving $tgt to $bin_dir_path"
    if [ ! -w "$bin_dir_path" ]; then
        cosmo:warn "Require sudo to writing directory -> $bin_dir_path"
        ___x_cmd sudo mv "$tgt" "$bin_dir_path"
    else
        ___x_cmd_cmds mv "$tgt" "$bin_dir_path"
    fi
}

