summaryrefslogtreecommitdiff
path: root/src/lib/librelib
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/librelib')
-rwxr-xr-xsrc/lib/librelib79
1 files changed, 79 insertions, 0 deletions
diff --git a/src/lib/librelib b/src/lib/librelib
new file mode 100755
index 0000000..2dc9314
--- /dev/null
+++ b/src/lib/librelib
@@ -0,0 +1,79 @@
+#!/usr/bin/env bash
+# Copyright (c) 2013 by Luke Shumaker <lukeshu@sbcglobal.net>
+#
+# 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 3 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() { echo "$@"; }
+fi
+
+print() {
+ mesg=$1
+ shift
+ printf -- "$(gettext "$mesg")\n" "$@"
+}
+
+prose() {
+ print "$@" | fmt -su
+}
+
+cmd=${0##*/}
+usage() {
+ . libremessages
+ print 'Usage: . $(%s LIBRARY)' "$cmd"
+ print "Finds a shell 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_LIBDIR.' "$default_libdir"
+ echo
+ print "Example usage:"
+ printf ' . $(%s conf.sh)\n' "$cmd"
+}
+
+main() {
+ if [[ $# != 1 ]]; then
+ usage >&2
+ return 2
+ fi
+ if [[ $1 == '-h' ]]; then
+ usage
+ return 0;
+ fi
+
+ if [[ -z $LIBRETOOLS_LIBDIR ]]; then
+ export LIBRETOOLS_LIBDIR=$default_libdir
+ fi
+
+ lib=$1
+ lib=${lib#libre}
+ lib=${lib%.sh}
+
+ for file in ${lib} libre${lib} ${lib}.sh libre${lib}.sh; do
+ if [[ -f "$LIBRETOOLS_LIBDIR/$file" ]]; then
+ printf '%s\n' "$LIBRETOOLS_LIBDIR/$file"
+ return 0;
+ fi
+ done
+ print '%s: could not find library: %s' "$cmd" "$lib" >> /dev/stderr
+ return 1
+}
+
+main "$@"