
___x_cmd_install_best(){
    [ $# -gt 0 ]    ||  set -- --help

    local priority=""
    local mode=exec
    while [ $# -gt 0 ]; do
        case "$1" in
            -h|--help)      ___x_cmd help -m install --best "$@" ; return 0 ;;
            -p|--priority)  priority="$2" ;     arg:2:shift ;;
            # --nosudo)       nosudo=1 ;          shift ;;
            # --nosys)        nosys=1 ;           shift ;;

            # --printname)    mode=printname ;    shift ;;        # use to debug or test.
            --printcmd|--dry-run)
                            mode=printcmd ;     shift ;;        # print best command without executing
            # --printinfo)    mode=printinfo ;    shift ;;        # for ai agent, just in case.

            *)              break ;;
        esac
    done

    local cmd=""
    while [ $# -gt 0 ]; do
        cmd="$( ___x_cmd_install_best___printcmd --priority "$priority" "$1" )"
        if [ -n "$cmd" ] ; then
            case "$mode" in
                printcmd)
                    printf '%s\n' "$cmd" ;;
                *)
                    eval "$cmd" || {
                        local exitcode=$?
                        install:error --cmd "$cmd" --exitocd "$exitcode" "Fail to execute command."
                        return 1
                    } ;;
            esac
        else
            N=install M="Fail to install." log:ret:64
        fi
        shift
    done
}

___x_cmd_install_best___printcmd(){

    local priority=""
    while [ $# -gt 0 ]; do
        case "$1" in
            -p|--priority)  priority="$2" ; arg:2:shift ;;
            *)              break ;;
        esac
    done

    local software="$1";    [ -n "$software" ] || return 1

    local alltools=; local best_tool=
    alltools="$( ___x_cmd_install_printtool "$software" )"
    [ -n "$alltools" ] || return 1

    best_tool="$(
        printf '%s\n' "$alltools" | ___x_cmd_cmds_awk \
        -v PRIORITY="$priority" \
        -f "$___X_CMD_ROOT_MOD/install/lib/awk/bestpick.awk"
    )"
    [ -n "$best_tool" ] || return 1

    ___x_cmd_install_printcmd0 "$best_tool" "$software"
}

