#!/bin/bash
# dpkg-deb(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_deb()
{
  local cur prev words cword was_split
  _comp_initialize -s -- "$@" || return

  local i=$cword

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

  local debformats=(
    0.939000
    2.0
  )
  local debcompression=(
    gzip
    none
    xz
    zstd
  )
  local debcompstrategy=(
    extreme
    filtered
    fixed
    huffman
    none
    rle
  )

  local noargopts='!(-*|*[bcIWfexXRvDzZS]*)'
  # shellcheck disable=SC2254
  case $prev in
  --contents | \
  --info | \
  --show | \
  --ctrl-tarfile | \
  --fsys-tarfile | \
  --field | \
  --control | \
  --extract | \
  --vextract | \
  --raw-extract | \
  -${noargopts}[cIWfexXR])
    _comp_compgen_filedir '?(u|d)deb'
    return
    ;;
  --build | \
  -${noargopts}b)
    _comp_compgen_filedir -d
    return
    ;;
  --deb-format)
    _comp_compgen -- -W "${debformats[*]}"
    return
    ;;
  --compression | -Z)
    _comp_compgen -- -W "${debcompression[*]}"
    return
    ;;
  --compression-level | -z)
    _comp_compgen -- -W "$(seq 0 22)"
    return
    ;;
  --compression-strategy | -S)
    _comp_compgen -- -W "${debcompstrategy[*]}"
    return
    ;;
  --threads-max)
    local nproc
    # Most Unices.
    : ${nproc:=$(getconf _NPROCESSORS_ONLN 2>/dev/null)}
    # Fallback for at least Irix.
    : ${nproc:=$(getconf _NPROC_ONLN 2>/dev/null)}
    _comp_compgen -- -W "$(seq 1 "${nproc:-1}")"
    return
    ;;
  esac

  [[ $was_split ]] && return

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