#
#  libkysdk-system's Library
#
#  Copyright (C) 2025, KylinSoft Co., Ltd.
# 
#  This library is free software; you can redistribute it and/or
#  modify it under the terms of the GNU Lesser General Public
#  License as published by the Free Software Foundation; either
#  version 3 of the License, or (at your option) any later version.
# 
#  This library is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#  Lesser General Public License for more details.
# 
#  You should have received a copy of the GNU General Public License
#  along with this library.  If not, see <https://www.gnu.org/licenses/>.
# 
#  Authors: ZhiMin Shao <shaozhimin@kylinos.cn>
# 
# 

__contains_word_kylin_sysinfo () {
    local w word=$1; shift
    for w in "$@"; do
        [[ $w = "$word" ]] && return
    done
}

_kylin-sysinfo() {
    local i verb comps custom
    local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}

    local -A VERBS=(
        [VERSION]='version'
        [HELP]='--help'
    )

    for ((i=0; i < COMP_CWORD; i++)); do
        if __contains_word_kylin_sysinfo "${COMP_WORDS[i]}" ${VERBS[*]}; then
            verb=${COMP_WORDS[i]}
            break
        fi
    done

    if [[ -z $verb ]]; then
        comps=${VERBS[*]}
    elif __contains_word_kylin_sysinfo "$verb" ${VERBS[VERSION]}; then
        comps='--production --minor --alias --serial --all --customization --major'
    elif __contains_word_kylin_sysinfo "$verb" ${VERBS[HELP]}; then
        comps=''
    fi

    COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
    return 0
}

complete -F _kylin-sysinfo kylin-sysinfo
