-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathseek-and-destroy
More file actions
executable file
·194 lines (174 loc) · 5.54 KB
/
seek-and-destroy
File metadata and controls
executable file
·194 lines (174 loc) · 5.54 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
#!/bin/bash
# SPDX-FileCopyrightText: 2017 - 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 find && exit 3
#
# Global functions
#
function seek {
local inputstring=
local item
local cmd
case ${MODE} in
precise)
for item in "${@}"; do
inputstring="${inputstring:+${inputstring} -o }-iname \"${item}\" -o -iname \".${item}\""
done
;;
sloppy)
for item in "${@}"; do
inputstring="${inputstring:+${inputstring} -o }-iname \"*${item}*\" -o -iname \".*${item}*\""
done
;;
*)
show_error "ERROR: seeker mode ${MODE@Q} is incorrect."
exit 3
;;
esac
cmd="find ${HOME}/ \
\( -path \"${HOME}/Desktop\" \
-o -path \"${HOME}/Documents\" \
-o -path \"${HOME}/Downloads\" \
-o -path \"${musicdir:-${HOME}/Music}\" \
-o -path \"${HOME}/Pictures\" \
-o -path \"${projectdir:-${HOME}/Projects}\" \
-o -path \"${HOME}/Public\" \
-o -path \"${HOME}/Share\" \
-o -path \"${HOME}/Sync\" \
-o -path \"${HOME}/Templates\" \
-o -path \"${videodir:-${HOME}/Videos}\" \
-o -path \"${HOME}/.bundle\" \
-o -path \"${HOME}/.cargo\" \
-o -path \"${HOME}/.config/nvim\" \
-o -path \"${HOME}/.go\" \
-o -path \"${HOME}/.ipython\" \
-o -path \"${HOME}/.julia\" \
-o -path \"${HOME}/.local/share/containers\" \
-o -path \"${HOME}/.local/share/gem\" \
-o -path \"${HOME}/.local/share/nvim\" \
-o -path \"${HOME}/.luarocks\" \
-o -path \"${HOME}/.lyrics\" \
-o -path \"${HOME}/.mozilla\" \
-o -path \"${HOME}/.npm\" \
-o -path \"${HOME}/.pkgbuild\" \
-o -path \"${HOME}/.R\" \
-o -path \"${HOME}/.rustup\" \
-o -path \"${HOME}/.ssh\" \
-o -path \"${HOME}/.thunderbird\" \
-o -path \"${HOME}/.vim\" \
-o -path \"${HOME}/.weechat\" \
-o -path \"${HOME}/.zotero\" \
-o -path \"${archivedir:-${HOME}/Desktop}\" \
-o -path \"${backup1:-${HOME}/Desktop}\" \
-o -path \"${backup2:-${HOME}/Desktop}\" \
-o -path \"${kvmdir:-${HOME}/Desktop}\" \
-o -path \"${kvmshare:-${HOME}/Desktop}\" \
-o -path \"${vboxdir:-${HOME}/Desktop}\" \
-o -path \"${encryptpath:-${HOME}/Desktop}\" \
-o -path \"${decryptpath:-${HOME}/Desktop}\" \
-o -path \"${gtkdir:-${HOME}/Desktop}\" \
-o -path \"${plasmadir:-${HOME}/Desktop}\" \
-o -path \"${icondir:-${HOME}/Desktop}\" \
-o -path \"${zshdir:-${HOME}/Desktop}\" \
-o -path \"${pkgbuilddir:-${HOME}/Desktop}\" \
-o -path \"${calibrelibrarydir:-${HOME}/Desktop}\" \
-o -path \"${zoterodatadir:-${HOME}/.zotero-data}\" \
-o -path \"${entrydir:-${HOME}/Desktop}\" \) -prune , \
\( ${inputstring} \) -print"
eval "${cmd}"
}
function destroy {
local item
local parent
while read -r item; do
! [ -e "${item}" ] && continue
parent="$(dirname "${item}")"
rm -rf "${item}"
if [[ "${parent}" != . ]]; then
rmdir -p --ignore-fail-on-non-empty "${parent}"
fi
done
}
function print_usage {
show_header "Usage: seek-and-destroy -p|-s <search string>"
show_listitem " -p|--precise seek files and directories that match exactly"
show_listitem " -s|--sloppy match anything containing <search string>"
}
#
# Parse command line options
#
OPTIONS="hps"
LONGOPTIONS=help,precise,sloppy
PARSED=$(getopt -o "${OPTIONS}" --long "${LONGOPTIONS}" -n "${0}" -- "${@}")
eval set -- "${PARSED}"
MODE=
while [ ${#} -ge 1 ]; do
case ${1} in
-p | --precise)
if [[ -n ${MODE} ]]; then
show_warning "WARNING: overriding seek from ${MODE@Q} to 'precise'."
fi
MODE=precise
shift
;;
-s | --sloppy)
if [[ -n ${MODE} ]]; then
show_warning "WARNING: overriding seek from ${MODE@Q} to 'sloppy'."
fi
MODE=sloppy
shift
;;
-h | --help)
print_usage
exit
;;
--)
shift
break
;;
*)
show_error "ERROR: invalid flag ${1@Q}. Exiting."
exit 3
;;
esac
done
#
# Run seeker and destroyer
#
MODE="${MODE:-precise}"
if [ ${#} = 0 ]; then
show_error "ERROR: no input(s) given. Exiting."
exit 3
fi
show_header "--- Running ${MODE} seeker ---"
RES="$(seek "${@}")"
if [ -n "${RES}" ]; then
echo "${RES}"
CHECK=$(ask_question 'Permission to destroy? (y/N)')
if [[ ${CHECK} =~ ^([yY][eE][sS]|[yY])$ ]]; then
echo "Yessir!"
echo "${RES}" | destroy
show_success "Target(s) eliminated."
else
echo "As you wish..."
fi
else
echo "Target(s) not found. Standing down."
fi