]> git.mxchange.org Git - flightgear.git/blob - scripts/completion/fg-completion.bash
Fix unused constants in YASim
[flightgear.git] / scripts / completion / fg-completion.bash
1 # © 2014 Raphael Dümig ( duemig at in dot tum dot de ) forum handle: "hamster"
2 # distributed under the terms of the GPLv3
3
4 local_config_file="$HOME/.fgfsrc"
5
6 _get_opt()
7 {
8     local opt len
9     opt=$1
10     len=`expr ${#opt} + 3`
11     
12     if [ -e "$local_config_file" ]
13     then
14         stored_opts="$(cat "$local_config_file")"
15     fi
16     
17     for word in $stored_opts "${words[@]}"
18     do
19         if [ "${word:0:$len}" == "--$opt=" ]
20         then
21             # TODO: we have to get rid of at least the most important escape sequences!!!
22             value="$(echo "${word:$len}" | sed 's/\\ / /g')"
23         fi
24     done
25 }
26
27 _get_opts()
28 {
29     local opt len
30     opt=$1
31     len=`expr ${#opt} + 3`
32     value=""
33     
34     if [ -e "$local_config_file" ]
35     then
36         stored_opts="$(cat "$local_config_file")"
37     fi
38     
39     for word in "$stored_opts ${words[@]}"
40     do
41         if [ "${word:0:$len}" == "--$opt=" ]
42         then
43             value+="$(echo "${word:$len}" | sed 's/\\ / /g') "
44         fi
45     done
46 }
47
48 _get_fg_root()
49 {
50     local value
51     _get_opt "fg-root"
52     
53     # value on the command line?
54     if [ -n "$value" ]
55     then
56         root="$value"
57         return 0
58     # value stored in environment variable $FG_ROOT?
59     else if [ -n "$FG_ROOT" ]
60     then
61         root="$FG_ROOT"
62         return 0
63     # no clue! search at the most common places
64     else
65         for path in /usr{/local,}/share{/games,}/{F,f}light{G,g}ear{/data,} {~,}/Applications/FlightGear.app/Contents/Resources/data
66         do
67             if [ -e "$path" ]
68             then
69                 root="$path"
70                 break
71             fi
72         done
73     fi
74     fi
75 }
76
77 _get_fg_scenery()
78 {
79     local value
80     _get_opt "fg-scenery"
81     
82     # value on command line?
83     if [ -n "$value" ]
84     then
85         scenery="$value"
86     # value stored in environment variable $FG_SCENERY?
87     else if [ -n "$FG_SCENERY" ]
88     then
89         scenery="$FG_SCENERY"
90     # no clue! try default:
91     else
92         local root
93         _get_fg_root
94         scenery="$root/Scenery"
95     fi
96     fi
97     
98     return 0
99 }
100
101 _get_fg_aircraft()
102 {
103     local value
104     _get_opt "fg-aircraft"
105     
106     # value on command line?
107     if [ -n "$value" ]
108     then
109         aircraft_dir="$value"
110     # value stored in environment variable $FG_AIRCRAFT?
111     else if [ -n "$FG_AIRCRAFT" ]
112     then
113         aircraft_dir="$FG_AIRCRAFT"
114     # no clue! try default:
115     else
116         local root
117         _get_fg_root
118         aircraft_dir="$root/Aircraft"
119     fi
120     fi
121 }
122
123 _get_airport()
124 {
125     local value
126     _get_opt "airport"
127     
128     if [ -z "$value" ]
129     then
130         airport="KSFO"
131     else
132         airport=$value
133     fi
134 }
135
136 _get_carrier()
137 {
138     local value
139     _get_opt "carrier"
140     
141     carrier=$value
142 }
143
144 _get_scenarios()
145 {
146     local value
147     _get_opts "ai-scenario"
148     
149     scenarios="$value nimitz_demo"
150 }
151
152
153 _fgfs() 
154 {
155     local cur prev words cword split
156     _init_completion -s || return
157
158     local root airport aircraft_dir carrier scenarios scenery value
159     
160     # auto-completion for values of keys ( --key=value )
161     case "$prev" in
162         --fg-aircraft|--fg-root|--fg-scenery|--flight-plan|--terrasync-dir|--materials-file|--config|--browser-app)
163             # completion of filesystem path
164             _filedir
165             return 0 ;;
166         --ai-scenario)
167             # list of scenarios in $FG_ROOT/AI
168             _get_fg_root
169             scenarios="$(find "$root/AI" -maxdepth 1 -iname *.xml -printf '%f\n' | sed -e 's/.xml$//')"
170             
171             COMPREPLY=( $(compgen -W "${scenarios}" -- ${cur}) )
172             return 0 ;;
173         --aircraft|--vehicle)
174             # list of aircrafts in $FG_AIRCRAFT
175             _get_fg_aircraft
176             aircrafts="$(find "$aircraft_dir" -maxdepth 2 -mindepth 2 -iname *-set.xml -printf '%f\n' | sed -e 's/-set.xml$//')"
177             
178             COMPREPLY=( $(compgen -W "${aircrafts}" -- ${cur}) )
179             return 0 ;;
180         --airport)
181             _get_fg_root
182             _get_fg_scenery
183             # is an index file present in the scenery?
184             if [ -e "$scenery/Airports/index.txt" ]
185             then
186                 COMPREPLY=( $(compgen -W "$(awk 'BEGIN { FS="|"; } { print $1 }' "$scenery/Airports/index.txt")" -- ${cur}) )
187                 return 0
188             # or at least the apt.dat file?
189             else if [ -e "$root/Airports/apt.dat.gz" ]
190             then
191                 COMPREPLY=( $(compgen -W "$(zcat "$root/Airports/apt.dat.gz" | awk '/^1 / { print $5 }')" -- ${cur}) )
192                 return 0
193             fi
194             fi
195             
196             return 0 ;;
197         --carrier)
198             _get_fg_root
199             _get_scenarios
200             
201             carriers=""
202             
203             for scenario in $scenarios
204             do
205                 carriers+="$(awk -- '
206 BEGIN { carrier=0; name=""; }
207 /<type>/ {
208     a=index($0,"<type>")+6; s=index(substr($0,a),"</type>")-1;
209     if(substr($0,a,s) == "carrier") carrier=1;
210 }
211 /<name>/ {
212     if(carrier) {
213         a=index($0,"<name>")+6; s=index(substr($0,a),"</name>")-1;
214         print substr($0,a,s);
215         carrier=0;
216     }
217 }
218 /<\/entry>/ {
219     carrier=0;
220     name="";
221 }' "$root/AI/$scenario.xml") "
222             done
223             
224             COMPREPLY=( $(compgen -W "${carriers}" ${cur}) )
225             return 0 ;;
226         --runway)
227             _get_fg_scenery
228             _get_airport
229             
230             # try to find a thresholds file
231             path="$scenery/Airports/${airport:0:1}/${airport:1:1}/${airport:2:1}"
232             
233             if [ -e "$path/$airport.threshold.xml" ]
234             then
235                 runways="$(awk -- '
236 /<rwy>/ {
237     a=index($0,"<rwy>")+5;
238     s=index(substr($0,a),"</rwy>")-1;
239     print substr($0,a,s)
240 }' "$path/$airport.threshold.xml")"
241             fi
242             
243             COMPREPLY=( $(compgen -W "${runways}" -- ${cur}) )
244             return 0 ;;
245         --parkpos)
246             # try to find out the name of the carrier or the ID of the airport
247             _get_carrier
248             
249             if [ -n "$carrier" ]
250             then
251                 _get_fg_root
252                 _get_scenarios
253                 
254                 for scenario in $scenarios
255                 do
256                     positions="$(awk -v carrier_name="$carrier" '
257 BEGIN { carrier=0; name=0; parkpos=0; }
258 /<type>/ {
259     a=index($0,"<type>")+6; s=index(substr($0,a),"</type>")-1;
260     if(substr($0,a,s) == "carrier") carrier=1;
261 }
262 /<parking-pos>/ {
263     parkpos=(carrier && name);
264 }
265 /<name>/ {
266     a=index($0,"<name>")+6; s=index(substr($0,a),"</name>")-1;
267     if(parkpos) print substr($0,a,s);
268     else if(carrier) name=(substr($0,a,s) == carrier_name);
269 }
270 /<\/parking-pos>/ {
271     parkpos=0;
272 }
273 /<\/entry>/ {
274     carrier=name=parkpos=0;
275 }' "$root/AI/$scenario.xml")"
276                     
277                     if [ -n "$positions" ]
278                     then
279                         break
280                     fi
281                 done
282                 
283             else
284                 _get_fg_scenery
285                 _get_airport
286                 
287                 # search for the groundnet or parking file
288                 path="$scenery/Airports/${airport:0:1}/${airport:1:1}/${airport:2:1}"
289                 
290                 if [ -e "$path/$airport.groundnet.xml" ]
291                 then
292                     file="$airport.groundnet.xml"
293                 else if [ -e "$path/$airport.parking.xml" ]
294                 then
295                     file="$airport.parking.xml"
296                 else
297                     # no file found => do not try to analyze it!
298                     return 0
299                 fi
300                 fi
301                 
302                 # build a list of the parking positions at that airport
303                 positions="$(awk -- '
304 /<Parking/ {
305     pos_active=1;
306     name=number="";
307 }
308 /name="/ {
309     if(pos_active) {
310         a=index($0,"name=\"")+6;
311         s=index(substr($0,a),"\"")-1;
312         name=substr($0,a,s);
313     }
314 }
315 /number="/ {
316     if(pos_active) {
317         a=index($0,"number=\"")+8;
318         s=index(substr($0,a),"\"")-1;
319         number=substr($0,a,s);
320     }
321 }
322 /\/>/ {
323     if(pos_active == 1 && (name!="" || number!="")) print name number;
324     pos_active=0;
325     name=number="";
326 }' "$path/$file")"
327             fi
328             
329             COMPREPLY=( $(compgen -W "${positions}" -- ${cur}) )
330             return 0 ;;
331         --control)
332             COMPREPLY=( $(compgen -W "joystick keyboard mouse" -- ${cur}) )
333             return 0 ;;
334         --failure)
335             COMPREPLY=( $(compgen -W "pitot static vacuum electrical" -- ${cur}) )
336             return 0 ;;
337         --fix)
338             _get_fg_root
339             COMPREPLY=( $(compgen -W "$(zcat "$root/Navaids/fix.dat.gz" | awk '{ print substr($3,0,5) }')" -- ${cur}) )
340             return 0 ;;
341         --fdm)
342             COMPREPLY=( $(compgen -W "jsb larcsim yasim magic balloon ada external null" -- ${cur}) )
343             return 0 ;;
344         --min-status)
345             COMPREPLY=( $(compgen -W "alpha beta early-production production" -- ${cur}) )
346             return 0 ;;
347         --log-class)
348             COMPREPLY=( $(compgen -W "ai environment flight general io network sound terrain" -- ${cur}) )
349             return 0 ;;
350         --log-level)
351             COMPREPLY=( $(compgen -W "bulk debug info warn alert" -- ${cur}) )
352             return 0 ;;
353         --ndb)
354             _get_fg_root
355             COMPREPLY=( $(compgen -W "$(zcat "$root/Navaids/nav.dat.gz" | awk '/^2 / { print $8 }')" -- ${cur}) )
356             return 0 ;;
357         --ndb-frequency)
358             _get_fg_root
359             _get_opt "ndb"
360             COMPREPLY=( $(compgen -W "$(zcat "$root/Navaids/nav.dat.gz" | awk -v nav=$value '/^2 / { if($8 == nav) print $5 }')" -- ${cur}) )
361             return 0 ;;
362         --season)
363             COMPREPLY=( $(compgen -W "summer winter" -- ${cur}) )
364             return 0 ;;
365         --texture-filtering)
366             COMPREPLY=( $(compgen -W "1 2 4 8 16" -- ${cur}) )
367             return 0 ;;
368         --timeofday)
369             COMPREPLY=( $(compgen -W "real dawn morning noon afternoon dusk evening midnight" -- ${cur}) )
370             return 0 ;;
371         --view-offset)
372             COMPREPLY=( $(compgen -W "LEFT RIGHT CENTER" -- ${cur}) )
373             return 0 ;;
374         --vor)
375             _get_fg_root
376             COMPREPLY=( $(compgen -W "$(zcat "$root/Navaids/nav.dat.gz" | awk '/^3 / { print $8 }')" -- ${cur}) )
377             return 0 ;;
378         --vor-frequency)
379             _get_fg_root
380             _get_opt "vor"
381             COMPREPLY=( $(compgen -W "$(zcat "$root/Navaids/nav.dat.gz" | awk -v nav=$value '/^3 / { if($8 == nav) print substr($5,0,3) "." substr($5,4) }')" -- ${cur}) )
382             return 0 ;;
383     esac
384     
385     case "${cur}" in
386         -*)
387             # auto completion for options
388             COMPREPLY=( $(compgen -W "$(_parse_help fgfs "--help --verbose")" -- ${cur}) )
389             # no whitespace after keys
390             [[ $COMPREPLY == *= ]] && compopt -o nospace ;;
391         *)
392             _filedir
393     esac
394     
395     return 0
396 }
397 complete -F _fgfs fgfs