auto
  #   	ˆl–äY>”Êe¶¥2 ^¸Ó·˜´o¿„ÏˆÂÁ ?÷     	# SPDX-License-Identifier: GPL-2.0-only
#
# Copyright (C) 2015-2023 Red Hat, Inc.,  Tony Asleson <tasleson@redhat.com>
#
# Bash completion for lsmcli. This may be far from ideal,
# suggestions & improvements appreciated!

potential_args=''

# Skip value lookups by default
NO_VALUE_LOOKUP=${LSMCLI_AUTO_COMPLETE_VALUE:=0}

function join { local IFS="$1"; shift; echo "$*"; }

# Linear search of an array of strings for the specified string
function listcontains() {
    declare -a the_list=("${!1}")

    for word in "${the_list[@]}" ; do
        [[ ${word} == $2 ]] && return 0
    done
    return 1
}

# Given a list of what is possible and what is on the command line return
# what is left.
# $1 What is possible
# Results are returned in global string $potential_args
function possible_args()
{
    local l=()

    for i in $1
    do
        listcontains COMP_WORDS[@] "$i"
        if [[ $? -eq 1 ]] ; then
            l+=("$i")
        fi
    done

    potential_args=$( join ' ', "${l[@]}" )
}

# Returns the position of the value in the COMP_WORDS that contains $1, or
# 255 if it doesn't exist
function arg_index()
{
    count=0
	for i in "${COMP_WORDS[@]}"
	do
		if [[ "$i" == "$1" ]] ; then
			return ${count}
		fi
		let count+=1
	done
	return 255
}

function _lsm()
{
    local cur prev opts
    sep='#'
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"
    opts_short="-b -v -u -P -H -t -e -f -w -b"
    opts_long=" --help --version --uri --prompt --human --terse --enum \
              --force --wait --header --script "
    opts_cmds="list job-status capabilities plugin-info volume-create \
                volume-delete volume-resize volume-replicate \
                volume-replicate-range volume-replicate-range-block-size \
                volume-dependants volume-dependants-rm volume-access-group \
                volume-mask volume-unmask access-group-create \
                access-group-delete access-group-add access-group-remove \
                volume-enable volume-disable iscsi-chap fs-create fs-delete \
                fs-resize fs-export fs-unexport fs-clone fs-snap-create \
                fs-snap-delete fs-snap-restore fs-dependants fs-dependants-rm \
                file-clone ls lp lv ld la lf lt lb c p vc vd vr vm vi ve vi ac \
                aa ar ad vri volume-raid-info pool-member-info pmi \
                vrc volume-raid-create vrcc volume-raid-create-cap \
                volume-phy-disk-cache-update vpdcu \
                volume-read-cache-policy-update vrcpu \
                volume-write-cache-policy-update vwcpu \
                volume-cache-info vci volume-ident-led-on vilon \
                volume-ident-led-off viloff local-disk-list ldl \
                system-read-cache-pct-update srcpu \
                local-disk-ident-led-on ldilon \
                local-disk-ident-led-off ldiloff \
                local-disk-fault-led-on ldflon \
                local-disk-fault-led-off ldfloff"

    list_args="--type"
    list_type_args="volumes pools fs snapshots exports nfs_client_auth \
                    access_groups systems disks plugins target_ports \
                    batteries"

    opts_filter="--sys --pool --vol --disk --ag --fs --nfs"

    cap_args="--sys"
    volume_create_args="--name --size --pool"
    volume_delete_args="--vol --force"  # Should force be here, to easy to tab through?"
    volume_resize_args="--vol --size --force" # Should force be here, to easy to tab through?"

    volume_replicate_args="--vol --name --rep-type"
    # Hmmm, this looks like a bug with CLI, should support lower and upper case?
    volume_rep_types="CLONE COPY MIRROR_ASYNC MIRROR_SYNC"

    volume_replicate_range_args="--src-vol --dst-vol --rep-type --src-start \
                                --dst-start --count --force" # Force ?

    volume_replication_range_bs="--sys"
    volume_dependants="--vol"

    volume_access_group_args="--vol"
    volume_masking_args="--vol --ag"

    access_group_create_args="--n