aboutsummaryrefslogtreecommitdiff
blob: 15118f1518c6d95c93fc98786809f3c80ceab9fd (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
#!@BASH@
# -*-eselect-*-  vim: ft=eselect
# Copyright (c) 2005-2013 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/>.

# Where are our data?
ESELECT_DATA_PATH="@DATADIR@/eselect"

# Where are modules installed by default?
ESELECT_DEFAULT_MODULES_PATH="${ESELECT_DATA_PATH}/modules"

# Look in these places for modules
ESELECT_MODULES_PATH=( \
	"${HOME}/.eselect/modules" \
	"${ESELECT_DEFAULT_MODULES_PATH}" )

# Look in this place for libraries
ESELECT_CORE_PATH="${ESELECT_DATA_PATH}/libs"

# Look here for the default contents of a module
ESELECT_DEFAULT_ACTIONS="${ESELECT_CORE_PATH}/default.eselect"

# Our program name and version
ESELECT_PROGRAM_NAME="eselect"
ESELECT_VERSION="@VERSION@"

# Invocation information
ESELECT_BINARY_NAME="$0"
ESELECT_KILL_TARGET="$$"

# Support variables for Gentoo Prefix
EPREFIX="@EPREFIX@"
EROOT="${ROOT%${EPREFIX:+/}}${EPREFIX}"

# Remove all alias definitions. Unset functions and variables that are
# known to cause trouble.
"unalias" -a
unset -f rm
unset CDPATH GLOBIGNORE
IFS=$' \t\n'

shopt -s extglob
shopt -s expand_aliases

# Save stderr file descriptor
if (( BASH_VERSINFO[0] == 4 && BASH_VERSINFO[1] >= 1 || BASH_VERSINFO[0] > 4 ))
then
	exec {ESELECT_STDERR}>&2	# >=bash-4.1
fi

# Load core functions
source "${ESELECT_CORE_PATH}/core.bash" || exit 255
# Load necessary functions for the main script
inherit manip output path-manipulation tests

# Sneaky trick to make die in subshells work. If you don't get
# it, don't ask...
trap 'echo "exiting" >&2; exit 250' 15

# es_do_usage
# Display eselect usage
es_do_usage() {
	echo "Usage: eselect <global options> <module name> <module options>"
}

# es_do_help
# Display eselect help
es_do_help() {
	es_do_usage
	echo
	# display all recognized global options
	write_list_start "Global options:"
	write_kv_list_entry "--brief" "Make output shorter"
	write_kv_list_entry "--colour=<yes|no|auto>" \
		"Enable or disable colour output (default 'auto')"
	echo
	# display all available eselect modules
	do_action modules list
}

# es_do_version
# Display eselect version
es_do_version() {
	echo "eselect ${ESELECT_VERSION}"
	echo
	echo "Copyright (c) 2005-2013 Gentoo Foundation."
	echo "Distributed under the terms of the GNU GPL version 2 or later."
}

### main code ###

# figure out what the action is. we need to know whether we're
# invoked as a something-config/something-update.
action=""
for suffix in config update{,r} tool manager reader; do
	if [[ ${0%%-${suffix}} != "$0" ]]; then
		action=$(basename "$0")
		action=${action%%-${suffix}}
		break
	fi
done
unset suffix

if [[ -z ${action} ]]; then
	binname=$(basename "$0")
	for prefix in config update{,r} manage 'read'; do
		if [[ ${binname##${prefix}-} != ${binname} ]]; then
			action=$(basename "$0")
			action=${action##${prefix}-}
			break
		fi
	done
	unset binname prefix
fi

if [[ -z ${action} ]] && [[ -n ${1##--} ]]; then
	while [[ ${1##--} != "$1" ]]; do
		case ${1##--} in
			brief)
				set_output_mode brief
				;;
			colour=*|color=*|colour|color)
				# accept all arguments that are valid for ls or emerge
				case ${1#*=} in
					yes|y|always|force|$1) colour=yes ;;
					no|n|never|none) colour=no ;;
					auto|tty|if-tty) colour="" ;;
					*) die -q "Invalid argument for ${1%%=*} option" ;;
				esac
				;;
			help|version)
				action=${1##--}
				;;
			*)
				die -q "Unknown option $1"
				;;
		esac
		shift
	done
	if [[ -z ${action} ]]; then
		action=$1
		shift
	fi
fi

# enable colour output and get width of terminal iff stdout is a tty
if [[ -t 1 ]]; then
	colours ${colour:-yes}
	init_columns
else
	colours ${colour:-no}
fi
unset colour

if [[ -n ${action} ]]; then
	if is_function "es_do_${action}"; then
		[[ $# -gt 0 ]] && die -q "Too many parameters"
		es_do_${action}
	else
		do_action "${action}" "$@"
	fi
else
	es_do_help
fi