# Bash completion script for k3d, because I couldn't find one. Do what you want with it.

# If you're using zsh, this script should work as long as you have these lines in .zshrc:
# 
# autoload bashcompinit
# bashcompinit
# source /path/to/your/bash_completion_file
# 

# functions to grab k3d clusters info [would be nice if k3d list had a simple mode for grabbing names]
__started-k3d-clusters ()
{
    k3d list | sed '1,3d;$d' | awk -F'|' '$4 == " running " {gsub(" ","");print $2}'
}

__stopped-k3d-clusters ()
{
    k3d list | sed '1,3d;$d' | awk -F'|' '$4 == " stopped " {gsub(" ","");print $2}'
}

__all-k3d-clusters ()
{
    k3d list | sed '1,3d;$d' | awk -F'|' '{gsub(" ","");print $2}'
}

# function to check if an opt is already in COMP_WORDS
__check_existing_compword ()
{
    for option in ${COMP_WORDS[*]}; do
        if [ "$1" == "$option" ]; then
            return 1
        fi
    done
    return 0
}

# function to lookup command opts and remove duplicates
__k3d_command_opts ()
{
    local opt opts_var complete command complete
    command=${COMP_WORDS[1]}
    # translate short commands into full commands
    case $command in
            c)       command="create";;
            d|del)   command="delete";;
            l|ls)    command="list";;
            i)       command="import-images";;
            ct)      command="check-tools";;
    esac
    opts_var="${command//-/_}_opts"
    eval opts_string='$'"$opts_var"
    # opts_string="${!opts_var}"    < does not work in zsh
    complete=()
    for opt in $opts_string; do
        __check_existing_compword "${opt}"
        if [ $? -eq 0 ]; then
            complete+=("$opt")
	fi
    done
    echo "${complete[*]}"
}

# Main k3d completion function
__k3d_complete ()
{
    local cur dashoptions prev param

    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"

    # default options
    dashoptions='-h --help -v --version --verbose --timestamp'
    k3d_commands='check-tools shell create add-node delete stop start list get-kubeconfig import-images version help'

    # command specific options
    shell_opts='--name --command --shell'
    create_opts='--name --volume --port --port-auto-offset --api-port --wait --image --server-arg --agent-arg --env --label --workers --auto-restart --enable-registry --registry-name --registry-port --registries-file'
    add_node_opts='--role --name --count --image --arg --env --volume --k3s --k3s-secret --k3s-token'
    delete_opts='--name --all --prune'
    stop_opts='--name --all'
    start_opts='--name --all'
    get_kubeconfig_opts='--name --all --overwrite'
    import_images_opts='--name --no-remove'

    # commands with no options
    version_opts=''
    help_opts=''
    list_opts=''

    
    case "$prev" in
	k3d)
              COMPREPLY=( $( compgen -W "$k3d_commands" -- $cur ) )
              return 0
              ;;
	check-tools | shell | create | add-node | delete | stop | start | list | get-kubeconfig | import-images)
	      COMPREPLY=( $(compgen -W "$(__k3d_command_opts)" -- $cur ) )
	      return 0
	      ;;
        --name | -n)
              case ${COMP_WORDS[1]} in
                  start)
                             # show stopped clusters
                             COMPREPLY=( $( compgen -W "$(__stopped-k3d-clusters)" -- $cur ) )
                             return 0
                             ;; 
                  stop) 
                             # show stopped clusters
                             COMPREPLY=( $( compgen -W "$(__started-k3d-clusters)" -- $cur ) )
                             return 0
                             ;; 
                  create|c) 
                             # sorry, can't help you here
                             return 0
                             ;;
                  *) 
                             # show stopped clusters
                             COMPREPLY=( $( compgen -W "$(__all-k3d-clusters)" -- $cur ) )
                             return 0
                             ;;
              esac
              ;;
        --wait | -t)
              COMPREPLY=( $( compgen -W "-1 15 30 60 120" -- $cur ) )
              return 0
              ;;
        --workers | -w)
              COMPREPLY=( $( compgen -W "2 4 8" -- $cur ) )
              return 0
              ;;
        --registry-name)
              COMPREPLY=( $( compgen -W "registry.local" -- $cur ) )
              return 0
              ;;
        --registry-port)
              COMPREPLY=( $( compgen -W "5000" -- $cur ) )
              return 0
              ;;
        --api-port | -a)
              COMPREPLY=( $( compgen -W "6443" -- $cur ) )
              return 0
              ;;
	--port | -p)
              return 0  # we do not guess port config
              ;;
        --volume | -v)
              case "$cur" in 
                  *:*) return 0
                       ;;
                  '')
                       COMPREPLY=($( compgen -W '/' -- "$cur" ))
                       type compopt &>/dev/null && compopt -o nospace
                       return 0
                       ;;
                  /*)
                       COMPREPLY=($( compgen -f -X '/' -- "$cur" ))
                       type compopt &>/dev/null && compopt -o nospace
                       return 0
                       ;;
              esac;
              return 0

              COMPREPLY=( $( compgen -d -X -- $cur ) )
              return 0
              ;;
    esac

    case "$cur" in
        *)    COMPREPLY=( $(compgen -W "$(__k3d_command_opts)" -- $cur ) )
              return 0
              ;;
    esac

    return 0
    
}

complete -F __k3d_complete k3d