#!/usr/bin/env sh

### Script: w3m-cleanup
##
## w3m の設定ディレクトリ内を掃除する。
##
## w3m の設定ディレクトリ内のキャッシュファイル、テンポラリファイルを削除する。
## このコマンドは w3m 実行中に実行してはならない。
##
## Metadata:
##
##   id - 03df8113-372a-4870-a2ac-f45040498f5b
##   author - <qq542vev at https://purl.org/meta/me/>
##   version - 1.1.6
##   date - 2022-06-28
##   since - 2019-07-23
##   copyright - Copyright (C) 2019-2020 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-cleanup [OPTION]... [CONFIG_DIRECTORY]...
##
## Options:
##   -c,     --{no-}cookie       Cookie を削除する
##   -H,     --{no-}history      閲覧履歴を削除をする
##   -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-cleanup 1.1.6'

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

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

	flag cookieFlag  -c --{no-}cookie  init:@no -- 'Cookie を削除する'
	flag historyFlag -H --{no-}history init:@no -- '閲覧履歴を削除をする'
	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 -- "${HOME}/.w3m"
esac

for directory in ${@+"${@}"}; do
	find \
		-- "${directory}" \
		'(' \
			-name 'w3mcache*' \
			-o -name 'w3mcookie*' \
			-o -name 'w3mframe*' \
			-o -name 'w3msrc*' \
			-o -name 'w3mtmp*' \
		')' \
		-prune -exec 'rm' '-fr' '--' '{}' '+'

	case "${cookieFlag}" in '1')
		rm -fr -- "${directory}/cookie"
	esac

	case "${historyFlag}" in '1')
		rm -fr -- "${directory}/history" "${directory}/request.log"
	esac
done
