# shellcheck shell=dash disable=2034
xrc ui
___x_cmd_install_run(){
    local X_help_cmd='x help -m install run';   help:arg-null:parse

    local i; for i in "$@"; do
        ___x_cmd_install_run___inner "$i" || return $?
    done
}

___x_cmd_install_run___app_(){
    [ "$#" -gt 0 ]  ||      N=install M="Please provide sofotware" log:ret:64

    local software="$1";    shift "$#"
    local data; data="$(___x_cmd_install___showcandidate "$software")" || return $?

    ! x env --xinstall has "$software" || data=" \"x env use ${software}\" ${data}"

    [ -n "$data" ] || return $?
    local cmd="";
    eval x ui select id,cmd "\"Choose the code you want to execute:\"" "$data" || return $?
    [ -n "$cmd" ] || return $?
    x_="$cmd"
}

___x_cmd_install___showcandidate(){
    ___X_CMD_INSTALL_IS_RUN=1 ___x_cmd_install_cat "$1"
}

___x_cmd_install_run___inner(){
    local software="$1"
    local x_=; ___x_cmd_install_run___app_ "$software" || return $?
    [ "$x_" != "Exit" ] || return 0
    x ui yesno "Whether to execute the following code: ${___X_CMD_UNSEENCHAR_NEWLINE}$(___x_cmd_ui bold cyan "$x_")" || return $?
    install:info --code "$x_" "Running the code"
    eval "$x_" || {
        install:error "Execute failed"
        # x_=; ___x_cmd_install___update_installer_
        # install:warn "Perhaps you need to use \`$(___x_cmd_ui bold cyan "$x_")\` firstly"
        return 1
    }
    # install:info "Download software[$software] successfully"
}

___x_cmd_install_run___update_installer_(){
    local cmd="$1"
    x_=""
    case "$cmd" in
        apt)    if x hascmd apt; then
                    x_="x install run apt"
                else
                    x_="sudo apt update"
                fi
                ;;
        pacman) if x hascmd pacman; then
                    x_="x install run pacman"
                else
                    x_="sudo pacman -Sy"
                fi
                ;;
        brew)   if x hascmd brew; then
                    x_="x install run brew"
                fi
                ;;
        dnf)    x_="sudo dnf check-update" ;;
        yum)    x_="sudo yum check-update" ;;
    esac
}

___x_cmd_install___update_installer_(){
    local installer
    installer="$(x os release | head -n 1)"
    if [ "$installer" = "Ubuntu" ]; then
        x_="sudo apt update"
    elif [ "$installer" = "arch"  ]; then
        x_="sudo pacman -Sy"
    elif [ "$installer" = "fedora" ]; then

        if x hascmd dnf; then
            x_="sudo dnf check-update"
        else
            x_="sudo yum check-update"
        fi
    else
        x_="Update your installer"
    fi
}
