aboutsummaryrefslogtreecommitdiff
blob: 3930ffa7a4d8a3536bda19d7547c6bc8a3be195d (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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# Copyright 1999-2009 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Id$

# To use this library, you must set MODULE and IFACE.
# MODULE is the name you use to refer to whatever it is that you're selecting
# in help text and so forth.
# IFACE is the subdirectory of /etc/env.d/ that eselect config files are stored
# in for these packages.
#
# There is a file installed at /etc/env.d/$IFACE/$libdir/config containing the
# CURRENT variable, which is set to the current eselect setting. Also, each
# implementation installs a single file at /etc/env.d/$IFACE/$libdir/$implem.
# This file contains a list of symlinks to be created in "source destination"
# format. You must use relative symlinks. In other words, "source" must be
# relative to "destination" but "destination" must be absolute. You may have
# comments in the symlink map file -- any line containing a '#' is considered
# a comment. One caveat about the symlink map -- instead of using "lib" or
# "lib64" etc, you must use @LIBDIR@.

inherit config multilib tests

# find_implems $iface $libdir
# find all possible implems for $libdir
find_implems() {
    local -a implems
    iface=$1 libdir=$2
    for file in ${ROOT}@sysconfdir@/env.d/${iface}/${libdir}/* ; do
        [[ -f ${file} ]] || continue
        [[ "${file##*/}" != "config" ]] || continue
        implems=(${implems[@]} "${file##*/}")
    done
    echo ${implems[@]}
}

