# shellcheck    shell=dash

# Section: op: unshift, pop
___x_cmd_path_shift(){
    PATH="${PATH#*:}"
}

___x_cmd_path_pop(){
    PATH="${PATH%:*}"
}
# EndSection

# Section: unshift
___x_cmd_path_add(){
    ___x_cmd_path_unshift "$*"
}

___x_cmd_path_unshift_(){
    ___x_cmd_path_obj_unshift_ "$PATH" "$1"
}

___x_cmd_path_unshift(){ # Can't remove this function !（pkg populate）
    local x_=""
    ___x_cmd_path_unshift_ "$*" || return $?
    PATH="$x_"
}
# EndSection

# Section: push
___x_cmd_path_push_(){
    ___x_cmd_path_obj_push_ "$PATH" "$1"
}

___x_cmd_path_push(){
    local x_=""
    ___x_cmd_path_push_ "$@" || return $?
    PATH="$x_"
}
# EndSection

# Section: down_or_push up_or_unshift
___x_cmd_path_down_or_push(){
    ___x_cmd_path_rm "$@"
    ___x_cmd_path_push "$@"
}

___x_cmd_path_up_or_unshift(){
    ___x_cmd_path_rm "$@"
    ___x_cmd_path_unshift "$@"
}

# EndSection

# Section: More frequently used function: up_or_unshift, add_existed_folder

___x_cmd_path_add_existed_folder_(){
    ___x_cmd_path_obj_add_existed_folder_ "$PATH" "$1" || return $?
}

___x_cmd_path_add_existed_folder(){
    local x_=""
    ___x_cmd_path_add_existed_folder_ "$@" || return $?
    PATH="$x_"
}

___x_cmd_path_add_folder(){
    local dir="${1:?Provide a directory}"
    ___x_cmd_path_up_or_unshift "$@"
}

# EndSection


## Section: uniq
if [ -z "$ZSH_VERSION" ]; then
___x_cmd_path_uniq(){
    local IFS=:
    local newpath=:
    local p; for p in $PATH; do
        [ "$newpath" != "${newpath#*":${p}:"}" ] || newpath="${newpath}${p}:"
    done
    PATH="${newpath#:}"
    PATH="${PATH%:}"
}

else
___x_cmd_path_uniq(){
    # local IFS=:
    local newpath=:
    local path_tmp="${PATH}:"
    local p; while :; do
        p="${path_tmp%%:*}"
        [ "$p" != "$path_tmp" ] || break
        path_tmp="${path_tmp#*:}"
        [ "$newpath" != "${newpath#*":${p}:"}" ] || newpath="${newpath}${p}:"
    done
    PATH="${newpath#:}"
    PATH="${PATH%:}"
}

fi
# EndSection
