# bash completion for mtmon (installed by the mtmon package)
_mtmon() {
    local cur prev
    COMPREPLY=()
    cur=${COMP_WORDS[COMP_CWORD]}
    prev=${COMP_WORDS[COMP_CWORD-1]}

    case $prev in
        -c|--config)
            # configuration file
            COMPREPLY=( $(compgen -f -- "$cur") )
            compopt -o filenames 2>/dev/null
            return 0 ;;
        --changelog)
            # versions from the installed changelog
            local f
            for f in /usr/share/mtmon/CHANGELOG.md /usr/share/doc/mtmon/CHANGELOG.md; do
                [ -r "$f" ] || continue
                COMPREPLY=( $(compgen -W "$(sed -n 's/^## \([0-9][^ ]*\).*/\1/p' "$f")" -- "$cur") )
                break
            done
            return 0 ;;
        --period-report)
            COMPREPLY=( $(compgen -W "week month 7d 30d" -- "$cur") )
            return 0 ;;
        --mute|--unmute)
            COMPREPLY=( $(compgen -W "all" -- "$cur") )
            return 0 ;;
        --forget|--rename-ssid|--new-api-key)
            # MAC / identity / SSID: nothing to suggest
            return 0 ;;
    esac

    local opts="--config --once --dry-run --debug --check --discover --clients --hash-password --new-api-key --test-central
                --rename-ssid --web --test-mail --show --report --forget --reset --changelog
                --version --help --mute --unmute --mutes --inventory --csv --certs --period-report --push-list --push-remove --server --config-upgrade --template --ap-ports --topology"
    if [[ $cur == -* || -z $cur ]]; then
        COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
    fi
    return 0
}
complete -F _mtmon mtmon
