-
Notifications
You must be signed in to change notification settings - Fork 15
/
profile-cleaner
executable file
·244 lines (211 loc) · 7.71 KB
/
profile-cleaner
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
#!/bin/bash
export BLD="\e[01m" RED="\e[01;31m" GRN="\e[01;32m" YLW="\e[01;33m" NRM="\e[00m"
VERS="1.89"
if [[ -z $(which parallel) ]]; then
echo -e ${RED}"Error: GNU Parallel is now a requirement of this script. Please install it and try again."${NRM}
exit 1
fi
find /tmp -name 'work2do.*' -o -name 'accounting.*' -o -name "totalwork2do" 2>/dev/null | SHELL=/bin/bash parallel rm -f
echo -e "${RED}profile-cleaner v$VERS${NRM}"
echo
# $XDG_CONFIG_HOME should be mapped to $HOME/.config
# some users may have modified it to a custom location so honor that
XDG_CONFIG_HOME="${XDG_CONFIG_HOME:-$HOME/.config}"
do_clean() {
echo -en "${GRN} Cleaning${NRM} ${1##*/}"
bsize=$(ls -l "$1" | gawk {'print $5'})
sqlite3 "$1" vacuum
sqlite3 "$1" reindex
asize=$(ls -l "$1" | gawk {'print $5'})
dsize=$(echo "scale=2; ($bsize-$asize)/1048576" | bc)
echo -e "$(tput cr)$(tput cuf 46) ${GRN}done${NRM} -${YLW}${dsize}${NRM} Mbytes"
echo "$name,$dsize" >> /tmp/accounting.$name
}
do_all() {
### chrome based ###
for name in chromium google-chrome; do
export name
prepath="$XDG_CONFIG_HOME"
[[ -h "$prepath" ]] && profilepath=$(readlink $prepath) || profilepath="$prepath"
if [[ ! -d "$profilepath" ]]; then
echo -en "${BLD} $name${NRM}"
echo -e "$(tput cr)$(tput cuf 16) ${BLD}[ ]${NRM}"
else
echo -en "${BLD} $name${NRM}"
echo -e "$(tput cr)$(tput cuf 16) ${BLD}[${GRN}X${NRM}${BLD}]${NRM}"
find "$profilepath" -maxdepth 2 -type f -name 'Histor*' -o -name 'Archiv*' -o -name 'Safe*' >> /tmp/work2do.$name
fi
done
### db based ###
for name in midori qupzilla; do
export name
[[ "$name" = "midori" ]] && prepath="$XDG_CONFIG_HOME"/$name
[[ "$name" = "qupzilla" ]] && prepath=$HOME/.$name/profiles
[[ -h "$prepath" ]] && profilepath=$(readlink $prepath) || profilepath="$prepath"
if [[ ! -d "$profilepath" ]]; then
echo -en "${BLD} $name${NRM}"
echo -e "$(tput cr)$(tput cuf 16) ${BLD}[ ]${NRM}"
else
echo -en "${BLD} $name${NRM}"
echo -e "$(tput cr)$(tput cuf 16) ${BLD}[${GRN}X${NRM}${BLD}]${NRM}"
find "$profilepath" -maxdepth 2 -type f -name '*.db' >> /tmp/work2do.$name
fi
done
### xul based ###
for name in firefox conkeror aurora thunderbird; do
[[ "$name" = "firefox" ]] && prepath=$HOME/.mozilla/$name
[[ "$name" = "conkeror" ]] && prepath=$HOME/.conkeror.mozdev.org/$name
[[ "$name" = "aurora" ]] && prepath=$HOME/.mozilla/aurora
[[ "$name" = "thunderbird" ]] && prepath=$HOME/.$name
export name
[[ -h "$prepath" ]] && profilepath=$(readlink $prepath) || profilepath="$prepath"
if [[ ! -f "$profilepath"/profiles.ini ]]; then
echo -en "${BLD} $name${NRM}"
echo -e "$(tput cr)$(tput cuf 16) ${BLD}[ ]${NRM}"
else
echo -en "${BLD} $name${NRM}"
echo -e "$(tput cr)$(tput cuf 16) ${BLD}[${GRN}X${NRM}${BLD}]${NRM}"
workArr=( $(grep '[P,p]'ath $profilepath/profiles.ini | sed -e 's/[P,p]ath=//' -e 's/\r//') )
for i in "${workArr[@]}"; do
if [ ! -d "$profilepath/$i" ]; then
find "$i" -maxdepth 4 -type f -name '*.db' >> /tmp/work2do.$name
find "$profilepath" -maxdepth 2 -type f -name '*.db' >> /tmp/work2do.$name
fi
done
fi
done
### run cleaner ###
echo
for i in /tmp/work2do.*; do
name=${i##*.}
export name
cat $i | SHELL=/bin/bash parallel do_clean 2>/dev/null
done
### report savings and remove temp files ###
echo
for i in /tmp/accounting.*; do
name=${i##*.}
echo -e " ${BLD}Profile(s) for ${i##*.} reduced by ${YLW}$(grep $name $i | gawk -F, '{ print $2 }' | paste -sd+ - | bc)${NRM} ${BLD}Mbytes.${NRM}"
done
find /tmp -name 'work2do.*' -o -name 'accounting.*' -o -name "totalwork2do" | SHELL=/bin/bash parallel rm -f
}
do_chromebased() {
[[ -h "$prepath" ]] && profilepath=$(readlink $prepath) || profilepath="$prepath"
[[ ! -d "$profilepath" ]] && echo -e ${RED}"Error: no profile directory for $name found."${NRM} && exit 1
echo -e " ${YLW}Cleaning profile for $name${NRM}"
find "$profilepath" -maxdepth 2 -type f -name 'Histor*' -o -name 'Archiv*' -o -name 'Safe*' | SHELL=/bin/bash parallel -k do_clean 2>/dev/null
echo
echo -e " ${BLD}Profile for $name reduced by ${YLW}$(grep $name /tmp/accounting.$name | gawk -F, '{ print $2 }' | paste -sd+ - | bc)${NRM} ${BLD}Mbytes.${NRM}"
[[ -f /tmp/accounting.$name ]] && rm -f /tmp/accounting.$name
}
do_xulbased() {
if [[ -h "$prepath" ]]; then
profilepath=$(readlink $prepath)
else
profilepath="$prepath"
fi
[[ "$name" = "aurora" ]] && name="heftig aurora"
if [[ ! -d "$profilepath" ]]; then
echo -e ${RED}"Error: cannot locate $profilepath"${NRM}
echo -e ${BLD}"This is the default path for $name and where $0 expects to find it."${NRM}
exit 1
fi
[[ ! -f $profilepath/profiles.ini ]] && echo -e ${RED}"Error: cannot locate $profilepath/profiles.ini to determine names of profiles for $name."${NRM} && exit 1
# workArr is a raw list of profiles
workArr=( $(grep '[P,p]'ath $profilepath/profiles.ini | sed -e 's/[P,p]ath=//' -e 's/\r//') ) # read each profile into the work array
# FinalArr is a processed list of profiles
# they are formated to abs paths since some are rel and some are abs
index=0
for i in "${workArr[@]}"; do
if [ ! -d ${profilepath}/"$i" ]; then
FinalArr[index]="$i"
else
FinalArr[index]="$profilepath/$i"
fi
index=$index+1
done
# here is the actual worker
echo -e " ${YLW}Cleaning profile for $name${NRM}"
find -L ${FinalArr[@]} -maxdepth 2 -name '*.sqlite' | SHELL=/bin/bash parallel do_clean 2>/dev/null
echo
echo -e " ${BLD}Profile(s) for $name reduced by ${YLW}$(grep $name /tmp/accounting.$name | gawk -F, '{ print $2 }' | paste -sd+ - | bc)${NRM} ${BLD}Mbytes.${NRM}"
[[ -f /tmp/accounting.$name ]] && rm -f /tmp/accounting.$name
}
do_dbbased() {
[[ -h "$prepath" ]] && profilepath=$(readlink $prepath) || profilepath="$prepath"
[[ ! -d "$profilepath" ]] && echo -e ${RED}"Error: no profile directory for $name found."${NRM} && exit 1
echo -e " ${YLW}Cleaning profile for $name${NRM}"
find ${profilepath} -maxdepth 2 -type f -name '*.db' | SHELL=/bin/bash parallel do_clean 2>/dev/null
echo
echo -e " ${BLD}Profile for $name reduced by ${YLW}$(grep $name /tmp/accounting.$name | gawk -F, '{ print $2 }' | paste -sd+ - | bc)${NRM} ${BLD}Mbytes.${NRM}"
[[ -f /tmp/accounting.$name ]] && rm -f /tmp/accounting.$name
}
export -f do_clean
case "$1" in
A|a)
do_all
exit 0
;;
C|c)
name="chromium" ; export name
prepath="$XDG_CONFIG_HOME"/$name
do_chromebased
exit 0
;;
Gc|gC|GC|gc)
name="google-chrome" ; export name
prepath="$XDG_CONFIG_HOME"/$name
do_chromebased
exit 0
;;
F|f)
name="firefox"; export name
prepath=$HOME/.mozilla/$name
do_xulbased
exit 0
;;
CK|cK|Ck|ck)
name="conkeror"; export name
prepath=$HOME/.conkeror.mozdev.org/$name
do_xulbased
exit 0
;;
H|h)
name="heftig aurora"; export name
prepath=$HOME/.mozilla/aurora
do_xulbased
exit 0
;;
T|t)
name="thunderbird"; export name
prepath=$HOME/.$name
do_xulbased
exit 0
;;
M|m)
name="midori"; export name
prepath="$XDG_CONFIG_HOME"/$name
do_dbbased
exit 0
;;
Q|q)
name="qupzilla"; export name
prepath=$HOME/.$name/profiles
do_dbbased
exit 0
;;
*)
echo -e " ${BLD}$0 ${NRM}${GRN}{a|c|ck|f|gc|h|m|t|q}${NRM}"
echo
echo -e " ${BLD}a) ${GRN}A${NRM}${BLD}ll supported profiles (auto-detected)"${NRM}
echo -e " ${BLD}c) ${GRN}C${NRM}${BLD}hromium"${NRM}
echo -e " ${BLD}ck) ${GRN}C${NRM}${BLD}on${GRN}k${NRM}${BLD}eror"${NRM}
echo -e " ${BLD}f) ${GRN}F${NRM}${BLD}irefox"${NRM}
echo -e " ${BLD}gc) ${GRN}G${NRM}${BLD}oogle ${GRN}C${NRM}${BLD}hrome"${NRM}
echo -e " ${BLD}h) ${GRN}H${NRM}${BLD}eftig's Aurora"${NRM}
echo -e " ${BLD}m) ${GRN}M${NRM}${BLD}idori"${NRM}
echo -e " ${BLD}t) ${GRN}T${NRM}${BLD}hunderbird"${NRM}
echo -e " ${BLD}q) ${GRN}Q${NRM}${BLD}upZilla"${NRM}
exit 0
;;
esac