# shellcheck    shell=dash

___x_cmd_htag___ls(){
    x_=
    ! [ -f "$___X_CMD_HTAG_METADATA/other" ] || \
    x rat_ "$___X_CMD_HTAG_METADATA/other"

    printf "%s" "${x_#?}"
}

___x_cmd_htag___handle(){
    while [ $# -gt 0 ]; do
        case "$1" in
            -*)     ___x_cmd_htag___rm      "${1#-}" ;;
            +*)     ___x_cmd_htag___add     "${1#\+}" ;;
            *)      ___x_cmd_htag___add     "$1" ;;
        esac
        shift
    done
}

___x_cmd_htag___add(){
    x touch "$___X_CMD_HTAG_METADATA/other" || return

    local x_=""
    x rat_ "$___X_CMD_HTAG_METADATA/other"

    local IFS="
"
    {
        sort | uniq >"$___X_CMD_HTAG_METADATA/other"
    } <<A
$*
$x_
A
    local IFS=,
    htag:info "add tag => $*"
}

___x_cmd_htag___rm(){
    x mkdirp "$___X_CMD_HTAG_METADATA"
    local data
    local IFS="
"
    data=$(
        <"$___X_CMD_HTAG_METADATA/other" data="$*" command awk -v tag="$1"  '
            BEGIN{
                al = split(ENVIRON["data"], a, "\n")
                for (i = 1; i <= al; i++) b[a[i]] = 1
            }
            b[$0] == 1 { next }
            { print }
        '
    )

    printf "%s\n" "${data}" >"$___X_CMD_HTAG_METADATA/other"
    local IFS=","
    htag:info "rm tag => $*"
}

___x_cmd_htag___clear(){
    ___x_cmd_cmds_rm -rf "$___X_CMD_HTAG_METADATA"
    htag:info "clear all tags"
}

___x_cmd_htag___load_(){
    x_=
    ! [ -f "$___X_CMD_HTAG_METADATA/other" ] || \
    x rat_ "$___X_CMD_HTAG_METADATA/other"

}

___x_cmd_htag___has(){
    local x_htag_server=
    local x_htag_dn=
    local x_htag_ip=
    local x_htag_other=

    ___x_cmd_htag___info_
    local nl="
"

    local o="${nl}${x_htag_other}"
    while [ $# -gt 0 ]; do
        case "$1" in
            ip)     [ -n "$x_htag_ip" ]         || return ;;
            dn)     [ -n "$x_htag_dn" ]         || return ;;
            server) [ -n "$x_htag_server" ]     || return ;;
            *)
                    [ -n "${x_htag_other}" ]    || return
                    [ "${o}" != "${o%"${nl}${1}${nl}"*}" ] || return
                    ;;
        esac
        shift
    done
}
