#!/bin/bash
# dpkg-query(1) bash completion
#
# Copyright © 2002 Laurent Martelli <laurent@bearteam.org>
# Copyright © 2002 Chris Lawrence <lawrencc@debian.org>
# Copyright © 2002-2004 Ian Macdonald <ian@caliban.org>
# Copyright © 2002 Rafael Sepúlveda <drs@gnulinux.org.mx>
# Copyright © 2002 Guillaume Rousse <rousse@ccr.jussieu.fr>
# Copyright © 2004 Philipp Weis <pweis@pweis.com>
# Copyright © 2004 Itay Ben-Yaacov <nib_maps@yahoo.com>
# Copyright © 2008-2011 David Paleino <d.paleino@gmail.com>
# Copyright © 2009-2026 Ville Skyttä <ville.skytta@iki.fi>
# Copyright © 2013 Igor Murzov <e-mail@date.by>
# Copyright © 2015 Andrea Dari <andreadari91@gmail.com>
# Copyright © 2018 Uwe Storbeck <uwe@ibr.ch>
# Copyright © 2018 Luca Capello <luca@pca.it>
# Copyright © 2018 Gabriel F. T. Gomes <gabriel@inconstante.eti.br>
# Copyright © 2021-2023 Koichi Murase <myoga.murase@gmail.com>
# Copyright © 2024 Arnaud Rebillout <arnaudr@kali.org>
# Copyright © 2026 Guillem Jover <guillem@debian.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

_comp_cmd_dpkg_compgen__control_files()
{
  local pkg
  pkg="${words[$((cword - 1))]}"
  _comp_compgen_split -- "$(command dpkg-query --control-list "$pkg")"
}

# dpkg-query(1) completion
#
_comp_cmd_dpkg_query()
{
  # shellcheck disable=SC2034
  local cur prev words cword was_split comp_args
  _comp_initialize -s -- "$@" || return

  local args i=$cword

  # Find the last option flag.
  if [[ $cur != -* ]]; then
    while [[ $prev != -* && $i -ne 1 ]]; do
      prev=${words[--i - 1]}
    done
  fi

  local noargopts='!(-*|*[splLWSc]*)'
  # shellcheck disable=SC2254
  case $prev in
  --status | \
  --print-avail | \
  --list | \
  --show | \
  --control-list | \
  -${noargopts}[splW])
    _comp_compgen -x dpkg installed_packages
    return
    ;;
  --control-show | \
  --control-path)
    # Find how many real args used so far.
    for ((i = 1; i < cword; i++)); do
      if [[ ${words[i]} == --@(control-show|control-path) ]]; then
        args=$((cword - i))
        break
      fi
    done

    case $args in
    1)
      _comp_compgen -x dpkg installed_packages
      return
      ;;
    2)
      _comp_cmd_dpkg_compgen__control_files
      return
      ;;
    esac
    return
    ;;
  --listfiles | \
  -${noargopts}[L])
    _comp_compgen -x dpkg purgeable_packages
    return
    ;;
  --search | \
  -${noargopts}S)
    _comp_compgen_filedir
    return
    ;;
  --admindir | \
  --root)
    _comp_compgen_filedir -d
    return
    ;;
  esac

  [[ $was_split ]] && return

  _comp_compgen_help
  [[ ${COMPREPLY-} == *= ]] && compopt -o nospace
} && complete -F _comp_cmd_dpkg_query dpkg-query
