# bash completion for dci-queue
#
# usage: dci-queue [-h] [-l {DEBUG,INFO,WARNING,ERROR,CRITICAL}] [-t TOP_DIR]
#                  [-c] [-p]
#                  {add-crontab,add-pool,add-resource,clean,dci-job,install,list,log,
#                   remove-crontab,remove-pool,remove-resource,run,schedule,search,
#                   searchdir,uninstall,unschedule}

_dci_queue() {
    local cur prev opts opt verb pool_name dci_queue_dir i skip_next positional
    opt=-W
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"
    dci_queue_dir="${DCI_QUEUE_DIR:-$HOME/.dci-queue}"

    # Handle options that take a value argument
    case "$prev" in
        -l|--log-level)
            COMPREPLY=( $(compgen -W "DEBUG INFO WARNING ERROR CRITICAL" -- "$cur") )
            return 0
            ;;
        -t|--top-dir)
            COMPREPLY=( $(compgen -d -- "$cur") )
            return 0
            ;;
        -n|--lines)
            return 0
            ;;
    esac

    # Find the verb and count positional args after it
    verb=""
    pool_name=""
    positional=0
    skip_next=0
    for (( i=1; i < COMP_CWORD; i++ )); do
        if (( skip_next )); then
            skip_next=0
            continue
        fi
        case "${COMP_WORDS[i]}" in
            -l|--log-level|-t|--top-dir|-n|--lines|--priority|-e|--extra-pool)
                skip_next=1
                ;;
            -*)
                ;;
            *)
                if [ -z "$verb" ]; then
                    verb="${COMP_WORDS[i]}"
                else
                    if (( positional == 0 )); then
                        pool_name="${COMP_WORDS[i]}"
                    fi
                    positional=$((positional + 1))
                fi
                ;;
        esac
    done

    if [ -z "$verb" ]; then
        opts="-h --help -l --log-level -t --top-dir -c --console-output -p --podman add-crontab add-pool add-resource clean dci-job install list log remove-crontab remove-pool remove-resource run schedule search searchdir uninstall unschedule"
        COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
        return 0
    fi

    # Subcommand-specific flags
    if [[ "$cur" == -* ]]; then
        case "$verb" in
            log)
                opts="-f --follow -n --lines"
                ;;
            schedule)
                opts="-b --block -C --command-output -f --force -r --remove-resource -p --priority -e --extra-pool"
                ;;
            run)
                opts="-C --command-output"
                ;;
            add-pool)
                opts="-n --no-install"
                ;;
            remove-pool)
                opts="-n --no-uninstall"
                ;;
            remove-resource)
                opts="-f --force"
                ;;
            *)
                return 0
                ;;
        esac
        COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
        return 0
    fi

    # First positional after verb: pool name
    case "$verb" in
        add-crontab|add-resource|clean|dci-job|install|list|log|remove-crontab|remove-pool|remove-resource|run|schedule|search|searchdir|uninstall|unschedule)
            if (( positional == 0 )); then
                opts="$(ls "$dci_queue_dir/queue" 2>/dev/null)"
                COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
                return 0
            fi
            ;;
    esac

    # Second positional: job id for verbs that take one
    if (( positional == 1 )) && [ -n "$pool_name" ]; then
        case "$verb" in
            log|dci-job)
                opts="$(ls "$dci_queue_dir/log/$pool_name/" 2>/dev/null)"
                COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
                return 0
                ;;
            unschedule)
                opts="$(ls "$dci_queue_dir/queue/$pool_name/" 2>/dev/null | grep -v '^\.' | sed 's/\.exec$//')"
                COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
                return 0
                ;;
        esac
    fi

    return 0
}

complete -F _dci_queue dci-queue

# Local variables:
# mode: shell-script
# sh-basic-offset: 4
# sh-indent-comment: t
# indent-tabs-mode: nil
# End:
# ex: ts=4 sw=4 et filetype=sh
