-
Notifications
You must be signed in to change notification settings - Fork 0
/
tt-setup
executable file
·250 lines (230 loc) · 6.5 KB
/
tt-setup
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
#!/bin/zsh
# shellcheck shell=bash
# tt-setup
# v0.9.15
# macOS
#
# Set-up Trash Tools - part of Trash Tools
# Copyright (c) 2021 Joss Brown (pseud.)
# License: MIT / place of jurisdiction: Berlin, Germany / German laws apply
#
# keyboard shortcut: - (command-line only)
#
# setup/test Trash Tools
export LANG=en_US.UTF-8
export PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/local/bin:/opt/homebrew/bin:/opt/sw/bin:"$HOME"/.local/bin:"$HOME"/bin:"$HOME"/local/bin
if [[ $1 =~ ^(-p|--protections)$ ]] ; then
echo -e " * Manually add filenames for categorical protection to:\n * ~/.config/TrashTools/protections.txt" >&2
protections=$(cat "$HOME/.config/TrashTools/protections.txt" 2>/dev/null)
if [[ $protections ]] ; then
echo "--------" >&2
while read -r protected
do
echo "Protected: $protected"
done < <(echo "$protections")
exit
else
exit 1
fi
elif [[ $1 =~ ^(-h|--history) ]] ; then
account=$(id -u)
undohist=$(cat "/tmp/local.lcars.TrashTools.$account.trashes" 2>/dev/null)
if [[ $undohist ]] ; then
echo " * Trashes history" >&2
while read -r histentry
do
echo "--------" >&2
histvol=$(echo "$histentry" | awk -F":" '{print $1}')
histfile=$(echo "$histentry" | awk -F":" '{print substr($0, index($0,$2))}')
echo -e "File: $histfile"
echo -e "Volume: $histvol"
done < <(echo "$undohist")
exit
else
echo " * Trashes history is empty" >&2
exit 1
fi
fi
errors=false
echo " * Looking for Trash Tools scripts..."
read -d '' programs <<"EOP"
empty-trashes
list-protected
protect
put-back
trashes
undo-trashes
unlink
EOP
while read -r program
do
if command -v "$program" &>/dev/null ; then
echo "✅ Installed: $program"
else
echo "❓ MISSING: $program"
errors=true
fi
done < <(echo "$programs")
echo " * Looking for requisites..."
read -d '' clilist <<"EOT"
tag
trash
EOT
while read -r cli
do
if command -v "$cli" &>/dev/null ; then
echo "✅ Installed: $cli"
else
echo "❓ MISSING: $cli"
errors=true
fi
done < <(echo "$clilist")
echo " * Testing requisites..."
if [[ $(tag -h 2>/dev/null | head -1) == "tag - A tool for manipulating and querying file tags." ]] ; then
echo "✅ Tested: tag"
else
echo "❌ FAILED: tag"
errors=true
fi
if [[ $(trash 2>&1) == "Specify one or more paths" ]] ; then
echo "✅ Tested: trash"
else
echo "❌ FAILED: trash"
errors=true
fi
echo " * Looking for user directories..."
read -d '' ulocs <<"EOL"
.cache/Finder
.config/TrashTools
EOL
while read -r uloc
do
uloc="$HOME/$uloc"
if ! [[ -e "$uloc" ]] ; then
if mkdir -p "$uloc" &>/dev/null ; then
echo "✅ Directory created: $uloc"
else
echo "❌ ERROR creating: $uloc"
errors=true
fi
else
if ! [[ -d "$uloc" ]] ; then
if ! rm -rf "$uloc" &>/dev/null ; then
echo "⚠️ WARNING - improper filetype: $uloc"
fi
if mkdir -p "$uloc" &>/dev/null ; then
echo "✅ Directory created: $uloc"
else
echo "❌ ERROR creating: $uloc"
errors=true
fi
else
echo "✅ Directory exists: $uloc"
fi
fi
done < <(echo "$ulocs")
read -d '' configs <<"EOC"
.config/TrashTools/protections.txt
EOC
while read -r configloc
do
configloc="$HOME/$configloc"
if [[ -f "$configloc" ]] ; then
echo "✅ Config exists: $configloc"
else
if touch "$configloc" &>/dev/null ; then
echo "✅ Empty config created: $configloc"
else
echo "❌ ERROR creating: $configloc"
errors=true
fi
fi
done < <(echo "$configs")
echo " * Checking file manager settings..."
fman="$HOME/.config/TrashTools/file_manager.txt"
fmaninit=false
if ! [[ -f "$fman" ]] ; then
fmaninit=true
else
echo "✅ Config exists: $fman"
fman_all=$(grep -v "^$" < "$fman" 2>/dev/null)
fman_name=$(echo "$fman_all" | head -1)
fman_bid=$(echo "$fman_all" | tail -n +2)
if ! [[ $fman_name ]] || ! [[ $fman_bid ]] ; then
fmaninit=true
echo "⚠️ WARNING: improper file manager config information"
rm -f "$fman" 2>/dev/null
else
fmapploc=$(mdfind \
-onlyin /Applications/ \
-onlyin "$HOME"/Applications/ \
-onlyin "$HOME"/Developer/Applications/ \
-onlyin /Developer/Applications/ \
-onlyin /Network/Applications/ \
-onlyin /Network/Developer/Applications/ \
"kMDItemCFBundleIdentifier == \"$fman_bid\"" 2>/dev/null | LC_COLLATE=C sort | head -1)
if ! [[ $fmapploc ]] ; then
fmaninit=true
echo "⚠️ WARNING: '$fman_name' not installed – defaulting to Finder (com.apple.finder)"
rm -f "$fman" 2>/dev/null
else
fman_name=$(defaults read "$fmapploc/Contents/Info.plist" CFBundleName 2>/dev/null)
if ! [[ $fman_name ]] ; then
fmaninit=true
echo "⚠️ WARNING: CFBundleName for '$fman_bid' not available – defaulting to Finder (com.apple.finder)"
rm -f "$fman" 2>/dev/null
else
echo "✅ File manager: $fman_name ($fman_bid)"
fi
fi
fi
fi
if $fmaninit ; then
echo "ℹ️ INFO: default file manager not (yet) saved"
filemanager=$(defaults read -g NSFileViewer 2>/dev/null)
if ! [[ $filemanager ]] ; then
filemanager="com.apple.finder"
echo "ℹ️ INFO: system's default file manager not set by user"
fman_name="Finder"
echo "ℹ️ INFO: defaulting to 'Finder' (com.apple.finder)"
else
echo "ℹ️ INFO: '$filemanager' set by user as system's default file manager"
if [[ $filemanager == "com.apple.finder" ]] || [[ $filemanager == "com.apple.Finder" ]] ; then
fman_name="Finder"
else
fmapploc=$(mdfind \
-onlyin /Applications/ \
-onlyin "$HOME"/Applications/ \
-onlyin "$HOME"/Developer/Applications/ \
-onlyin /Developer/Applications/ \
-onlyin /Network/Applications/ \
-onlyin /Network/Developer/Applications/ \
"kMDItemCFBundleIdentifier == \"$filemanager\"" 2>/dev/null | LC_COLLATE=C sort | head -1)
if ! [[ $fmapploc ]] ; then
echo "⚠️ WARNING: '$filemanager' not installed – defaulting to Finder (com.apple.finder)"
fman_name="Finder"
filemanager="com.apple.finder"
else
fman_name=$(defaults read "$fmapploc/Contents/Info.plist" CFBundleName 2>/dev/null)
if ! [[ $fman_name ]] ; then
echo "⚠️ WARNING: CFBundleName for '$filemanager' not available – defaulting to Finder (com.apple.finder)"
fman_name="Finder"
filemanager="com.apple.finder"
fi
fi
fi
echo "ℹ️ INFO - CFBundleName: '$fman_name'"
fi
if echo -ne "$fman_name\n$filemanager" > "$fman" &>/dev/null ; then
echo "✅ Saved file manager info to: $fman"
echo "ℹ️ File manager: $fman_name ($filemanager)"
else
echo "❌ ERROR saving file manager info to: $fman"
errors=true
fi
fi
if $errors ; then
exit 1
else
exit 0
fi