1 # © 2014 Raphael Dümig ( duemig at in dot tum dot de ) forum handle: "hamster"
2 # distributed under the terms of the GPLv3
4 local_config_file="$HOME/.fgfsrc"
10 len=`expr ${#opt} + 3`
12 if [ -e "$local_config_file" ]
14 stored_opts="$(cat "$local_config_file")"
17 for word in $stored_opts "${words[@]}"
19 if [ "${word:0:$len}" == "--$opt=" ]
21 # TODO: we have to get rid of at least the most important escape sequences!!!
22 value="$(echo "${word:$len}" | sed 's/\\ / /g')"
31 len=`expr ${#opt} + 3`
34 if [ -e "$local_config_file" ]
36 stored_opts="$(cat "$local_config_file")"
39 for word in "$stored_opts ${words[@]}"
41 if [ "${word:0:$len}" == "--$opt=" ]
43 value+="$(echo "${word:$len}" | sed 's/\\ / /g') "
53 # value on the command line?
58 # value stored in environment variable $FG_ROOT?
59 else if [ -n "$FG_ROOT" ]
63 # no clue! search at the most common places
65 for path in /usr{/local,}/share{/games,}/{F,f}light{G,g}ear{/data,} {~,}/Applications/FlightGear.app/Contents/Resources/data
82 # value on command line?
86 # value stored in environment variable $FG_SCENERY?
87 else if [ -n "$FG_SCENERY" ]
90 # no clue! try default:
94 scenery="$root/Scenery"
104 _get_opt "fg-aircraft"
106 # value on command line?
109 aircraft_dir="$value"
110 # value stored in environment variable $FG_AIRCRAFT?
111 else if [ -n "$FG_AIRCRAFT" ]
113 aircraft_dir="$FG_AIRCRAFT"
114 # no clue! try default:
118 aircraft_dir="$root/Aircraft"
147 _get_opts "ai-scenario"
149 scenarios="$value nimitz_demo"
155 local cur prev words cword split
156 _init_completion -s || return
158 local root airport aircraft_dir carrier scenarios scenery value
160 # auto-completion for values of keys ( --key=value )
162 --fg-aircraft|--fg-root|--fg-scenery|--flight-plan|--terrasync-dir|--materials-file|--config|--browser-app)
163 # completion of filesystem path
167 # list of scenarios in $FG_ROOT/AI
169 scenarios="$(find "$root/AI" -maxdepth 1 -iname *.xml -printf '%f\n' | sed -e 's/.xml$//')"
171 COMPREPLY=( $(compgen -W "${scenarios}" -- ${cur}) )
173 --aircraft|--vehicle)
174 # list of aircrafts in $FG_AIRCRAFT
176 aircrafts="$(find "$aircraft_dir" -maxdepth 2 -mindepth 2 -iname *-set.xml -printf '%f\n' | sed -e 's/-set.xml$//')"
178 COMPREPLY=( $(compgen -W "${aircrafts}" -- ${cur}) )
183 # is an index file present in the scenery?
184 if [ -e "$scenery/Airports/index.txt" ]
186 COMPREPLY=( $(compgen -W "$(awk 'BEGIN { FS="|"; } { print $1 }' "$scenery/Airports/index.txt")" -- ${cur}) )
188 # or at least the apt.dat file?
189 else if [ -e "$root/Airports/apt.dat.gz" ]
191 COMPREPLY=( $(compgen -W "$(zcat "$root/Airports/apt.dat.gz" | awk '/^1 / { print $5 }')" -- ${cur}) )
203 for scenario in $scenarios
205 carriers+="$(awk -- '
206 BEGIN { carrier=0; name=""; }
208 a=index($0,"<type>")+6; s=index(substr($0,a),"</type>")-1;
209 if(substr($0,a,s) == "carrier") carrier=1;
213 a=index($0,"<name>")+6; s=index(substr($0,a),"</name>")-1;
214 print substr($0,a,s);
221 }' "$root/AI/$scenario.xml") "
224 COMPREPLY=( $(compgen -W "${carriers}" ${cur}) )
230 # try to find a thresholds file
231 path="$scenery/Airports/${airport:0:1}/${airport:1:1}/${airport:2:1}"
233 if [ -e "$path/$airport.threshold.xml" ]
237 a=index($0,"<rwy>")+5;
238 s=index(substr($0,a),"</rwy>")-1;
240 }' "$path/$airport.threshold.xml")"
243 COMPREPLY=( $(compgen -W "${runways}" -- ${cur}) )
246 # try to find out the name of the carrier or the ID of the airport
254 for scenario in $scenarios
256 positions="$(awk -v carrier_name="$carrier" '
257 BEGIN { carrier=0; name=0; parkpos=0; }
259 a=index($0,"<type>")+6; s=index(substr($0,a),"</type>")-1;
260 if(substr($0,a,s) == "carrier") carrier=1;
263 parkpos=(carrier && 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);
274 carrier=name=parkpos=0;
275 }' "$root/AI/$scenario.xml")"
277 if [ -n "$positions" ]
287 # search for the groundnet or parking file
288 path="$scenery/Airports/${airport:0:1}/${airport:1:1}/${airport:2:1}"
290 if [ -e "$path/$airport.groundnet.xml" ]
292 file="$airport.groundnet.xml"
293 else if [ -e "$path/$airport.parking.xml" ]
295 file="$airport.parking.xml"
297 # no file found => do not try to analyze it!
302 # build a list of the parking positions at that airport
303 positions="$(awk -- '
310 a=index($0,"name=\"")+6;
311 s=index(substr($0,a),"\"")-1;
317 a=index($0,"number=\"")+8;
318 s=index(substr($0,a),"\"")-1;
319 number=substr($0,a,s);
323 if(pos_active == 1 && (name!="" || number!="")) print name number;
329 COMPREPLY=( $(compgen -W "${positions}" -- ${cur}) )
332 COMPREPLY=( $(compgen -W "joystick keyboard mouse" -- ${cur}) )
335 COMPREPLY=( $(compgen -W "pitot static vacuum electrical" -- ${cur}) )
339 COMPREPLY=( $(compgen -W "$(zcat "$root/Navaids/fix.dat.gz" | awk '{ print substr($3,0,5) }')" -- ${cur}) )
342 COMPREPLY=( $(compgen -W "jsb larcsim yasim magic balloon ada external null" -- ${cur}) )
345 COMPREPLY=( $(compgen -W "alpha beta early-production production" -- ${cur}) )
348 COMPREPLY=( $(compgen -W "ai environment flight general io network sound terrain" -- ${cur}) )
351 COMPREPLY=( $(compgen -W "bulk debug info warn alert" -- ${cur}) )
355 COMPREPLY=( $(compgen -W "$(zcat "$root/Navaids/nav.dat.gz" | awk '/^2 / { print $8 }')" -- ${cur}) )
360 COMPREPLY=( $(compgen -W "$(zcat "$root/Navaids/nav.dat.gz" | awk -v nav=$value '/^2 / { if($8 == nav) print $5 }')" -- ${cur}) )
363 COMPREPLY=( $(compgen -W "summer winter" -- ${cur}) )
366 COMPREPLY=( $(compgen -W "1 2 4 8 16" -- ${cur}) )
369 COMPREPLY=( $(compgen -W "real dawn morning noon afternoon dusk evening midnight" -- ${cur}) )
372 COMPREPLY=( $(compgen -W "LEFT RIGHT CENTER" -- ${cur}) )
376 COMPREPLY=( $(compgen -W "$(zcat "$root/Navaids/nav.dat.gz" | awk '/^3 / { print $8 }')" -- ${cur}) )
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}) )
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 ;;
397 complete -F _fgfs fgfs