cd647e2096b61ce583a4bd48318484cb814762e9
[paraslash.git] / bash_completion
1 # Copyright (C) 2007 Andre Noll <maan@systemlinux.org>
2 #
3 # Licensed under the GPL v2. For licencing details see COPYING.
4
5 PC="para_client -l error -- "
6
7 __para_commandlist=
8 __para_sender_list=
9 __para_table_list=
10 __para_mood_list=
11 __para_lyrics_list=
12 __para_image_list=
13 __para_playlist_list=
14 __para_attributes_list=
15
16 __paracomp()
17 {
18         local cur="${COMP_WORDS[COMP_CWORD]}"
19         if [ $# -gt 2 ]; then
20                 cur="$3"
21         fi
22         COMPREPLY=($(compgen -P "$2" -W "$1" -- "$cur"))
23 }
24
25 __para_complete_file()
26 {
27         local cur="${COMP_WORDS[COMP_CWORD]}"
28         local sed_cmd="-e s|^\($cur[^/]\+/\).*|\1|1"
29         $PC ls -p -sp "${cur}*" | sed $sed_cmd | uniq
30 }
31
32 __para_complete_command()
33 {
34         if test -z "$__para_command_list"; then
35                 __para_command_list="$($PC help | cut -f 1)"
36         fi
37         echo "$__para_command_list"
38 }
39
40 __para_complete_table()
41 {
42         if test -z "$__para_table_list"; then
43                 __para_table_list="$(ls $HOME/.paraslash/afs_database/)"
44         fi
45         echo "$__para_table_list"
46 }
47 __para_complete_sender()
48 {
49         if test -z "$__para_sender_list"; then
50                 __para_sender_list="$($PC si | grep "supported senders:" | sed -e 's/.*: //')"
51         fi
52         echo "$__para_sender_list"
53 }
54 __para_complete_attribute()
55 {
56         if test -z "$__para_attributes_list"; then
57                 __para_attributes_list="$($PC lsatt)"
58         fi
59         echo "$__para_attributes_list"
60 }
61 __para_complete_playlist()
62 {
63         if test -z "$__para_playlist_list"; then
64                 __para_playlist_list="$($PC lspl)"
65         fi
66         echo "$__para_playlist_list"
67 }
68 __para_complete_mood()
69 {
70         if test -z "$__para_mood_list"; then
71                 __para_mood_list="$($PC lsmood)"
72         fi
73         echo "$__para_mood_list"
74 }
75 __para_complete_image()
76 {
77         if test -z "$__para_image_list"; then
78                 __para_image_list="$($PC lsimg)"
79         fi
80         echo "$__para_imagelist"
81 }
82 __para_complete_lyrics()
83 {
84         if test -z "$__para_lyrics_list"; then
85                 __para_lyrics_list="$($PC lslyr)"
86         fi
87         echo "$__para_lyrics_list"
88 }
89
90 __para_select()
91 {
92         local cur="${COMP_WORDS[COMP_CWORD]}"
93         case "$cur" in
94         *)
95                 __paracomp "$($PC lspl "${cur}*" | sed -e 's|^|p/|1') $($PC lsmood | sed -e 's|^|m/|1')"
96                 ;;
97         esac
98 }
99
100 __para_setatt()
101 {
102         local cur="${COMP_WORDS[COMP_CWORD]}"
103         if [ $COMP_CWORD -lt 3 ]; then
104                 __paracomp "$(__para_complete_attribute)"
105         else
106                 if test -z "$cur" -o "$cur" = "${cur#/}"; then
107                         __paracomp "$(__para_complete_attribute)"
108                 else
109                         __paracomp "$(__para_complete_file)"
110                 fi
111         fi
112 }
113
114 __para_sender()
115 {
116         if test $COMP_CWORD -eq 2; then
117                 __paracomp "$(__para_complete_sender)"
118         elif test $COMP_CWORD -eq 3; then
119                 __paracomp "on off add delete allow deny help"
120         else
121                 COMPREPLY=()
122         fi
123 }
124
125 _para_client()
126 {
127         local i c=1 command
128
129
130         while [ $c -lt $COMP_CWORD ]; do
131                 i="${COMP_WORDS[c]}"
132                 case "$i" in
133                 --*) ;;
134                 *) command="$i"; break ;;
135                 esac
136                 c=$((++c))
137         done
138
139         if [ $c -eq $COMP_CWORD ]; then
140                 case "${COMP_WORDS[COMP_CWORD]}" in
141                 --*=*) COMPREPLY=();;
142                 *) __paracomp "$(__para_complete_command)";;
143                 esac
144                 return
145         fi
146
147         case "$command" in
148         stop|play|term|hup|pause|nomore|si|version) COMPREPLY=();;
149         setatt) __para_setatt;;
150         select) __para_select;;
151         touch|ls|rm|cpsi) __paracomp "$(__para_complete_file)";;
152         mvatt|lsatt|rmatt) __paracomp "$(__para_complete_attribute)";;
153         help) __paracomp "$(__para_complete_command)";;
154         sender) __para_sender;;
155         init) __paracomp __paracomp "$(__para_complete_table)";;
156         mvmood|lsmood|rmmood) __paracomp "$(__para_complete_mood)";;
157         mvlyr|lslyr|rmlyr) __paracomp "$(__para_complete_lyrics)";;
158         mvimg|lsimg|rmimg) __paracomp "$(__para_complete_image)";;
159         mvpl|lspl|rmpl) __paracomp "$(__para_complete_playlist)";;
160         esac
161
162 }
163 complete -o default -o nospace -F _para_client para_client
164 complete -o default -o nospace -F _para_client para
165