# shellcheck shell=dash

# RC hook: source cached compiled code during shell startup
___x_cmd_slash_init(){
    case "$1" in
        -h|--help) ___x_cmd help -m slash init; return ;;
    esac

    local cache_dir="$___X_CMD_ROOT_CACHE/slash/compile"
    case "$___X_CMD_SHELL" in
        bash|ash)
            [ -f "$cache_dir/slash.sh" ] || return 0
            slash:info --source-file "$cache_dir/slash.sh" "Sourcing compiled sh script"
            ___x_cmd_source_invoke "$cache_dir/slash.sh"
            ;;
        zsh)
            [ -f "$cache_dir/slash.sh" ] || return 0
            slash:info --source-file "$cache_dir/slash.sh" "Sourcing compiled sh script"
            ___x_cmd_source_invoke "$cache_dir/slash.sh"

            # Zsh-specific: load slash command completion for `/<TAB>`
            [ -f "$___X_CMD_ROOT_MOD/slash/lib/complete-zsh" ] || return 0
            slash:info --source-file "$___X_CMD_ROOT_MOD/slash/lib/complete-zsh" "Sourcing zsh completion script"
            ___x_cmd_source_invoke "$___X_CMD_ROOT_MOD/slash/lib/complete-zsh"
            ;;
        dash)
            [ -f "$cache_dir/slash.dash" ] || return 0
            slash:info --source-file "$cache_dir/slash.dash" "Sourcing compiled dash script"
            ___x_cmd_source_invoke "$cache_dir/slash.dash"
            ;;
    esac
}
