#!/usr/bin/env sh

### Script: w3m-private
##
## プライベートモードの w3m を実行する。
##
## キャッシュファイル、テンポラリファイル、 Cookie 、閲覧履歴は w3m 終了後に削除される。
##
## Metadata:
##
##   id - 2d64a911-2e62-4593-8c90-6c904aa6bf6e
##   author - <qq542vev at https://purl.org/meta/me/>
##   version - 2.0.2
##   date - 2022-12-10
##   since - 2019-07-29
##   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:
##   w3m-private [OPTION]... -- [W3M_ARG]
##
## Options:
##   -c,     --copy PATTERN      PATTERN に一致するファイルをコピーする
##           --no-copy           -c, --copy をリセットする
##   -l,     --link PATTERN      PATTERN に一致するファイルへのシンボリックリンクを作成する
##           --no-link           -l, --link をリセットする
##   -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=w3m-private 2.0.2'

. 'initialize.sh'
. 'option_error.sh'
. 'append_array_posix.sh'

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

	param :'append_array_posix copy "-o" "-path" "${OPTARG}"' -c --copy var:PATTERN -- 'PATTERN に一致するファイルをコピーする'
	flag  copy    --no-copy    init:@no on: no: -- '-c, --copy をリセットする'
	param :'append_array_posix link "-o" "-path" "${OPTARG}"' -l --link var:PATTERN -- 'PATTERN に一致するファイルへのシンボリックリンクを作成する'
	flag  link    --no-link    init:@no on: no: -- '-l, --link をリセットする'
	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}"

tmpDir=$(mktemp -d)
mkdir -p -- "${tmpDir}/.w3m"
shellScript=$(
	cat <<-'__EOF__'
	type="${1}"
	dest="${2}"
	shift 2

	for item in ${@+"${@}"}; do
		dir=$(dirname -- "${item}"; printf '_')
		dir="${dir%?_}"

		mkdir -p -- "${dest}/${dir}"

		case "${type}" in
			'copy') cp -fpR "${item}" "${dest}/${dir}";;
			'link') ln -fs "${PWD}/${item}" "${dest}/${dir}";;
		esac
	done
	__EOF__
)

(
	cd -- "${HOME}/.w3m"

	find -- '.' \
		'!' '(' \
			-name 'bookmark.html' \
			-o -name 'cookie' \
			-o -name 'history' \
			-o -name 'passwd' \
			-o -name 'pre_form' \
			-o -name 'request.log' \
			-o -name 'w3mcache*' \
			-o -name 'w3mcookie*' \
			-o -name 'w3mframe*' \
			-o -name 'w3msrc*' \
			-o -name 'w3mtmp*' \
		')' \
		-path './*' -prune \
		-exec 'cp' '-fLpR' -- '{}' "${tmpDir}/.w3m" ';'

	cd -- "${HOME}"

	for type in 'copy' 'link'; do
		eval "oprands=\${${type}}"
		eval set -- "${oprands}"

		case "${#}" in [1-9]*)
			shift
			eval "find -L -- '.' '(' ${*} ')' -exec sh -c \"\${shellScript}\" 'sh' '${type}' \"${tmpDir}\" '{}' '+'"
		esac
	done
)

HOME="${tmpDir}" org_lc w3m ${@+"${@}"}
