# shellcheck shell=dash
# Section: vcm-git

###
  # @description: set up folder info variables
###
## Set up help func
___x_cmd_theme_git_get_friendly_ref() {
    ___x_cmd_theme_git_get_branch || ___x_cmd_theme_git_get_tag || ___x_cmd_theme_git_get_short_sha
}

___x_cmd_theme_git_get_branch() {
    command git symbolic-ref -q --short HEAD 2> /dev/null || return 1
}

___x_cmd_theme_git_get_tag() {
    command git describe --tags --exact-match 2> /dev/null || return 1
}

___x_cmd_theme_git_get_short_sha() {
    command git rev-parse --short HEAD 2> /dev/null
}

___x_cmd_theme_hg_get_friendly_ref() {
    command hg branch 2>/dev/null
}

___x_cmd_theme_svn_get_friendly_ref() {
    command svn info 2> /dev/null | awk '{
        if ( $1 == "URL:" ) {
            match($0,/(branches|tags)\/[^\/]+|trunk/)
            printf("%s", substr($0,RSTART, RLENGTH))
        }
    }'
}
# EndSection

# Section: vcm-main
___x_cmd_theme_vcm_git_bin(){
    printf "%s" "${___X_CMD_THEME_VCM_GIT_BIN:=$(command -v git)}"
}

___x_cmd_theme_vcm_hg_bin(){
    printf "%s" "${___X_CMD_THEME_VCM_HG_BIN:=$(command -v hg)}"
}

___x_cmd_theme_vcm_svn_bin(){
    printf "%s" "${___X_CMD_THEME_VCM_SVN_BIN:=$(command -v svn)}"
}

## Set up common folder info variables
___x_cmd_theme_git_get_prompt_info_exe() {
    if [ -n "$___X_CMD_THEME_NO_CHECK_INFO" ]; then
        printf "NONE"
        return
    fi

    if [ -n "$(___x_cmd_theme_vcm_git_bin)" ]; then
        if [ -f .git/HEAD ] || [ -n "$(command git rev-parse --is-inside-work-tree 2>/dev/null)" ]; then
            printf "git"
            return
        fi
    fi

    if [ -n "$(___x_cmd_theme_vcm_svn_bin)" ]; then
        if [ -d .svn ] || [ -n "$(command svn info --show-item wc-root 2>/dev/null)" ]; then
            printf "svn"
            return
        fi
    fi

    if [ -n "$(___x_cmd_theme_vcm_hg_bin)" ]; then
        if [ -d .hg ] || [ -n "$(command hg root 2>/dev/null)" ]; then
            printf "hg"
            return
        fi
    fi

    printf "NONE"
}

___x_cmd_theme_vcm() {
    ___X_CMD_THEME_VCM_TYPE="$(___x_cmd_theme_git_get_prompt_info_exe 2>/dev/null)"
}
___x_cmd_theme_scm() {
    ___X_CMD_THEME_SCM_TYPE="$(___x_cmd_theme_git_get_prompt_info_exe 2>/dev/null)"
}

___x_cmd_theme_get_git_repo_path(){
    command git rev-parse --show-toplevel 2>/dev/null
}

___x_cmd_theme_get_hg_repo_path(){
    command hg root 2>/dev/null
}

___x_cmd_theme_get_git_repo_name() {
    local __repo_path
    if __repo_path="$(___x_cmd_theme_get_git_repo_path)"; then
        printf "%s" "${__repo_path##*/}"
    else
        return 1
    fi
}

___x_cmd_theme_get_hg_repo_name() {
    local __repo_path
    if __repo_path="$(___x_cmd_theme_get_hg_repo_path)"; then
        printf "%s" "${__repo_path##*/}"
    else
        return 1
    fi
}

___x_cmd_theme_is_git_dirty() {
    [ -n "$(command git status --porcelain --ignore-submodules=dirty 2> /dev/null | tail -1)" ] && printf 'true'
}

___x_cmd_theme_is_hg_dirty() {
    [ -n "$(command hg status 2>/dev/null)" ] && printf 'true'
}

___x_cmd_theme_is_short_path_usable(){
    [ -z "$___X_CMD_THEME_FULL_PATH_CONIFG" ] || return 1
    [ "$PWD" != "${PWD##*"${1:?"Pealse provide repo path"}"}" ]
}

# EndSection
