#!/usr/bin/env sh

### Script: parenturipath
##
## 上位パスの URI を表示する。
##
## Metadata:
##
##   id - c02c4757-c17f-40ab-8d17-09de961aeeea
##   author - <qq542vev at https://purl.org/meta/me/>
##   version - 1.3.3
##   date - 2022-06-28
##   since - 2019-05-26
##   copyright - Copyright (C) 2019-2022 qq542vev. Some rights reserved.
##   license - <CC-BY at https://creativecommons.org/licenses/by/4.0/>
##   package - w3mplus
##
## See Also:
##
##   * <Project homepage at https://github.com/qq542vev/w3mplus>
##   * <Bug report at https://github.com/qq542vev/w3mplus/issues>
##
## Help Output:
##
## ------ Text ------
## Usage:
##   parenturipath [OPTION]... [URI]...
##
## Options:
##   -n,     --number UNSIGNED_INTEGER 
##                               指定した回数上昇する
##   -h,     --help              このヘルプを表示して終了する
##   -v,     --version           バージョン情報を表示して終了する
##
## Exit Status:
##     0 - successful termination
##    64 - command line usage error
##    65 - data format error
##    66 - cannot open input
##    67 - addressee unknown
##    68 - host name unknown
##    69 - service unavailable
##    70 - internal software error
##    71 - system error (e.g., can't fork)
##    72 - critical OS file missing
##    73 - can't create (user) output file
##    74 - input/output error
##    75 - temp failure; user is invited to retry
##    76 - remote error in protocol
##    77 - permission denied
##    78 - configuration error
##   129 - received SIGHUP
##   130 - received SIGINT
##   131 - received SIGQUIT
##   143 - received SIGTERM
## ------------------

readonly 'VERSION=parenturipath 1.3.3'

. 'initialize.sh'
. 'option_error.sh'
. 'regex_match.sh'

# @getoptions
parser_definition() {
	setup REST abbr:true error:option_error plus:true no:0 help:usage \
		-- 'Usage:' "  ${2##*/} [OPTION]... [URI]..." \
		'' 'Options:'

	param number  -n --number  init:='1' validate:'regex_match "${OPTARG}" "^(0|[1-9][0-9]*)$"' var:UNSIGNED_INTEGER -- '指定した回数上昇する'
	disp  :usage  -h --help    -- 'このヘルプを表示して終了する'
	disp  VERSION -v --version -- 'バージョン情報を表示して終了する'

	msg -- '' 'Exit Status:' \
		'    0 - successful termination' \
		'   64 - command line usage error' \
		'   65 - data format error' \
		'   66 - cannot open input' \
		'   67 - addressee unknown' \
		'   68 - host name unknown' \
		'   69 - service unavailable' \
		'   70 - internal software error' \
		"   71 - system error (e.g., can't fork)" \
		'   72 - critical OS file missing' \
		"   73 - can't create (user) output file" \
		'   74 - input/output error' \
		'   75 - temp failure; user is invited to retry' \
		'   76 - remote error in protocol' \
		'   77 - permission denied' \
		'   78 - configuration error' \
		'  129 - received SIGHUP' \
		'  130 - received SIGINT' \
		'  131 - received SIGQUIT' \
		'  143 - received SIGTERM'
}
# @end

# @gengetoptions parser -i parser_definition parse "${1}"
# @end

eval "$(getoptions parser_definition parse "${0}")"
parse ${@+"${@}"}
eval "set -- ${REST}"

case "${#}" in '0')
	set -- $(cat)
esac

awkScript=$(
	cat <<-'__EOF__'
	@include "uri_path_parent.awk"
	@include "url_encode_normalize.awk"
	@include "uri_check.awk"
	@include "uri_parse.awk"
	@include "sysexits.awk"

	BEGIN {
		exitStatus = 0

		for(i = 1; i < ARGC; i++) {
			uri = ARGV[i]

			if(uri_check(uri)) {
				uri_parse(url_encode_normalize(uri), element)
				element["path"] = uri_path_parent(element["path"], number)

				printf("%s\n", element["scheme"] element["authority"] element["path"])
			} else {
				printf("not URI: %s\n", uri) | "cat >&2"

				exitStatus = sysexits("EX_USAGE")
			}
		}

		exit exitStatus
	}
	__EOF__
)

awk \
	-v "number=${number}" \
	-- "${awkScript}" \
	${@+"${@}"} \
|| end_call "${?}"
