aboutsummaryrefslogtreecommitdiff
blob: 8973e891012f9addb49fcd98df7d5594aa3cfec8 (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
#!/bin/bash

# Copyright (c) 2005-2009 Gentoo Foundation.
# $Id$
# 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_VERSION="%VERSION%"
ESELECT_PROGRAM_NAME="eselect"

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

# Global options
ESELECT_KNOWN_OPTIONS="no-colour no-color help version"
ESELECT_OPTIONS=""

# 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

# 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

# ec_find_module foo
# Find and echo the filename of the foo module. If there's no foo module,
# die.
ec_find_module() {
    local modname="$1" modpath="" modfile=""
    [[ -z ${modname} ]] && die "Usage: ${FUNCNAME} <module>"
    for modpath in "${ESELECT_MODULES_PATH[@]}" ; do
        [[ -f ${modpath}/${modname}.eselect ]] && break
    done

    modfile="${modpath}/${modname}.eselect"
    [[ -r ${modfile} ]] || die -q "Can't load module ${modname}"
    echo ${modfile}
}

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

# ec_do_help
# Display eselect help
ec_do_help() {
    ec_do_usage
    echo
    ec_do_list-options
    echo
    ec_do_list-modules
}

# ec_do_version
# Display eselect version
ec_do_version() {
    echo "eselect ${ESELECT_VERSION}"
    echo
    echo "Copyright (c) 2005-2009 Gentoo Foundation."
    echo "Distributed under the terms of the GNU General Public License v2."
}

# ec_do_list-options
# Display all recognized global options
ec_do_list-options() {
    write_list_start "Global options:"
    write_kv_list_entry "--no-color,--no-colour"    "Disable coloured output"
}

# ec_do_list-modules
# Display all available eselect modules DEPRECATED
ec_do_list-modules() {
    do_action modules list "$@"
}

### main code ###

# enable colour output and get width of terminal iff stdout is a tty
if [[ -t 1 ]]; then
    colours
    init_columns
else
    nocolours
fi

# 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
        has ${1##--} ${ESELECT_KNOWN_OPTIONS} || \
            die -q "Unknown option ${1}!"
        case ${1##--} in
            no-colour|no-color)
                ESELECT_OPTIONS="${ESELECT_OPTIONS} NOCOLOUR"
                nocolours
                ;;
            help|version)
                action=${1##--}
                ;;
        esac
        shift
    done
    if [[ -z ${action} ]]; then
        action=${1}
        shift
    fi
fi

if [[ -n ${action} ]] ; then
    if is_function "ec_do_${action}" ; then
        ec_do_${action} "$@"
    else
        do_action "${action}" "$@"
    fi
else
    ec_do_help
fi

# Local Variables:
# sh-indentation: 4
# indent-tabs-mode: nil
# End:

# vim: set sw=4 et sts=4 tw=80 :