aboutsummaryrefslogtreecommitdiff
blob: 3a058eb3c4a01f20a8ba8e7725729e378938ea6d (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# Copyright 1999-2009 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Id$

DESCRIPTION="Manage contributed bash-completion scripts"
MAINTAINER="eselect@gentoo.org"
SVN_DATE='$Date$'
VERSION=$(svn_date_to_version "${SVN_DATE}")

find_targets() {
	local targets bc x i=0
	bcdirs[i]="${ROOT}/usr/share/bash-completion/*"

	if [[ -n "${ES_BASHCOMP_DIRS}" ]] ; then
		for x in ${ES_BASHCOMP_DIRS} ; do
			bcdirs[$((++i))]="${x}/*"
		done
	fi

	for bc in ${bcdirs[@]} ; do
		[[ -e ${bc} && ${bc} != *~ ]] && targets="${targets}\n$(basename ${bc})"
	done

	echo -ne ${targets} | sort -u
}

is_enabled() {
	local bcdir="${ROOT}/${HOME}/.bash_completion.d"

	if [[ ${1} == "--global" ]] ; then
		bcdir="${ROOT}/etc/bash_completion.d"
		shift
	fi

	[[ -e ${bcdir}/${1} ]] || return 1
	return 0
}

### list action ###

describe_list() {
	echo "List available completions"
}

describe_list_options() {
	echo "--global : List globally enabled completions"
}

do_list() {
	local opts
	targets=( $(find_targets) )
	write_list_start "Available completions:"

	if [[ ${1} == "--global" ]] ; then
		opts="--global"
		shift
	fi

	if [[ -n "${targets[@]}" ]] ; then
		for (( n = 0 ; n < ${#targets[@]} ; ++n )) ; do
			is_enabled ${opts:-} ${targets[${n}]} && \
				targets[${n}]="${targets[${n}]} $(highlight '*')"
		done
		write_numbered_list "${targets[@]}"
	else
		write_kv_list_entry "(none found)" ""
	fi

	return 0
}

### enable action ###

describe_enable() {
	echo "Enable specified completion(s)"
}

describe_enable_parameters() {
	echo "<target>"
}

describe_enable_options() {
	echo "<target> : Target name or number (from 'list' action)"
	echo "--global : Enable for all users"
}

do_enable() {
	local bc bcdir="${ROOT}/${HOME}/.bash_completion.d"

	if [[ ${1} == "--global" ]] ; then
		bcdir="${ROOT}/etc/bash_completion.d"
		shift
	fi

	[[ -z ${1} ]] && die -q "You didn't specify any completions to enable."

	# create directory if necessary
	if [[ ! -d ${bcdir} && -w $(dirname ${bcdir}) ]] ; then
		mkdir ${bcdir} || die -q "Failed to create ${bcdir}"
	elif [[ ! -d ${bcdir} ]] ; then
		die -q "You don't have permission to create ${bcdir}"
	fi

	# make sure we have proper permissions
	[[ -w ${bcdir} ]] || \
		die -q "You don't have permission to write to ${bcdir}"

	targets=( $(find_targets) )

	for bc in $@ ; do
		local file target=${bc}

		is_number "${target}" && \
			target=${targets[$(( ${target} - 1 ))]}

		[[ -z "${target}" ]] && \
			die -q "Target \"${bc}\" doesn't appear to be valid!"

		bc=${target}

		# ignore any unrecognized options
		[[ ${bc} == --* ]] && continue

		# what form is the argument in?
		case "${bc}" in
			# absolute path
			/*)
				file="${ROOT}/${bc}"
				;;
			# relative path
			*/*)
				file="${ROOT}/${PWD}/${bc}"
				;;
			# no path
			*)
				# CWD
				if [[ -f ${bc} ]] ; then
					file="${ROOT}/${PWD}/${bc}"
				# assume /usr/share/bash-completion
				elif [[ -f ${ROOT}/usr/share/bash-completion/${bc} ]]
				then
					file="${ROOT}/usr/share/bash-completion/${bc}"
				else
					if [[ -n "${ES_BASHCOMP_DIRS}" ]] ; then
						for x in ${ES_BASHCOMP_DIRS} ; do
							[[ -f ${x}/${bc} ]] && file="${x}/${bc}"
						done
					fi

					[[ -e ${file} ]] || \
						file="${ROOT}/usr/share/bash-completion/${bc}"
				fi
				;;
		esac

		# does it exist?
		if [[ ! -e ${file} ]] ; then
			write_error_msg "${file} doesn't exist"
			continue
		fi

		# already installed?
		if [[ -e ${bcdir}/$(basename ${bc}) ]] ; then
			write_error_msg "$(basename ${bc}) is already installed"
			continue
		fi

		# finally, create the symlink
		ln -s "${file}" "${bcdir}" || \
			die -q "Failed to create symlink from '${file}' to '${bcdir}'"
	done
}

### disable action ###

describe_disable() {
	echo "Disable specified completion(s)"
}

describe_disable_parameters() {
	echo "<target>"
}

describe_disable_options() {
	echo "<target> : Target name or number (from 'list' action)"
	echo "--global : Disable for all users"
}


do_disable() {
	local bc bcdir="${ROOT}/${HOME}/.bash_completion.d"

	if [[ ${1} == "--global" ]] ; then
		bcdir="${ROOT}/etc/bash_completion.d"
		shift
	fi

	[[ -z ${1} ]] && die -q "You didn't specify any completions to disable"

	targets=( $(find_targets) )

	for bc in $@ ; do
		local file target=${bc}

		is_number "${target}" && \
			target=${targets[$(( ${target} - 1 ))]}

		[[ -z "${target}" ]] && \
			die -q "Target \"${bc}\" doesn't appear to be valid!"

		bc=${target}
		file="${bcdir}/${bc}"

		# ignore any unrecognized options
		[[ ${bc} == --* ]] && continue

		# is in installed?
		if [[ ! -e ${file} ]] ; then
			write_error_msg "${bc} is not installed"
			continue
		fi

		# remove it if we have permissions
		if [[ -w $(dirname ${file}) ]] ; then
			rm "${file}" || die -q "Failed to remove ${file}"
		else
			die -q "You don't have permission to remove ${file}"
		fi
	done
}

# vim: set ft=eselect :