#!/usr/bin/env bash
# Copyright (C) 2013-2014, 2017-2018, 2024 Luke T. 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

export TEXTDOMAIN='librelib'

cmd=${0##*/}
usage() {
	print 'Usage: . $(%s LIBRARY)' "$cmd"
	print '   or: %s {-h|--help}' "$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, --help' 'Show this message'
}

main() {
	if [[ -z $LIBRETOOLS_LIBRARY_PATH ]]; then
		export LIBRETOOLS_LIBRARY_PATH=$default_libdir
	fi

	. "$(librelib messages)"

	local args mode=run
	if ! args="$(getopt -n "$cmd" -o 'h' -l 'help' -- "$@")"; then
		mode=errusage
	else
		eval "set -- $args"
		local flag
		while true; do
			flag=$1
			shift
			case "$flag" in
				-h | --help) mode=usage ;;
				--) break ;;
				*) panic 'unhandled flag: %q' "$flag" ;;
			esac

		done
		if [[ $mode == run && $# != 1 ]]; then
			gnuerror 'requires a library name'
			mode=errusage
		fi
	fi
	case "$mode" in
		errusage)
			print "Try '%s --help' for more information." "${0##*/}" >&2
			return $EXIT_INVALIDARGUMENT
			;;
		usage)
			usage
			return $EXIT_SUCCESS
			;;
		run) : ;;
	esac

	lib=$1
	lib=${lib#libre}
	lib=${lib%.sh}

	librelib "$lib"
}

librelib() {
	# This function MUST NOT use any external library routines or
	# variables.

	local lib="$1"

	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

	if type gettext &>/dev/null; then
		_() { gettext "$@"; }
	else
		_() { echo -n "$@"; }
	fi
	local mesg
	mesg="$(_ '%s: could not find library: %s')"
	printf "$mesg\n" "$cmd" "$lib" >&2
	return 1 # $EXIT_FAILURE
}

main "$@"
