1#compdef dcut
2
3# TODO: dcut supports two ways to be called:
4#  * dcut HOST COMMAND ...
5#  * dcut COMMAND ...
6# So ideally, if the first argument is completed, both commands and hosts should
7# be offered. If host is given, the second argument should be completed as
8# command and if not, it should be completed as command specific option.
9
10function _dput_hosts() {
11  local expl
12
13  if ( [[ ${+_dput_cfhosts} -eq 0 ]] || _cache_invalid dputhosts ) && ! _retrieve_cache dputhosts; then
14    local cmd
15    if _pick_variant dputng="usage: dput" dput -H ; then
16      cmd=(dirt hosts)
17    else
18      cmd=(dput -H)
19    fi
20     _dput_cfhosts=(${${(M)${(f)"$($cmd)"}:#*=>*}/ =>*/})
21    _store_cache dputhosts _dput_cfhosts
22  fi
23
24  _wanted dputhosts expl 'target host' compadd -a _dput_cfhosts
25}
26
27function _dcut_commands() {
28  local -a cmds
29  if _pick_variant dcutng="usage: dcut" dcut -v ; then
30    cmds=(
31      dm:'manage Debian Maintainer permissions'
32      break-the-archive:'break the archive'
33      reschedule:'reschedule a deferred upload'
34      cancel:'cancel a deferred upload'
35      rm:'remove a file from the upload queue'
36      upload:'upload a command file'
37    )
38  else
39    cmds=(
40      rm:'remove a file from the upload queue'
41      cancel:'cancel a deferred upload'
42      reschedule:'reschedule a deferred upload'
43    )
44  fi
45  _describe -t commands command cmds
46}
47
48function _dcut() {
49  local -a all_opts dcut_opts dcut_ng_opts
50  local state line context
51  local -A opt_args
52  local curcontext="${curcontext}"
53
54  all_opts=(
55    '(-c --config)'{-c,--config=}'[specify config file]:config file:_files'
56    '(-h --help)'{-h,--help}'[show help message and exit]'
57    '(-m --maintainer)'{-m,--maintainer=}'[use maintainer for upload field and GPG key selection]:maintainer address'
58    '(-k --keyid)'{-k,--keyid}'[use key id for signing]:keyid'
59    '(-O --output)'{-O,--output=}'[write command file to file instead of uploading]:output file:_files'
60    '(-P --passive)'{-P,--passive}'[use passive FTP for uploads]'
61    '(-v --version)'{-v,--version}'[show version information]'
62    ': :_dcut_commands'
63  )
64
65  dcut_opts=(
66    '(-d --debug)'{-d,--debug}'[debug mode]'
67    '(-s --simulate)'{-s,--simulate}'[simulate an upload only]'
68    '(-i --input)'{-i,--input=}'[create commands file to remove every file listed in the changes file]:changes file:_files -g"*.changes(-.)"'
69    '(-U --upload)'{-U,--upload=}'[upload commands file]:commands file:_files'
70    '(--host 1)'{--host=}'[upload to host if it is named like a command]:host:_dput_hosts'
71    '*:: :->dcut_command_arguments'
72  )
73
74  dcut_ng_opts=(
75    '*'{-d,--debug}'[enable debug messages, repeat twice to increase the verbosity level]'
76    '*'{-s,--simulate}'[simulate an upload only, repeat twice to increase level of simulation]'
77    '*:: :->dcut_ng_command_arguments'
78  )
79
80  if _pick_variant dcutng="usage: dcut" dcut -v ; then
81    _arguments -C \
82      "$all_opts[@]" "$dcut_ng_opts[@]" && return
83  else
84    _arguments -C \
85      "$all_opts[@]" "$dcut_opts[@]" && return
86  fi
87
88  case $state in
89  (dcut_command_arguments)
90    # arguments to dcut commands
91    local -a opts
92    case ${line[1]} in
93    (cancel)
94      # dcut cancel arguments
95      opts=(
96        '1::changes file:_files -g"*.changes(-.)"'
97      )
98      ;;
99    (reschedule)
100      # dcut reschedule arguments
101      opts=(
102        '1::changes file:_files -g"*.changes(-.)"'
103        '2::new delayed queue:(0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15)'
104      )
105      ;;
106    (rm)
107      # dcut rm arguments
108      opts=(
109        '--searchdirs[search in all directores for the given files]'
110        '1::file to be deleted:_files'
111      )
112      ;;
113    esac
114    _arguments "$opts[@]" && return
115    ;;
116  (dcut_ng_command_arguments)
117    # arguments to dput-ng's dcut commands
118    local -a opts
119    case ${line[1]} in
120    (cancel)
121      # dcut cancel arguments
122      opts=(
123        '(-f --filename)'{-f,--filename}'[.changes file name of the upload to be cancelled]:file name:_files -g"*.changes(-.)"'
124      )
125      ;;
126    (dm)
127      # dcut dm arguments
128      opts=(
129        '--uid[full name and address or GPG fingerprint of the Debian Maintainer]'
130        '*--allow[grant permission to upload a source package]:source package'
131        '*--deny[remove permission to upload a source pckage]:source package'
132      )
133      ;;
134    (reschedule)
135      # dcut reschedule arguments
136      opts=(
137        '(-f --filename)'{-f,--filename}'[.changes file name to be rescheduled]:file name:_files -g"*.changes(-.)"'
138        '(-d --days)'{-d,--days}'[new delayed queue]:days:(0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15)'
139      )
140      ;;
141    (rm)
142      # dcut rm arguments
143      opts=(
144        '*'{-f,--filename}'[file name to be removed]:file name:_files'
145        '--searchdirs[search in all directores for the given files]'
146      )
147      ;;
148    (upload)
149      # dcut upload arguments
150      opts=(
151        '-f --filename)'{-f,--filename}'[file to be uploaded]:file name:_files'
152      )
153      ;;
154    esac
155    _arguments "$opts[@]" && return
156    ;;
157  esac
158}
159
160_dcut "$@"
161