-
Notifications
You must be signed in to change notification settings - Fork 0
/
rmscreen.sh
244 lines (191 loc) · 4.62 KB
/
rmscreen.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#!/usr/bin/env bash
##
## rmscreen - remove last screenshot accidentally taken
## Copyright (C) 2020-2023 Daniel Haase
##
## This program is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program. If not, see <http://www.gnu.org/licenses/gpl.txt>.
##
# shellcheck disable=SC2012,SC2155,SC2310
set -o errexit
set -o nounset
set -o pipefail
NAME="rmscreen"
VERSION="0.2.0"
SCREENSHOT_DIRECTORY="${SCREENSHOT_DIRECTORY:-""}"
function check_command {
if ! command -v "${1}" &>/dev/null; then
echo >&2 "no such command \"${1}\""
exit 1
fi
}
function print_version {
cat <<-EOF
${NAME} ${VERSION}
copyright (c) 2020-2023 Daniel Haase
EOF
}
function print_usage {
print_version
cat <<-EOF
usage: ${NAME} [--force | --version | --help]
-f | --force
delete screenshot even if not from today
-V | --version
print version information and exit
-h | --help
print this usage description and exit
EOF
}
function find_subdirectory_by_regex {
local -r parent_directory="${1}"
local -r regex="${1}"
if [[ ! -d "${parent_directory}" || -z "${regex}" ]]; then
return 1
fi
for directory in "${parent_directory}"/*; do
if [[ ! -d "${directory}" ]]; then
continue
fi
if [[ "${directory,,}" =~ ${regex} ]]; then
echo "${directory}"
return 0
fi
done
return 1
}
function find_picture_directory {
local directory="$(xdg-user-dir PICTURES)"
if [[ -d "${directory}" ]]; then
echo "${directory}"
return 0
fi
if [[ ! -d "${HOME}" ]]; then
return 1
fi
local -r regex=".*(pic|image).*"
find_subdirectory_by_regex "${directory}" "${regex}"
}
function find_screenshot_directory {
if [[ -n "${SCREENSHOT_DIRECTORY}" &&
-d "${SCREENSHOT_DIRECTORY}" ]]; then
echo "${SCREENSHOT_DIRECTORY}"
return 0
fi
local picture_directory="$(find_picture_directory)"
local directory
if [[ ! -d "${picture_directory}" ]]; then
return 1
fi
find_subdirectory_by_regex "${picture_directory}" ".*screen.*"
}
function find_latest_screenshot {
local -r directory="${1}"
if [[ ! -d "${directory}" ]]; then
return 1
fi
ls --almost-all --sort=time "${1}" | head --lines=1
} 2>/dev/null
function get_file_birth {
local -r filepath="${1}"
if [[ ! -f "${filepath}" ]]; then
return 1
fi
local birth="$(stat --format=\"%w\" "${filepath}" |
cut --delimiter=\" \" --fields=1)"
if [[ "${birth}" == "-" ]]; then
birth="$(stat --format=\"%y\" "${filepath}" |
cut --delimiter=\" \" --fields=1)"
fi
echo "${birth}"
}
function is_today {
[[ "${1}" == "$(date +%Y-%m-%d)" ]]
}
function confirm_deletion {
local prompt="delete \"${1}\"?"
local choice
read -r -n 1 -s -p "${prompt} (Y/n) " choice
if [[ "${choice,,}" == "n"* ]]; then
echo >&2 "n"
return 1
else
echo >&2 "y"
return 0
fi
}
check_command "cat"
check_command "cut"
check_command "date"
check_command "head"
check_command "ls"
check_command "rm"
check_command "stat"
check_command "xdg-user-dir"
declare force=0
if [[ $# -eq 1 ]]; then
case "${1}" in
-f | --force)
force=1
;;
-V | --version)
print_version
exit 0
;;
-h | --help)
print_usage
exit 0
;;
*)
print_usage
exit 2
;;
esac
fi
declare -r directory="$(find_screenshot_directory)"
if [[ ! -d "${directory}" ]]; then
echo >&2 "failed to locate screenshot directory"
exit 3
fi
declare -r screenshot="$(find_latest_screenshot "${directory}")"
if [[ -z "${screenshot}" ]]; then
echo "no screenshots found"
exit 0
fi
if [[ ! -f "${directory}/${screenshot}" ]]; then
echo >&2 "failed to locate most recent screenshot"
exit 3
fi
if [[ "${force}" -lt 1 ]]; then
declare file_birth="$(get_file_birth "${screenshot}")"
if ! is_today "${file_birth}"; then
echo "most recent screenshot is not from today"
exit 0
fi
fi
if ! confirm_deletion "${directory}/${screenshot}"; then
exit 0
fi
if [[ ! -w "${directory}" || ! -x "${directory}" ]]; then
echo >&2 "insufficient directory permissions to delete screenshot"
exit 3
fi
if ! rm --force "${directory}/${screenshot}" 2>/dev/null; then
echo >&2 "operation failed"
exit 4
fi
cat <<-EOF
successfully removed screenshot
"${directory}/${screenshot}"
EOF
exit 0