blob: 27649f868cd47ddd51c36e1eb674f164423c01bc (
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
|
#!/usr/bin/env bash
# Copyright (C) 2013-2014, 2017-2018 Luke Shumaker <lukeshu@parabola.nu>
# Copyright (C) 2020 Andreas Grapentin <andreas@grapentin.org>
# License: GNU GPLv2+
#
# This program 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.
#
# This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
default_libdir=/usr/lib/libretools
if type gettext &>/dev/null; then
_() { gettext "$@"; }
else
_() { echo "$@"; }
fi
_l() {
TEXTDOMAIN='librelib' TEXTDOMAINDIR='/usr/share/locale' "$@"
}
print() {
local mesg; mesg="$(_ "$1")"; shift
printf -- "$mesg\n" "$@"
}
cmd=${0##*/}
usage() {
. libremessages # Don't change this to use librelib
print 'Usage: . $(%s LIBRARY)' "$cmd"
print 'Usage: %s -h' "$cmd"
print "Finds a Bash library file"
echo
prose "While some libraries can be sourced just by their name because
they are installed in PATH (like libremessages), some are not
installed there (like conf.sh), so a path must be given.
Hardcoding that path is the way of the dark side."
echo
prose 'By default, it looks for the files in `%s`, but this can be
changed with the environmental variable LIBRETOOLS_LIBRARY_PATH.' "$default_libdir"
echo
print "Example usage:"
printf ' . $(%s conf)\n' "$cmd"
echo
print "Options:"
flag '-h' 'Show this message'
}
main() {
if [[ $# != 1 ]]; then
_l usage >&2
return 2 # $EXIT_INVALIDARGUMENT
fi
if [[ $1 == '-h' ]]; then
_l usage
return 0 # $EXIT_SUCCESS
fi
if [[ -z $LIBRETOOLS_LIBRARY_PATH ]]; then
export LIBRETOOLS_LIBRARY_PATH=$default_libdir
fi
lib=$1
lib=${lib#libre}
lib=${lib%.sh}
local libdir_array
IFS=: read -r -a libdir_array <<<"$LIBRETOOLS_LIBRARY_PATH"
local libdir
for libdir in "${libdir_array[@]}"; do
for file in ${lib} libre${lib} ${lib}.sh libre${lib}.sh; do
if [[ -f "$libdir/$file" ]]; then
printf '%s\n' "$libdir/$file"
return 0 # $EXIT_SUCCESS
fi
done
done
_l print '%s: could not find library: %s' "$cmd" "$lib" >&2
return 1 # $EXIT_FAILURE
}
main "$@"
|