# is_active $iface $libdir $implem
# returns true if $implem is currently used for the $iface/$libdir combination
is_active() {
    [[ ${#@} -eq 3 ]] || die "Need exactly 3 arguments!"
    current=$(load_config ${ROOT}@sysconfdir@/env.d/${1}/${2}/config CURRENT)
    [[ ${current} == ${3} ]]
}

# switch_implem $iface $libdir $implem
# switches $iface/$libdir combination to $implem
switch_implem() {
    # set us up
    [[ ${#@} -eq 3 ]] || die "Need exactly 3 arguments!"
    local iface=${1}
    local libdir=${2}
    local implem=${3##*/}
    local implem_file=${ROOT}@sysconfdir@/env.d/${iface}/${libdir}/${implem}
    local current=$(load_config \
        "${ROOT}@sysconfdir@/env.d/${iface}/${libdir}/config" CURRENT)
    local current_file=${ROOT}@sysconfdir@/env.d/${iface}/${libdir}/${current}
    local dest src

    # Get rid of old symlinks, if we have a current config
    if [[ -f "${current_file}" ]]; then
        while read line; do
            # Skip comments
            [[ "${line}" = *#* ]] && continue

            line=${line//@LIBDIR@/${libdir}}

            set ${line}
            dest=$2
            rm -f ${ROOT}${dest}
        done < ${current_file}
    fi

    # Set up new symlinks
    while read line; do
        # Skip comments
        [[ "${line}" = *#* ]] && continue

        line=${line//@LIBDIR@/${libdir}}

        set ${line}
        src=$1
        dest=$2
        ln -sf ${src} ${ROOT}${dest}
    done < ${implem_file}

    store_config \
        "${ROOT}@sysconfdir@/env.d/${iface}/${libdir}/config" \
        CURRENT ${implem}
}

# iface_do_list $libdir
# Lists the available implementations for $libdir
iface_do_list() {
    local -a implems
    local active libdir=$1 iface=$IFACE
    implems=( $(find_implems $iface $libdir ) )

    # None installed for $libdir
    [[ -z ${implems[@]} ]] \
        && return

    write_list_start "Installed $MODULE for library directory $(highlight ${libdir})"
    for implem in ${implems[@]} ; do
        (( i++ ))
        active=''
        is_active ${iface} ${libdir} ${implem##*/} \
            && active=' *'

        write_numbered_list_entry $i "${implem}$(highlight "${active}")"
    done
}

# iface_do_show $libdir
# Shows the current implementation for $libdir
iface_do_show() {
    local iface=$IFACE libdir=$1 implem
    config=${ROOT}@sysconfdir@/env.d/${iface}/${libdir}/config
    [[ ${#config[@]} -eq 1 ]] \
        || return
    [[ -e ${config} ]] \
        || return

    implem=$(load_config \
        "${ROOT}@sysconfdir@/env.d/${iface}/${libdir}/config" CURRENT)
    [[ -e ${ROOT}@sysconfdir@/env.d/${iface}/${libdir}/${implem} ]] \
        || die "File \"${ROOT}@sysconfdir@/env.d/${iface}/${libdir}/${implem}\" is missing!"

    echo "${libdir}: ${implem}"
}

# get_libdirs
# Wraps list_libdirs() to ensure that output is sorted consistently
get_libdirs() {
    list_libdirs | sort
}

### list action

describe_list() {
    echo "List all installed $MODULE implementations"
}

do_list() {
    local libdir
    # Count for listing IFACE/libdir combinations
    # We keep it here so it doesn't reset on every call to iface_do_list()
    local i=0

    for libdir in $(get_libdirs); do
        [[ -d ${ROOT}@prefix@/${libdir} ]] \
            && [[ ! -h ${ROOT}@prefix@/${libdir} ]] \
            || continue
        iface_do_list $libdir
    done
}

### set action

describe_set() {
    echo "Activate one of the installed $MODULE implementations"
}

describe_set_parameters() {
    echo "<implementation>"
}

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

do_set() {
    [[ ${#@} == 0 ]] \
        && die -q "Please specify exactly 1 implementation!"
    local fail=0 iface=$IFACE
    local libdirs=$(get_libdirs)
    local libdir implem libdir_ct i=0
    local -a file implems new_implems mylibdirs myimplems

    # Build up list of all valid implems
    for libdir in ${libdirs}; do
        new_implems=( $(find_implems ${iface} ${libdir}) )
        implems=( ${implems[@]} ${new_implems[@]} )
        libdir_ct[$i]=${#new_implems[@]}
        (( i++ ))
    done

    # Parse passed parameters into valid libdirs. Other arguments are considered
    # implementations (or numbers for them) and are validated later.
    # If libdirs are specified, then switch for them. Otherwise, switch for all
    # libdirs.
    for param in ${@} ; do
        if has ${param} ${libdirs} ; then
            mylibdirs=(${mylibdirs[@]} ${param})
        else
            myimplems=(${myimplems[@]} ${param})
        fi
    done
    set ${myimplems[@]}

    # We can only change one implem at a time
    [[ ${#myimplems[@]} -ne 1 ]] && \
        die -q "Please specify exactly 1 implemention."

    [[ -n ${mylibdirs[@]} ]] && libdirs=${mylibdirs[@]}

    i=0
    for libdir in ${libdirs}; do
        # Only move on if we actually have implems here, otherwise we screw up
        # $libdir_max and waste time executing pointless code
        if [[ ${libdir_ct[$i]} -gt 0 ]]; then
            for item in ${@} ; do
                if is_number ${item} ; then
                    # On the first libdir, minimum must be 1. Maxes and later
                    # mins are incremented by the size of the previous libdir_ct
                    if [[ -n ${libdir_min} ]]; then
                        libdir_min=$(( ${libdir_min} + ${libdir_ct[$(( $i - 1 ))]} ))
                    else
                        libdir_min="1"
                    fi
                    libdir_max=$(( ${libdir_min} + ${libdir_ct[$i]} - 1 ))
                    if [[ ${item} -ge ${libdir_min} ]] && [[ ${item} -le ${libdir_max} ]] ; then
                        if ! switch_implem ${iface} ${libdir} ${implems[$(( ${item} -1 ))]}; then
                            fail=1
                            echo "Failed to switch to implementation \"${item}\" for library directory \"${libdir}\"!"
                            continue
                        fi
                    else
                        fail=1
                        echo "Item not in range ${libdir_min}-${libdir_max} for ${libdir}: ${item}"
                        continue
                    fi
                else
                    # Only attempt to switch to an implementation if it's available
                    # for that libdir
                    if has ${item} $(find_implems ${iface} ${libdir}); then
                        if ! switch_implem ${iface} ${libdir} ${item}; then
                            fail=1
                            echo "Failed to switch to implementation \"${item}\" for library directory \"${libdir}\"!"
                            continue
                        fi
                    fi
                fi
            done
        fi
        (( i++ ))
    done

    [[ ${fail} == 1 ]] && die -q "One or more actions have failed!"
}

### show action

describe_show() {
    echo "Print the currently active $MODULE implementation"
}

do_show() {
    local libdir
    local libdirs=$(get_libdirs)
    for param in ${@} ; do
        if has ${param} ${libdirs} ; then
            mylibdirs=(${mylibdirs[@]} ${param})
        fi
    done
    [[ -n ${mylibdirs[@]} ]] && libdirs=${mylibdirs[@]}

    for libdir in ${libdirs}; do
        [[ -d ${ROOT}@prefix@/${libdir} ]] \
            && [[ ! -h ${ROOT}@prefix@/${libdir} ]] \
            || continue
        iface_do_show $libdir
    done
}

### add action

describe_add() {
    echo "Add a new $MODULE implementation"
}

describe_add_parameters() {
    echo "<libdir> <file> <implementation>"
}

describe_add_options() {
    echo "libdir : library directory where $MODULE implementation is installed (lib, lib64, etc.)"
    echo "file : path to file containing symlink map"
    echo "implementation : name of the $MODULE implementation"
}

do_add() {
    [[ ${#@} -ne 3 ]] \
        && die -q "Please specify 1 library directory, 1 file to install and 1 implementation!"

    # If $D is set, we're adding from portage so we want to respect sandbox.
    # Otherwise, respect the ROOT variable.
    local PREFIX=${D:-${ROOT}}

    # Create directory if necessary
    if [[ ! -e ${PREFIX}@sysconfdir@/env.d/${IFACE}/${1} ]]; then
        mkdir -p "${PREFIX}@sysconfdir@/env.d/${IFACE}/${1}"
    else
        if [[ ! -d ${PREFIX}@sysconfdir@/env.d/${IFACE}/${1} ]]; then
            die -q "${PREFIX}@sysconfdir@/env.d/${IFACE}/${1} exists but isn't a directory!"
        fi
    fi

    if ! cp ${2} "${PREFIX}@sysconfdir@/env.d/${IFACE}/${1}/${3}"; then
        die -q "Installing ${2} as ${PREFIX}@sysconfdir@/env.d/${IFACE}/${1}/${3} failed!"
    fi
}

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

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