#!/bin/sh -efu
# This file is covered by the GNU General Public License,
# which should be included with libshell as the file LICENSE.
# All copyright information are listed in the COPYING.

if [ -z "${__included_shell_args-}" ]; then
__included_shell_args=1

. shell-error

# Checks that given option value is a readable file.
# Arguments: $1 is option name, $2 is option value.
# If $2 is a readable file, outputs canonicalized file name,
# otherwise fails.
opt_check_read() {
	local value
	[ -r "$2" ] &&
	value="$(readlink -ev "$2")" ||
		fatal "$1: $2: file not available."
	printf %s "$value"
}

# Checks that given option value is a traversable directory.
# Arguments: $1 is option name, $2 is option value.
# If $2 is a readable file, outputs canonicalized directory name,
# otherwise fails.
opt_check_dir() {
	local value
	[ -d "$2" -a -x "$2" ] &&
	value="$(readlink -ev "$2")" ||
		fatal "$1: $2: directory not available."
	printf %s "$value"
}

# Checks that given option value is a positive decimal number.
# Arguments: $1 is option name, $2 is option value.
# If $2 is a positive decimal number, outputs it, otherwise fails.
opt_check_number()
{
	[ -n "${2##0*}" -a -n "${2##*[!0-9]*}" ] &&
	[ "$2" -gt 0 ] 2>/dev/null ||
		fatal "$1: $2: invalid number."
	printf %s "$2"
}

show_usage() {
	[ -z "$*" ] || message "$*"
	echo "Try \`$PROG --help' for more information." >&2
	exit 1
}

show_help() {
	fatal "show_help(): Undefined function"
}

print_version() {
	fatal "print_version() Undefined function"
}

readonly getopt_common_opts="q,v,V,h"
readonly getopt_common_longopts="quiet,verbose,version,help"
parse_common_option() {
	case "$1" in
		-h|--help) show_help
			;;
		-q|--quiet) quiet=-q; verbose=
			;;
		-v|--verbose) verbose=-v; quiet=
			;;
		-V|--version) print_version "$PROG"
			;;
		*) fatal "Unrecognized option: $1"
			;;
	esac
}

fi #__included_shell_args
