aboutsummaryrefslogtreecommitdiff
blob: a431a4f32f84c132edf8a2dfbce42c6dd6ab248b (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
# -*-eselect-*-  vim: ft=eselect
# Copyright (c) 2005-2012 Gentoo Foundation
#
# This file is part of the 'eselect' tools framework.
#
# eselect is free software: you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation, either version 2 of the License, or (at your option) any later
# version.
#
# eselect is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# eselect.  If not, see <http://www.gnu.org/licenses/>.

# This library is for managing environment variables like EDITOR or PAGER.
# To use it, you must set the following variables:
#
# EDITOR_VAR is the name of the environment variable, e.g. "EDITOR".
# EDITOR_ENVFILE is the path to the config file where the variable should be
# stored, e.g. "/etc/env.d/99editor". Several modules may share the same file.
# EDITOR_LIST is a space-separated list of available programs (full pathnames)
# e.g. "/bin/nano /usr/bin/emacs /usr/bin/vi". Alternatively, items can be of
# the form "name:/path/to/binary".
# EDITOR_PATH (optional) is a colon-separated list of directories where to
# search for available programs. Default is "/bin:/usr/bin".

inherit config

# find a list of valid targets
find_targets() {
	local cur i

	for i in ${EDITOR_LIST}; do
		[[ -f ${EROOT}${i#*:} ]] && echo "${EPREFIX}${i%%:*}"
	done

	# also output the current value if it isn't in our list
	cur=$(read_env_value)
	[[ -n ${cur} && ${EDITOR_LIST} != *:* && -f ${ROOT}${cur} ]] \
		&& ! has "${cur#${EPREFIX}}" ${EDITOR_LIST} \
		&& echo "${cur}"
}

# read variable value from config file
read_env_value() {
	load_config "${EROOT}${EDITOR_ENVFILE}" "${EDITOR_VAR}"
}

# write variable to config file
write_env_value() {
	[[ -w ${EROOT}${EDITOR_ENVFILE%/*} ]] \
		|| die -q "You need root privileges!"
	store_config "${EROOT}${EDITOR_ENVFILE}" "${EDITOR_VAR}" "$1"
}

### show action ###

describe_show() {
	echo "Show value of the ${EDITOR_VAR} variable in profile"
}

do_show() {
	[[ $# -gt 0 ]] && die -q "Too many parameters"

	local cur=$(read_env_value)
	write_list_start "${EDITOR_VAR} variable in profile:"
	write_kv_list_entry "${cur:-(none)}"
}

### list action ###

describe_list() {
	echo "List available targets for the ${EDITOR_VAR} variable"
}

do_list() {
	[[ $# -gt 0 ]] && die -q "Too many parameters"

	local cur targets i
	cur=$(read_env_value)
	targets=( $(find_targets) )

	write_list_start "Available targets for the ${EDITOR_VAR} variable:"
	for (( i = 0; i < ${#targets[@]}; i = i + 1 )); do
		targets[i]=${targets[i]%%:*}
		# display a star to indicate the currently chosen version
		[[ ${targets[i]} = "${cur}" ]] \
			&& targets[i]=$(highlight_marker "${targets[i]}")
	done
	write_numbered_list "${targets[@]}"

	if is_output_mode brief; then
		:
	elif [[ ${EDITOR_LIST} != *:* ]]; then
		write_numbered_list_entry " " "(free form)"
	elif [[ ${#targets[@]} -eq 0 ]]; then
		write_kv_list_entry "(none found)" ""
	fi
}

### set action ###

describe_set() {
	echo "Set the ${EDITOR_VAR} variable in profile"
}

describe_set_options() {
	echo "target : Target name or number (from 'list' action)"
}

describe_set_parameters() {
	echo "<target>"
}

do_set() {
	[[ -z $1 ]] && die -q "You didn't tell me what to set the variable to"
	[[ $# -gt 1 ]] && die -q "Too many parameters"

	local target=$1 targets=() dir ifs_save=${IFS-$' \t\n'}

	# target may be specified by its name or its index
	if is_number "${target}"; then
		targets=( $(find_targets) )
		[[ ${target} -ge 1 && ${target} -le ${#targets[@]} ]] \
			|| die -q "Number out of range: $1"
		target=${targets[target-1]%%:*}
	fi

	if [[ ${EDITOR_LIST} != *:* ]]; then
		# is the target an absolute path? if not, try to find it
		if [[ ${target} != /* ]]; then
			IFS=:
			for dir in ${EDITOR_PATH-/bin:/usr/bin}; do
				[[ -f ${EROOT}${dir}/${target} ]] || continue
				target=${EPREFIX}${dir}/${target}
				break
			done
			IFS=${ifs_save}
		fi
		# target is valid if it's a path to an existing binary
		[[ ${target} == /* && -f ${ROOT}${target} ]] \
			|| die -q "Target \"${target}\" doesn't appear to be valid!"
	else
		# target is valid only if it's in our list
		[[ ${#targets[@]} -gt 0 ]] || targets=( $(find_targets) )
		has "${target}" "${targets[@]%%:*}" \
			|| die -q "Target \"${target}\" doesn't appear to be valid!"
	fi

	echo "Setting ${EDITOR_VAR} to ${target} ..."
	write_env_value "${target}"

	# update profile
	do_action env update noldconfig
	echo "Run \". /etc/profile\" to update the variable in your shell."
}

### update action ###

describe_update() {
	echo "Update the ${EDITOR_VAR} variable if it is unset or invalid"
}

do_update() {
	[[ $# -gt 0 ]] && die -q "Too many parameters"

	local cur targets
	cur=$(read_env_value)

	[[ ${EDITOR_LIST} != *:* && ${cur} == /* && -f ${ROOT}${cur} ]] && return

	targets=( $(find_targets) )
	[[ ${#targets[@]} -gt 0 ]] \
		|| die -q "No valid target for ${EDITOR_VAR} found"

	has "${cur}" "${targets[@]%%:*}" && return

	echo "Setting ${EDITOR_VAR} to ${targets[0]%%:*} ..."
	write_env_value "${targets[0]%%:*}"

	do_action env update noldconfig
}