aboutsummaryrefslogtreecommitdiff
blob: f91c1c54d50763d30e7d02364cfdd242014690d9 (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
# -*-eselect-*-  vim: ft=eselect
# Copyright 2005-2020 Gentoo Authors
# Distributed under the terms of the GNU GPL version 2 or later

inherit config

DESCRIPTION="Manage the LANG environment variable"
MAINTAINER="matsuu@gentoo.org"

# file name as documented in http://www.gentoo.org/doc/en/utf-8.xml
LOCALE_ENVFILE="/etc/env.d/02locale"

locale_list() {
	"${EROOT}/usr/bin/locale" -a
}

validate_locale() {
	local lang=$1
	# exit status of locale is unreliable, check for output to stderr instead
	[[ -z $(LC_ALL=${lang} "${EROOT}/usr/bin/locale" 2>&1 >/dev/null) ]]
}

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

	list=$(locale_list)
	echo ${list}

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

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

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

### show action ###

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

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

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

### list action ###

describe_list() {
	echo "List available targets for the LANG 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 LANG 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 [[ ${#targets[@]} -eq 0 ]]; then
		write_kv_list_entry "(none found)" ""
	else
		write_numbered_list_entry " " "(free form)"
	fi
}

### set action ###

describe_set() {
	echo "Set the LANG 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=()

	# 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

	validate_locale "${target}" \
		|| die -q "Target \"${target}\" doesn't appear to be valid!"

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

	# update profile
	do_action env update noldconfig
	if [[ ${ROOT:-/} = / ]]; then
		echo "Run \". ${EROOT}/etc/profile\" to update the variable" \
			"in your shell."
	fi
}