
# setup spacemacs
# setup doomemacs

___x_cmd_emacs_setup(){
    while [ $# -gt 0 ]; do
        case "$1" in
            --spacemacs|--space)        default=spacemacs;  shift ;;
            --doomemacs|--doom)         default=doomemacs;  shift ;;
            *)                          break ;;
        esac
    done

    ___x_cmd_emacs_setup_"$default"
}

# https://www.gnu.org/software/emacs/manual/html_node/emacs/Find-Init.html
___x_cmd_emacs_setup_spacemacs(){
    local emacs_folder="$___X_CMD_ROOT_DATA/spacemacs/.emacs.d"

    if [ ! -e "$emacs_folder" ]; then
        ___x_cmd git clone -b develop https://github.com/syl20bnr/spacemacs "$emacs_folder"
    else
        emacs:info "Spacemacs already installed. Now trying to update."
        (
            ___x_cmd_cd0 "$emacs_folder"
            ___x_cmd git pull origin develop
        )
    fi

    local linedata="(load-file \"$emacs_folder/init.el\")"
    if [ -d "$HOME/.emacs.d" ]; then
        emacs:info "The target fodler ~/.emacs.d is found."
        emacs:info --line "$linedata" \
            "To avoid potential conflicts, we won't automatically modify your configuration file. Please add the following line to your Emacs init file."
    else
        ___x_cmd mkdirp "$HOME/.emacs.d"
        printf "%s\n" "$linedata" >"$HOME/.emacs.d/init.el"
    fi

    # local x_
    # ___x_cmd first_ "$HOME/.emacs.el" "$HOME/.emacs"

    # local linedata="(load-file \"$emacs_folder\")"

    # if [ -f "$x_" ]; then
    #     if ___x_cmd_cmds grep -q "x-cmd" "$x_" 2>/dev/null; then
    #         emacs:info "X-CMD loaded in config file -> $x_"
    #         emacs:info --line "$linedata" \
    #             "To avoid potential conflicts, we won't automatically modify your configuration file. Please add the following to your Emacs init file."
    #         return 0
    #     fi
    #     printf "%s\n" "$linedata" >>"$x_"
    # else
    #     emacs:info "Creating emacs init file -> $HOME/.emacs.el"
    #     printf "%s\n" "$linedata" >"$HOME/.emacs.el"
    # fi
}

# after second thought, I will create a special module for doom.
___x_cmd_emacs_setup_doomemacs(){
    local emacs_folder="$HOME/.config/emacs"

    if [ ! -e "$emacs_folder" ]; then
        ___x_cmd git clone https://github.com/doomemacs/doomemacs "$emacs_folder"
    else
        emacs:info "Doomemacs already installed. Now trying to update."
        (
            ___x_cmd_cd0 "$emacs_folder"
            ___x_cmd git pull origin master
        )
    fi

    "$HOME/.config/emacs/bin/doom" install
}

