# shellcheck shell=dash

boot:info "Running x boot init"

(
    ___x_cmd mkdirp "$___X_CMD_ROOT"
    # TODO: x boot set_newer_version
    ___x_cmd boot alterdefault "$___X_CMD_VERSION" "$___X_CMD_ROOT"

    ___x_cmd websrc autoset

    ___x_cmd_boot_initrootbin

    X_STR="[ ! -f \"\$HOME/.x-cmd.root/X\" ] || . \"\$HOME/.x-cmd.root/X\" # boot up x-cmd."

    ___x_cmd_boot_initrcfile        "$X_STR"    "$HOME/.shinit"

    if [ -n "$BASH_VERSION" ] || [ -f "$HOME/.bashrc" ] || [ -f "$HOME/.bash_profile" ] || ___x_cmd_hascmd bash; then
        ___x_cmd_boot_initrcfile    "$X_STR"    "$HOME/.bashrc"
        if [ -f "$HOME/.bash_profile" ]; then
            ___x_cmd_boot_initrcfile    "$X_STR"    "$HOME/.bash_profile"
        fi
    fi

    if [ -n "$ZSH_VERSION" ] || [ -f "$HOME/.zshrc" ] || ___x_cmd_hascmd zsh; then
        ___x_cmd_boot_initrcfile    "$X_STR"    "$HOME/.zshrc"
    fi

    # We don't support ksh now ...
    # if [ -n "$KSH_VERSION" ] || [ -f "$HOME/.kshrc" ] || ___x_cmd_hascmd ksh; then
    #     ___x_cmd_boot_initrcfile "$HOME/.kshrc"
    # fi

    ERROR_STR='Tip to setup x-cmd in'

    ! ___x_cmd_hascmd tcsh  ||      ! ___x_cmd tcsh --setup-is-required     || \
        boot:info   --cmd '                      x tcsh --setup'    \
                    --cmd '~/.x-cmd.root/bin/x-cmd tcsh --setup'    "$ERROR_STR tcsh"

    ! ___x_cmd_hascmd fish  ||      ! ___x_cmd fish --setup-is-required     || \
        boot:info   --cmd '                      x fish --setup'    \
                    --cmd '~/.x-cmd.root/bin/x-cmd fish --setup'    "$ERROR_STR fish"

    ! ___x_cmd_hascmd xonsh ||      ! ___x_cmd onsh --setup-is-required     || \
        boot:info   --cmd '                      x onsh --setup'    \
                    --cmd '~/.x-cmd.root/bin/x-cmd onsh --setup'    "$ERROR_STR xonsh"

    ! ___x_cmd_hascmd nu    ||      ! ___x_cmd nu   --setup-is-required     || \
        boot:info   --cmd '                      x nu   --setup'    \
                    --cmd '~/.x-cmd.root/bin/x-cmd nu   --setup'    "$ERROR_STR nushell"

    ! ___x_cmd_hascmd elvish ||     ! ___x_cmd elv  --setup-is-required     || \
        boot:info   --cmd '                      x elv  --setup'    \
                    --cmd '~/.x-cmd.root/bin/x-cmd elv  --setup'    "$ERROR_STR elvish"

    ! ___x_cmd_hascmd powershell.exe || ! ___x_cmd pwsh --setup-is-required || \
        boot:info   --cmd '                      x pwsh  --setup'    \
                    --cmd '~/.x-cmd.root/bin/x-cmd pwsh  --setup'   "$ERROR_STR powershell"

    # TODO: tclsh, etc.

    # In some system, like nixos, doesn't include awk by default. So we provide an awk using cosmo
    ___x_cmd hascmd awk || {
        boot:info "Awk is not available in the current environment. Using cosmo awk instead."
        boot:info "If you install awk mannually later and want to deactivate this cosmo version awk -> x cosmo unuse awk"
        # Notice: In the future, we might consider using busybox awk.
        ___x_cmd cosmo use awk || {
            boot:error "Failed to use cosmo version awk"
            return 1
        }
    }

    boot:info  --cmd 'x pkg bootupdate' \
            --tips 'If it fails, or is interrupted, you can run it manually later.' \
            'Updating the resource package indexes ...'

    ___x_cmd pkg bootupdate

    boot:info --cmd 'x shortcut init' 'Updating the shortcut config ...'
    ___x_cmd shortcut init

    # TODO: consider whether it is appropriate.
    if [ -n "$___X_CMD_BOOT_INIT_FOR_REPL" ]; then
        ___x_cmd advise gen_x_advise
        ___x_cmd shortcut compile all
    fi

) || return 1

boot:info "Initialization SUCCESS";
