#!/bin/bash
# dpkg-source(1) bash completion
#
# Copyright © 2006 Sven Mueller <sven@debian.org>
# Copyright © 2008-2009 David Paleino <d.paleino@gmail.com>
# Copyright © 2009-2026 Ville Skyttä <ville.skytta@iki.fi>
# Copyright © 2009 Guillaume Rousse <rousse@ccr.jussieu.fr>
# Copyright © 2021-2024 Koichi Murase <myoga.murase@gmail.com>
# 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_source()
{
  # shellcheck disable=SC2034
  local cur prev words cword comp_args
  _comp_initialize -- "$@" || return

  local word action
  local -a options fields
  local -a packopts packoptsv1 packoptsv3quilt packoptsv3custom packoptsv3git
  local -a unpackopts unpackoptsv1 unpackoptsv3quilt

  packoptsv1=(
    -sa -sA
    -sk -sK
    -sn
    -sp -sP
    -sr -sR
    -ss
    -su -sU
    --abort-on-upstream-changes
  )
  packoptsv3quilt=(
    --abort-on-upstream-changes
    --allow-version-of-quilt-db
    --auto-commit
    --create-empty-orig
    --include-binaries
    --include-removal
    --include-timestamp
    --no-preparation
    --no-unapply-patches
    --single-debian-patch
    --unapply-patches
  )
  packoptsv3custom=(
    --target-format
  )
  packoptsv3git=(
    --git-depth
    --git-ref
  )
  packopts=(
    -D
    -F
    -I --tar-ignore
    -T
    -U
    -V
    -c
    -i --diff-ignore
    -l
    -z --compression-level
    -Z --compression
    --extend-diff-ignore
    --format
    "${packoptsv1[@]}"
    "${packoptsv3quilt[@]}"
    "${packoptsv3custom[@]}"
    "${packoptsv3git[@]}"
  )
  unpackoptsv1=(
    -sn
    -sp
    -su
    --skip-debianization
  )
  unpackoptsv3quilt=(
    --skip-debianization
    --skip-patches
  )
  unpackopts=(
    --ignore-bad-version
    --no-check
    --no-copy
    --no-overwrite-dir
    --require-strong-checksums
    --require-valid-signature
    "${unpackoptsv1[@]}"
    "${unpackoptsv3quilt[@]}"
  )
  options=(
    -b --build
    -x --extract
    --after-build
    --before-build
    --before-build
    --commit
    --print-format
    --print-format
    "${packopts[@]}"
    "${unpackopts[@]}"
  )
  fields=(
    Architecture
    Binary
    Build-Conflicts
    Build-Conflicts-Arch
    Build-Conflicts-Indep
    Build-Depends
    Build-Depends-Arch
    Build-Depends-Indep
    Checksums-Sha1
    Checksums-Sha256
    Description
    Files
    Format
    Homepage
    Maintainer
    Origin
    Package-List
    Source
    Standards-Version
    Testsuite
    Testsuite-Triggers
    Uploaders
    Vcs-Arch
    Vcs-Browser
    Vcs-Bzr
    Vcs-Cvs
    Vcs-Darcs
    Vcs-Git
    Vcs-Hg
    Vcs-Mtn
    Vcs-Svn
    Version
  )

  action=options
  for word in "${words[@]:1}"; do
    if [[ $word == -x ]]; then
      action=unpack
    elif [[ $word == -b ]]; then
      action=pack
    elif [[ $word == -h ]]; then
      action=help
    fi
  done

  case $action in
  unpack)
    case $prev in
    -x)
      _comp_compgen_filedir 'dsc'
      ;;
    *)
      _comp_compgen -- -W "${unpackopts[*]}"
      _comp_compgen -a filedir
      ;;
    esac
    return
    ;;
  pack)
    case $prev in
    -b)
      _comp_compgen_filedir -d
      ;;
    -c | -l | -T | -i | -I)
      # -c: get controlfile.
      # -l: get per-version info from this file.
      # -T: read variables here, not debian/substvars.
      # -i: <regexp> filter out files to ignore diffs of.
      # -I: filter out files when building tarballs.
      # Return directory names and file names.
      _comp_compgen_filedir
      ;;
    -F)
      # -F: force change log format.
      _comp_compgen -- -W 'debian'
      ;;
    -V)
      # -V: set a substitution variable.
      # We don't know anything about possible variables or values
      # so we don't try to suggest any completion.
      COMPREPLY=()
      ;;
    -D)
      # -D: override or add a .dsc field and value.
      # If $cur doesn't contain a = yet, suggest variable names.
      if [[ $cur == *=* ]]; then
        # $cur contains a "="
        COMPREPLY=()
      else
        _comp_compgen -- -W "${fields[*]}"
      fi
      ;;
    -U)
      # -U: remove a field.
      # Suggest possible fieldnames.
      _comp_compgen -- -W "${fields[*]}"
      ;;
    *)
      # shellcheck disable=SC2016
      _comp_compgen -- -W '${packopts[*]} ${unpackopts[*]}'
      ;;
    esac
    return
    ;;
  *)
    _comp_compgen -- -W "${options[*]}"
    return
    ;;
  esac
} && complete -F _comp_cmd_dpkg_source dpkg-source
