# shellcheck shell=dash

___x_cmd_cosmo___install(){
    local cosmobin="$1"
    local bin_dir_path="$2"

    [ -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

    [ -n "$bin_dir_path" ] || {
        if ___x_cmd_is_stdout2tty; then
            xrc path
            cosmo:info "Select the target dir path where you want to install cosmo binary -> $cosmobin"
            bin_dir_path="$(___x_cmd path lsapp)" || true
            [ -d "$bin_dir_path" ] || bin_dir_path="${bin_dir_path%/*}"
        fi
    }

    [ -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" || 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
}

