summaryrefslogtreecommitdiffstats
path: root/dkms.bash-completion.in
blob: 4cddcab2ae61974280b9123028442cab468e78c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# shellcheck shell=bash
# shellcheck disable=SC2207
# Based on the completion from the Mandriva dkms package.

# This function completes available kernels
_kernels()
{
	COMPREPLY=( $( cd @MODDIR@ && compgen -d -- "$cur" ) )
}

# complete on full directory names under $1
_subdirectories()
{
	COMPREPLY=( $( cd "$1" && compgen -d -- "$cur" ) )
}

# complete on $2 part of filenames matching pattern $1 under /usr/src
_filename_parts()
{
	COMPREPLY=( $( command ls -F /usr/src/ 2>/dev/null | grep -E '^'$1'/$' \
		| sed -r -e 's/^([^-]+)-(.+)\/$/\'$2'/' | grep "^$cur" ) )
}

_dkms()
{
	local cur prev command module i

	COMPREPLY=()
	cur=${COMP_WORDS[COMP_CWORD]}

	if [[ $COMP_CWORD -eq 1 ]] ; then
		COMPREPLY=( $( compgen -W "add remove build unbuild install uninstall autoinstall \
			match mktarball ldtarball \
			status" -- $cur ) )
	else
		prev=${COMP_WORDS[COMP_CWORD-1]}
		command=${COMP_WORDS[1]}
		case $prev in
			-a)
				COMPREPLY=( $( compgen -W "$(uname -m)" -- $cur ) )
				return 0
				;;
			-m)
				if [ "$command" = 'add' ]; then
					_filename_parts '.*-.*' 1
				else
					_subdirectories /var/lib/dkms
				fi
				return 0
				;;
			-v)
				for (( i=1; i < COMP_CWORD; i++ )); do
					if [[ "${COMP_WORDS[i]}" == -m ]]; then
						module=${COMP_WORDS[i+1]}
						break
					fi
				done
				if [ -n "$module" ]; then
					if [ "$command" = 'add' ]; then
						_filename_parts "$module-.*" 2
					else
						_subdirectories /var/lib/dkms/$module
					fi
					return 0
				fi
				;;
			-k|--templatekernel)
				_kernels
				return 0
				;;
			-c|--spec|--archive|--config)
				_filedir
				return 0
				;;
			--kernelsourcedir|--dkmstree|--sourcetree|--installtree)
				_filedir -d
				return 0
				;;
		esac


		if [[ "$cur" == -* ]]; then
			case $command in
				add)
					options='-c --rpm_safe_upgrade'
					;;
				remove)
					options='--rpm_safe_upgrade'
					;;
				build)
					options='--config --force'
					;;
				unbuild)
					options=''
					;;
				install)
					options='--force'
					;;
				uninstall)
					options=''
					;;
				autoinstall)
					options=''
					;;
				match)
					options='--templatekernel'
					;;
				mktarball)
					options='--source-only --binaries-only'
					;;
				ldtarball)
					options='--archive --force'
					;;
				status)
					options=''
				;;
			esac

			options="$options -m -v -k -a --arch -q --quiet -V \
				--version --all --kernelsourcedir \
				--directive"

			COMPREPLY=( $( compgen -W "$options" -- $cur ) )
		fi
	fi
}
complete -F _dkms dkms