#!/usr/bin/env bash
# Copyright (C) 2012-2014, 2016, 2024 Luke T. Shumaker <lukeshu@parabola.nu>
#
# 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/>.

if [[ ${0##*/} != libremessages ]]; then
	. "$(librelib messages)"
else
	set -euE

	export TEXTDOMAIN='librelib'

	. "$(librelib messages)"

	funcs=(
		# general routines
		_                   # devtools common.sh
		in_array            # libmakepkg
		whitespace_collapse # librelib messages.sh

		# devtools messages
		# info
		plain     # devtools common.sh -> libmakepkg
		msg       # devtools common.sh -> libmakepkg
		msg2      # devtools common.sh -> libmakepkg
		stat_busy # devtools common.sh
		stat_done # devtools common.sh
		# essential
		ask # devtools common.sh -> libmakepkg
		# error
		warning  # devtools common.sh -> libmakepkg
		error    # devtools common.sh -> libmakepkg
		plainerr # devtools common.sh -> libmakepkg

		# libretools messages
		prose      # librelib messages.sh
		bullet     # librelib messages.sh
		flag       # librelib messages.sh
		print      # librelib messages.sh
		term_title # librelib messages.sh
		gnuerror   # librelib messages.sh
		panic      # librelib messages.sh

		# traps
		#setup_traps            # librelib messages.sh
		#setup_workdir          # devtools common.sh
		#cleanup                # devtools common.sh
		#trap_abort             # devtools common.sh
		#trap_exit              # devtools common.sh
		#abort                  # devtools common.sh
		#die                    # devtools common.sh

		# locks
		#lock                   # devtools common.sh
		#slock                  # devtools common.sh
		#lock_close             # devtools common.sh

		# makepkg-related
		pkgver_equal           # devtools common.sh
		find_cached_package    # devtools common.sh
		find_cached_srcpackage # devtools common.sh
	)

	usage() {
		print "Usage: %s COMMAND [ARGUMENTS]" "${0##*/}"
		print '   or: %s {-h|--help}' "${0##*/}"
		print "Tool for calling libretools library utilities."
		echo
		prose "See the messages.sh(3) man page for more information."
		echo
		print "Commands:"
		for func in "${funcs[@]}"; do
			bullet "$func"
		done
		echo
		print 'Options:'
		flag \
			'-h, --help' 'Show this message'
	}

	main() {
		local args mode=run
		if ! args="$(getopt -n "${0##*/}" -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 && $# -eq 0 ]]; then
				gnuerror 'requires a command'
				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) "$@" ;;
			*) panic 'invalid mode: %q' "$mode" ;;
		esac
	}

	main "$@"
fi
