# shellcheck shell=dash disable=2016

___X_CMD_NU_RCPATH=""

___x_cmd_nu_rcpath_(){
    [ -z "$___X_CMD_NU_RCPATH" ] || {
        x_="$___X_CMD_NU_RCPATH"
        return 0
    }

    ___x_cmd os name_
    case "$x_" in
        darwin) ___X_CMD_NU_RCPATH="$HOME/Library/Application Support/nushell/env.nu" ;;
        win)    ___X_CMD_NU_RCPATH="$HOME/AppData/Roaming/nushell/env.nu" ;;
        *)      ___X_CMD_NU_RCPATH="$HOME/.config/nushell/env.nu" ;;
    esac

    x_="$___X_CMD_NU_RCPATH"
}

___x_cmd_nu___setup(){
    local xnunrc_dir="$HOME/.x-cmd.root/local/data/nu"
    ___x_cmd mkdirp "$xnunrc_dir"
    ___x_cmd cp -f "$___X_CMD_ROOT_MOD/nu/lib/rc/rc.nu"     "$xnunrc_dir/rc.nu"
    ___x_cmd cp -f "$___X_CMD_ROOT_MOD/nu/lib/rc/core.nu"   "$xnunrc_dir/core.nu"

    # local home_rcfile="$HOME/dotfiles/nushell/nushell"
    local x_
    ___x_cmd_nu___boot_init_rcfile_ || return
    ___x_cmd ensurefp "$x_"

    if [ ! -f "$x_" ]; then
        ___x_cmd_nu___boot_init_defaultinit
        [ -f "$x_" ] || printf "" > "$___X_CMD_NU_RCPATH"
    fi

    ___x_cmd boot initrcfile1  "$___X_CMD_NU_SETUP_RCCODE"     "$x_" || return 1
    local shortcutfp="$HOME/.x-cmd.root/local/cache/shortcut/compile/shortcut.nu"
    ___x_cmd shortcut compile nu
    [ -f "$shortcutfp" ] || {
        ___x_cmd ensurefp "$shortcutfp"
        printf "" > "$shortcutfp"
    }

    ___x_cmd ensurefp "$HOME/.x-cmd.root/local/cache/advise/bootcode/v0.0.0.nu"
    ___x_cmd advise complete nu code > "$HOME/.x-cmd.root/local/cache/advise/bootcode/v0.0.0.nu"
    local addonfp="$HOME/.x-cmd.root/local/cache/advise/addon/complete.nu"
    [ -f "$addonfp" ] || {
        ___x_cmd ensurefp "$addonfp"
        printf "" > "$addonfp"
    }


    local themefp="$HOME/.x-cmd.root/local/cfg/theme/use/nu/default.nu"
    [ -f "$themefp" ] || {
        ___x_cmd ensurefp "$themefp"
        printf "" > "$themefp"
    }
}

# ___x_cmd_nu___boot_init_rcfile_home_(){
#     ___x_cmd os name_
#     case "$x_" in
#         win)    local homesubpath="${HOME#/?}"
#                 local homedrive="${HOME%"$homesubpath"}"; homedrive="${homedrive#/}"
#                 x_="${homedrive}:$homesubpath"
#                 ;;
#         *)      x_="$HOME"
#     esac
# }

___x_cmd_nu___boot_init_rcfile_(){
    local rootpath="$___X_CMD_ROOT"

    if ___x_cmd os is win; then
        case "$rootpath" in
            /*/*)
                local p="${rootpath#/?/}"
                rootpath="${rootpath%"/$p"}"
                rootpath="${rootpath#/}:/$p"
        esac
    fi

    x_=; ___x_cmd_shq1_ "$rootpath";    rootpath="$x_"

    # local X_STR='if ( $"($env.HOME)/.x-cmd.root/local/data/nu/rc.nu" | path exists ) { source "'"$nufp"'"; } # boot up x-cmd.'
    # local X_STR='if ( $"($env.HOME)/.x-cmd.root/local/data/nu/rc.nu" | path exists ) { use "'"$nufp"'" *; } # boot up x-cmd.'
    # ___X_CMD_NU_SETUP_RCCODE="use ${nufp} *; source ${advfp}; # boot up x-cmd."
    ___X_CMD_NU_SETUP_RCCODE="const ___X_CMD_ROOT = $rootpath; source $\"(\$___X_CMD_ROOT)/local/data/nu/rc.nu\"; # boot up x-cmd"

    ___x_cmd_nu_rcpath_
    x_="$___X_CMD_NU_RCPATH"
}

___x_cmd_nu___setup_is_required(){
    local x_;   ___x_cmd_nu___boot_init_rcfile_ || return;   local rcfile="$x_"
    ! {
        ___x_cmd boot ensurerccodewithoutextra  \
                                "$___X_CMD_NU_SETUP_RCCODE"                 "$rcfile"                                   && \
            ___x_cmd_cmds cmp   "$___X_CMD_ROOT_MOD/nu/lib/rc/rc.nu"        "$HOME/.x-cmd.root/local/data/nu/rc.nu"     && \
            ___x_cmd_cmds cmp   "$___X_CMD_ROOT_MOD/nu/lib/rc/core.nu"      "$HOME/.x-cmd.root/local/data/nu/core.nu"
    } 1>/dev/null 2>/dev/null
}

___x_cmd_nu___boot_init_defaultinit(){
    printf "Y\n" | ___x_cmd_nu___binexec nu 1>/dev/null 2>/dev/null
}
