#!/usr/bin/env sh

### Script: httpresponse
##
## HTTP レスポンスメッセージを作成する。
##
## Metadata:
##
##   id - c1098c4b-f92d-40a2-bf99-dcfae2c9bed8
##   author - <qq542vev at https://purl.org/meta/me/>
##   version - 2.1.1
##   date - 2022-12-10
##   since - 2019-07-15
##   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:
##   httpresponse [OPTION]... [FILE]...
##
## Options:
##   -H,     --header HEADER_FIELD 
##                               HTTP ヘッダフィールドを追加する
##           --no-header         -H, --header をリセットする
##   -s,     --status-line STATUS_LINE 
##                               HTTP のステータスラインを指定する
##   -T,     --template FILE     テンプレートファイルを指定する
##   -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=httpresponse 2.1.1'

. 'initialize.sh'
. 'option_error.sh'
. 'control_character.sh'
. 'regex_match.sh'
. 'append_string.sh'

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

	param :'append_string "HEADER_FIELDS" "${OPTARG}${CHAR_CR}${CHAR_LF}"' -H --header-field validate:'regex_match "${OPTARG}" "^[!#-'"'"'*+.0-9A-Z/^_\\`a-z|~-]+:[\\\\t -~]+$"' var:HEADER_FIELD -- 'HTTP ヘッダフィールドを追加する'
	flag  HEADER_FIELDS --no-header-field init:@no on: no: -- '-H, --header-field をリセットする'
	param STATUS_LINE   -s --status-line init:='HTTP/1.1 200 OK' validate:'regex_match "${OPTARG}" "^HTTP/[0-9]\\.[0-9] [0-9]{3} [ -~]*$"' var:STATUS_LINE -- 'HTTP のステータスラインを指定する'
	param W3MPLUS_TEMPLATE_HTTP -T --template init:@none var:FILE -- 'テンプレートファイルを指定する'
	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'

	msg -- '' 'Environment Variables:' \
		'  W3MPLUS_HTTP_PROGRAM - HTTP レスポンス用の呼び出しコマンド'
}
# @end

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

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

export 'HEADER_FIELDS' 'STATUS_LINE' 'W3MPLUS_TEMPLATE_HTTP'

case "${W3MPLUS_TEMPLATE_HTTP-}" in
	'')
		cat <<-__EOF__
		${STATUS_LINE}${CHAR_CR}
		Date: $(date -u -- '+%a, %d %b %Y %H:%M:%S GMT')${CHAR_CR}
		${HEADER_FIELDS}${CHAR_CR}
		__EOF__

		case "${#}" in [!0]*)
			cat -- ${@+"${@}"}
		esac
		;;
	*) org_lc sh -c "${W3MPLUS_HTTP_PROGRAM} \${@+\"\${@}\"}" 'sh' ${@+"${@}"};;
esac
