# /usr/share/bash-completion/completions/salsa
# Bash command completion for ‘salsa(1)’.
# Documentation: ‘bash(1)’, section “Programmable Completion”.

shopt -s progcomp

_salsa_completion () {
    COMPREPLY=()

    local cur="${COMP_WORDS[COMP_CWORD]}"
    local prev="${COMP_WORDS[COMP_CWORD-1]}"

    local opts=""
# Source: ./lib/Devscripts/Config.pm:sub parse_command_line
    opts+=" --help"
# Source: ./lib/Devscripts/Output.pm:sub ds_prompt
    opts+=" --info"
# Source: ./lib/Devscripts/Config.pm:[...]$ARGV[0]
    opts+=" --conf-file --no-conf"
# Source: ./lib/Devscripts/Salsa/Config.pm:use constant keys
# Note: '<VALUE>!' == '--<VALUE> --no-<VALUE>', '<SHORT>|<LONG>'
# Headings: $ grep '^    #' ./lib/Devscripts/Salsa/Config.pm
    # General salsa
    opts+=" --chdir --cache-file --no-cache --path"
    # Responses
    opts+=" --yes --no-yes --no-fail"
    # Output
    opts+=" --verbose --no-verbose --debug --info"
    # General GitLab
    opts+=" --user --user-id --group --group-id --token --token-file"
    # List/search
    opts+=" --all --all-archived --archived --no-archived"
    opts+=" --skip --skip-file --no-skip"
    # Features
    opts+=" --analytics --auto-devops --container --environments"
    opts+=" --feature-flags --forks --infrastructure --issues --jobs --lfs"
    opts+=" --monitor --mr --packages --pages --releases --repo --service-desk"
    opts+=" --request-access --requirements --security-compliance "
    opts+=" --snippets --wiki"
    # Branding
    opts+=" --avatar-path --desc --no-desc --desc-pattern"
    # Notification
    opts+=" --email --no-email --disable-email --no-disable-email"
    opts+=" --email-recipient --irc-channel --irker --no-irker --disable-irker"
    opts+=" --no-disable-irker --irker-host --irker-port --kgb --no-kgb"
    opts+=" --disable-kgb --no-disable-kgb --kgb-options --tagpending"
    opts+=" --no-tagpending --disable-tagpending --no-disable-tagpending"
    # Branch
    opts+=" --rename-head --no-rename-head --source-branch --dest-branch --debian-branch"
    opts+=" --enable-remove-source-branch --no-enable-remove-source-branch"
    opts+=" --disable-remove-source-branch --no-disable-remove-source-branch"
    # Merge requests
    opts+=" --mr-allow-squash --no-mr-allow-squash --mr-desc --mr-dst-branch"
    opts+=" --mr-dst-project --mr-remove-source-branch"
    opts+=" --no-mr-remove-source-branch --mr-src-branch --mr-src-project"
    opts+=" --mr-title"
    # CI
    opts+=" --build-timeout --ci-config-path"
    opts+=" --retry-failed-pipelines"
    # Pipeline schedules
    opts+=" --schedule-desc --schedule-ref --schedule-cron --schedule-tz"
    opts+=" --schedule-enable --no-schedule-enable --schedule-disable"
    opts+=" --no-schedule-disable --schedule-run --no-schedule-run"
    opts+=" --schedule-delete --no-schedule-delete"
    # Manage other GitLab instances
    opts+=" --api-url --git-server-url --irker-server-url --kgb-server-url"
    opts+=" --tagpending-server-url"

# Source: ./lib/Devscripts/Salsa.pm:sub run -> $ ls ./lib/Devscripts/Salsa/*.pm
# Skipping: Config Hooks Repo -> `with "Devscripts::Salsa::<VALUE>";`
#           Then filter from: ./lib/Devscripts/Salsa.pm:use constant cmd_aliases -> Preferred terminology
    local commands=""
# Headings: $ grep '^=head' ./scripts/salsa.pl
    # Managing users and groups
    commands+=" add_user join list_groups update_user whoami"
    # Managing projects
    commands+=" checkout fork forks last_ci_status merge_request"
    commands+=" merge_requests pipeline_schedule pipeline_schedules"
    commands+=" protect_branch protected_branches push push_repo"
    commands+=" rename_branch update_safe update_watch5 path"
    # Other
    commands+=" purge_cache"

# Aliases source: ./lib/Devscripts/Salsa.pm:use constant cmd_aliases -> Preferred terminology
    commands+=" check_projects create_project delete_project delete_user"
    commands+=" list_projects list_users search_groups search_projects "
    commands+=" search_users update_projects"

    # Disable completion for the arguments which require variables afterwards
    case "${prev}" in
      --api-url) ;&
      --desc-pattern) ;&
      --irc-channel) ;&
      --path) ;&
      --group-id) ;&
      --user-id)
      COMPREPLY=()
        ;;
      *)
        if [[ "$cur" == -* ]]; then
            COMPREPLY=( $( compgen -W "$opts" -- $cur ) )
        else
            COMPREPLY=( $( compgen -W "$commands" -- $cur ) )
        fi
    ;;
    esac
    return 0
}

complete -F _salsa_completion salsa
