-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcleanup
More file actions
executable file
·261 lines (230 loc) · 6.25 KB
/
cleanup
File metadata and controls
executable file
·261 lines (230 loc) · 6.25 KB
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#!/bin/bash
# SPDX-FileCopyrightText: 2023 - 2024 sudorook <daemon@nullcodon.com>
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
# 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 <https://www.gnu.org/licenses/>.
set -euo pipefail
ROOT="$(dirname "${0}")"
source "${ROOT}"/globals
! check_command cmp jq sed && exit 3
#
# Functions
#
function delete_directory() {
local dir="${1}"
local dir_owner
if [ -d "${dir}" ]; then
dir_owner="$(stat -c "%U" "${dir}")"
if [[ "${USER}" = "${dir_owner}" ]]; then
if [[ -v 2 ]]; then
show_info "${2}"
fi
rm -rf "${dir}"
sync
else
show_warning "${dir@Q} owned by ${dir_owner@Q}. Skipping."
fi
fi
}
function delete_file() {
local file="${1}"
local file_owner
if [ -f "${file}" ]; then
file_owner="$(stat -c "%U" "${file}")"
if [[ "${USER}" = "${file_owner}" ]]; then
if [[ -v 2 ]]; then
show_info "${2}"
fi
rm -f "${file}"
sync
else
show_warning "${file@Q} owned by ${file_owner@Q}. Skipping."
fi
fi
}
function delete_config_section() {
local file="${1}"
local file_owner
local header="${2}"
local tmp
if [ -f "${file}" ]; then
file_owner="$(stat -c "%U" "${file}")"
if [[ "${USER}" = "${file_owner}" ]]; then
tmp="$(mktemp)"
sed "/\[${header}\]/,/^\s*$/{d}" "${file}" | sed '${/^$/d}' > "${tmp}"
if ! cmp -s "${tmp}" "${file}"; then
if [[ -v 3 ]]; then
show_info "${3}"
fi
mv "${tmp}" "${file}"
else
rm "${tmp}"
fi
sync
else
show_warning "${file@Q} owned by ${file_owner@Q}. Skipping."
fi
fi
}
function delete_json_section() {
local file="${1}"
local file_owner
local path="${2}"
local tmp
if [ -f "${file}" ]; then
file_owner="$(stat -c "%U" "${file}")"
if [[ "${USER}" = "${file_owner}" ]]; then
tmp="$(mktemp)"
# Check if file end with newline. If it does not, then use '-j'.
if [ "$(tail -n 1 "${file}" | wc -l)" -eq 0 ]; then
jq -j "${path} = []" "${file}" > "${tmp}"
else
jq "${path} = []" "${file}" > "${tmp}"
fi
if ! cmp -s "${tmp}" "${file}"; then
if [[ -v 3 ]]; then
show_info "${3}"
fi
mv "${tmp}" "${file}"
else
rm "${tmp}"
fi
sync
else
show_warning "${file@Q} owned by ${file_owner@Q}. Skipping."
fi
fi
}
#
# Main
#
# KDE Clipboard
if check_command qdbus 2> /dev/null; then
if qdbus > /dev/null 2>&1; then
show_info "Clearing Klipper history."
qdbus org.kde.klipper /klipper org.kde.klipper.klipper.clearClipboardHistory
fi
elif check_command qdbus6 2> /dev/null; then
if qdbus6 > /dev/null 2>&1; then
show_info "Clearing Klipper history."
qdbus6 org.kde.klipper /klipper org.kde.klipper.klipper.clearClipboardHistory
fi
fi
# Nvim state
delete_directory \
"${HOME}/.local/state/nvim" \
"Clearing Neovim state."
# Vim undo
delete_directory \
"${HOME}/.vim/undo" \
"Clearing Vim undo history."
# Recent files, etc.
if check_command sweeper 2> /dev/null; then
if [ -v DESKTOP_SESSION ]; then
show_info "Running sweeper."
sweeper --automatic 2> /dev/null
fi
fi
# System-wide cleanup
if check_command bleachbit 2> /dev/null; then
if [ -f "${HOME}/.config/bleachbit/bleachbit.ini" ]; then
show_info "Running BleachBit (${USER})."
bleachbit -c --preset
else
show_warning "BleachBit not configured. Skipping."
fi
fi
# Anki backups
if [ -d "${HOME}/.local/share/Anki2" ]; then
while read -r DIR; do
delete_directory \
"${DIR}" \
"Clearing Anki ${DIR@Q}."
done < <(find "${HOME}/.local/share/Anki2" -type d \( -name backups -o -name media.trash \))
fi
# Ark history
if [ -f "${HOME}"/.local/share/ark/ark_recentfiles ]; then
TMP='[RecentFiles]
files\size=0'
if ! cmp -s "${HOME}"/.local/share/ark/ark_recentfiles <(echo "${TMP}"); then
show_info "Clearing Ark history."
echo "${TMP}" > "${HOME}"/.local/share/ark/ark_recentfiles
fi
fi
# CMake caches.
delete_directory \
"${HOME}/.cmake" \
"Deleting CMake cache."
# KFileDialog history
delete_config_section \
"${HOME}/.config/xdg-desktop-portal-kderc" \
"KFileDialog Settings" \
"Clearing KFileDialog history."
# Okular recent files.
delete_directory \
"${HOME}/.local/share/okular/docdata" \
"Deleting Okular document data."
delete_config_section \
"${HOME}/.config/okularrc" \
"Recent Files" \
"Clearing Okular recent file history."
# Gwenview image viewer history.
delete_directory \
"${HOME}/.local/share/gwenview/recentfolders" \
"Deleting Gwenview folder history."
delete_config_section \
"${HOME}/.config/gwenviewrc" \
"Recent Files" \
"Clearing Gwenview recent file history."
# Calibre
delete_directory \
"${HOME}/Documents/Library/.caltrash/" \
"Emptying Calibre trash."
delete_json_section \
"${HOME}/.config/calibre/viewer-webengine.json" \
".session_data.standalone_recently_opened" \
"Clearing ebook-viewer history."
# Remove trash directory
delete_directory \
"${HOME}/.local/share/Trash/" \
"Deleting trash directory."
# Trash at Share/ mountpoint
delete_directory \
"${HOME}/Share/.Trash-1000" \
"Deleting ~/Share trash directory."
# Python history
delete_file \
"${HOME}/.python_history" \
"Deleting Python REPL history."
# Node REPL history
delete_file \
"${HOME}"/.node_repl_history \
"Deleting node REPL history."
# mpv watch_later
delete_directory \
"${HOME}/.local/state/mpv/watch_later" \
"Clearing mpv history."
# ollama history
delete_file \
"${HOME}/.ollama/history" \
"Clearing ollama history"
# less history
delete_file \
"${HOME}/.local/state/lesshst" \
"Deleting less history."
# wget history
delete_file \
"${HOME}/.wget-hsts" \
"Deleting wget history."