# shellcheck    shell=dash

# x pkg sphere runpkgbin [--sphere X] exa version bin/exa "$@"
# x pkg sphere runpkgbin --sphere X python Miniconda3-py310_23.9.0-0 shim_bin/python
___x_cmd_pkg_sphere_runpkgbin(){

    pkg:sphere:parse:option
    sphere_name="${sphere_name:-"X"}"

    local name="$1"     ;   [ -n "$name" ]      || N=pkg M="Provide a package name"     log:ret:64
    local version="$2"  ;   [ -n "$version" ]   || N=pkg M="Not found version"          log:ret:64
    local bin_file="$3" ;   [ -n "$bin_file" ]  || N=pkg M="Not found bin_file value"   log:ret:64
    shift 3

    local bin_dir="${bin_file%/*}"
    local x_=; ___x_cmd_pkg_sphere_getbinpath_ \
        --sphere "$sphere_name" --sphereroot "$___X_CMD_PKG_ROOT_SPHERE"    \
        --osarch "$osarch" "$name" "$version" "$bin_dir" || return $?

    bin_file="${x_}/${bin_file##*/}"

    # run the command
    pkg:debug "binpath => $bin_file"
    "$bin_file" "$@"
}

# ___x_cmd_pkg_sphere_getbinpath_ exa v0.0.1 shim_dir
___x_cmd_pkg_sphere_getbinpath_(){

    pkg:sphere:parse:option
    sphere_name="${sphere_name:-"X"}"

    local name="$1"     ;   [ -n "$name" ]      || N=pkg M="Provide a package name"     log:ret:64
    local version="$2"  ;   [ -n "$version" ]   || N=pkg M="Not found version"          log:ret:64
    local bin_dir="$3" ;    [ -n "$bin_dir" ]   || N=pkg M="Not found bin_dir value"    log:ret:64
    shift 3

    ___x_cmd_pkg_sphere_add --prebuild          \
        --protection use --reason "use,runpkgbin"   \
        --sphere "$sphere_name" --sphereroot "$___X_CMD_PKG_ROOT_SPHERE"    \
        --osarch "$osarch" "$name" "$version"   || N=pkg M="Install $name $version failed"  log:ret:1

    # run the command
    local x_treename=; ___x_cmd_pkg_treename_ "$name" "$version" "$osarch" || return $?
    x_="$___X_CMD_PKG_ROOT_SPHERE/$sphere_name/$x_treename/$name/$version/$bin_dir"
}
