1################################################################################
2#
3# Copyright (c) 2011-2021, EURid. All rights reserved.
4# The YADIFA TM software product is provided under the BSD 3-clause license:
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9#
10#        * Redistributions of source code must retain the above copyright
11#          notice, this list of conditions and the following disclaimer.
12#        * Redistributions in binary form must reproduce the above copyright
13#          notice, this list of conditions and the following disclaimer in the
14#          documentation and/or other materials provided with the distribution.
15#        * Neither the name of EURid nor the names of its contributors may be
16#          used to endorse or promote products derived from this software
17#          without specific prior written permission.
18#
19# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29#
30################################################################################
31
32_yadifa_ctrl_find_subcmd()
33{
34    local subcword
35    for (( subcword=1; subcword < ${#COMP_WORDS[@]}-1; subcword++ ))
36    do
37        case ${COMP_WORDS[subcword]} in
38            cfgreload|freeze|freezeall|loglevel|logreopen|notify|querylog|reload|shutdown|sync|thaw|thawall|unfreeze|unfreezeall|zonecfgreload)
39                subcmd="${COMP_WORDS[subcword]}"
40                break
41                ;;
42            *)
43                ;;
44        esac
45    done
46}
47
48_yadifa_ctrl()
49{
50    local subcmd
51    _yadifa_ctrl_find_subcmd
52
53    if [[ "x$subcmd" = "x" ]]
54    then
55        # standard options to complete
56        opts="cfgreload freeze freezeall loglevel logreopen notify querylog reload shutdown sync thaw thawall unfreeze unfreezeall zonecfgreload"
57
58    else
59        opts="-c --config -s --server -p --port -K --key-name -y --key"
60
61    fi
62
63    #
64    #   some basic options' arguments can be completed
65    #
66    case ${prev} in
67        freeze|notify|reload|sync|thaw|unfreeze|zonecfgreload)
68            _yadifa_find_all_zones
69
70            return 0
71            ;;
72        -p)
73            _yadifa_find_ports
74
75            return 0
76            ;;
77        -s|--server)
78            _yadifa_find_servers
79
80            return 0
81            ;;
82        -K|--key-name)
83            _yadifa_find_key_names
84
85            return 0
86            ;;
87        -y|--key)
88            _yadifa_find_key_names
89
90            return 0
91            ;;
92        loglevel)
93            local running="$(seq 0 15)"
94            COMPREPLY=( $( compgen -W "${running}" -- "$cur" ) )
95
96            return 0
97            ;;
98        querylog)
99            local running="enable disable"
100            COMPREPLY=( $( compgen -W "${running}" -- "$cur" ) )
101
102            return 0
103            ;;
104        -c|--config)
105            local xpat='conf'
106            _filedir "${xpat}"
107
108            return 0
109            ;;
110        -t)
111            # options to complete if '-t' has been used
112            local running="freeze unfreeze reload cfgreload sync querylog loglevel logreopen shutdown"
113            COMPREPLY=( $( compgen -W "${running}" -- "$cur" ) )
114
115            return 0
116            ;;
117        *)
118            ;;
119    esac
120
121
122    COMPREPLY=( $( compgen -W "${opts}" -- "$cur" ) )
123
124    return 0
125}
126
127_yadifa_find_keys()
128{
129    # TODO find different keys in the configuration
130    local keys
131
132    COMPREPLY=( $( compgen -W "${keys}" -- "$cur" ) )
133
134    return 0
135}
136
137_yadifa_find_key_names()
138{
139    # TODO find different key names in the configuration
140    local keynames
141
142    COMPREPLY=( $( compgen -W "${keynames}" -- "$cur" ) )
143
144    return 0
145}
146
147_yadifa_find_servers()
148{
149    # TODO find different servers in the configuration
150    local servers
151
152    COMPREPLY=( $( compgen -W "${servers}" -- "$cur" ) )
153
154    return 0
155}
156
157_yadifa_find_ports()
158{
159    # TODO find different port numbers in the configuration
160    local ports="53"
161
162    COMPREPLY=( $( compgen -W "${ports}" -- "$cur" ) )
163
164    return 0
165}
166
167_yadifa_find_all_zones()
168{
169    # TODO find all the zones in the configuration
170    local zones
171
172    COMPREPLY=( $( compgen -W "${zones}" -- "$cur" ) )
173
174    return 0
175}
176
177_yadifa_find_cmd()
178{
179    local subcword
180    for (( subcword=1; subcword < ${#COMP_WORDS[@]}-1; subcword++ ))
181    do
182        case ${COMP_WORDS[subcword]} in
183            ctrl)
184                cmd="ctrl"
185                break
186                ;;
187            *)
188                ;;
189        esac
190    done
191}
192
193_yadifa()
194{
195    local cur prev bprev cmd opts
196
197    COMPREPLY=()
198    cur="${COMP_WORDS[COMP_CWORD]}"
199    prev="${COMP_WORDS[COMP_CWORD-1]}"
200    bprev="${COMP_WORDS[COMP_CWORD-2]}"
201
202    # Look if we have a command or not
203    _yadifa_find_cmd
204
205    # standard options to complete
206    opts="ctrl -V --version --help -h -v --verbose"
207
208    case $cmd in
209        ctrl)
210            _yadifa_ctrl
211
212            return 0
213            ;;
214        *)
215            ;;
216    esac
217
218    COMPREPLY=( $( compgen -W "${opts}" -- "$cur" ) )
219
220    return 0
221} &&
222complete -F _yadifa yadifa
223