-
Notifications
You must be signed in to change notification settings - Fork 4
/
paccache-service-manager
executable file
·232 lines (201 loc) · 8.31 KB
/
paccache-service-manager
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
#!/bin/bash
#
# Simple service manager for pacman cache cleaner.
#
# Requires packages:
# pacman-contrib (for paccache)
# yad (for yad)
# polkit (for pkexec)
#
source /usr/share/endeavouros/scripts/eos-script-lib-yad || exit 1
export -f eos_yad
AssignIconVariables_in_eos_bash_shared
DIE() {
local msg="$1"
shift
if false ; then
echo "Error: $msg" | eos_yad --text-info --image=$ICO_ERROR --title="$progname error" \
--wrap --width=500 --height=300 --button=yad-quit:1 "$@"
else
eos_yad --form --image=$ICO_ERROR --title="$progname error" --text="Error: $msg" --button=yad-quit:1 --scroll "$@"
fi
exit 1
}
GetCurrentValues() {
local t="$timer"
local s="$service"
if [ ! -r $s ] ; then
# service is only under /usr/lib/... so get current values from there
t="$dir2/$pt"
s="$dir2/paccache.service"
local why="Is pacman-contrib installed?"
test -r $t || DIE "sorry, $t does not exist. $why"
test -r $s || DIE "sorry, $s does not exist. $why"
fi
current_period="$( grep "^OnCalendar=" $t | cut -d '=' -f 2)"
current_keepcount="$(grep "^ExecStart=" $s | awk '{print $NF}')"
current_delete_uninstalled=FALSE
# ad hoc check for the 'keepcount' value
case "$current_keepcount" in
-r) current_keepcount=3 ;; # original value
-rk*)
[ "$(echo "${current_keepcount:3}" | sed 's|[0-9]||g')" = "" ] || {
local line=$(grep "^ExecStart=" $s)
DIE "invalid value for option <tt>-k</tt> in file\n<tt>$s</tt>\nline:\n\n<tt>${line::70}</tt>"
}
current_keepcount="${current_keepcount:3}" # value after using this app
[ -z "$current_keepcount" ] && DIE "file '$s': paccache option '-k' requires a value."
;;
*)
local line=$(grep "^ExecStart=" $s)
DIE "invalid line in file\n<tt>$s</tt>\nline:\n\n<tt>${line::70}</tt>"
;;
esac
}
Usage() {
cat <<EOF
Usage: $progname [options]
Options:
--defaults Enable manager with default values (run $period, keep latest $keepcount).
-v, --verbose Increase verbosity (= see more terminal output).
-h, --help This help.
EOF
[ -n "$1" ] && exit "$1"
}
AddCommand() {
local show=yes
case "$1" in
--silent) show=no; shift ;;
esac
local command="$1"
cmds+=" ; $command"
if [ "$verbose" = "yes" ] && [ "$show" = "yes" ] ; then
echo "$command" >> $vfile
fi
}
_paccache_cleaner_manager()
{
local progname=paccache-service-manager
eos_assert_deps $progname yad || return 1
local dir=/etc/systemd/system
local dir2=/usr/lib/systemd/system # current values may be only here...
local service=$dir/paccache.service
local timer=$dir/paccache.timer
local current_period
local current_keepcount
local current_delete_uninstalled
local pt="paccache.timer"
local cmd_yad
local cmds=":" cmds2=() cmd_period cmd_ruk=""
local xx
local period_vals=""
local txt
local image=$ICO_DISK # drive-harddisk # disk-utility
local result=""
local field_nr=1
local indent="<tt> </tt>"
local du="$(LANG=C /usr/bin/du --si -s /var/cache/pacman/pkg | /usr/bin/awk '{print $1}' | sed 's|\([KMGT]\)$| \1B|')"
local df="$(LANG=C /usr/bin/df --si | /usr/bin/grep -w / | /usr/bin/awk '{print $4}' | sed 's|\([KMGT]\)$| \1B|')"
local options=""
local defaults=no
local period=weekly keepcount=2 delete_uninstalled=FALSE
local first_time=no
local verbose=no
local vfile="" # temporary file for showing all commands in verbose mode
local keepcount_max=100
if [ ! -r $service ] ; then
first_time=yes
fi
case "$1" in
-v | --verbose) options+=" -v"
options="${options# }" # drop leading space
verbose=yes
vfile="$(mktemp)"
;;
-h | --help) Usage 0
;;
--defaults) defaults=yes
;;
esac
GetCurrentValues
if [ "$defaults" = "no" ] ; then
txt="<b>Modifies the service that cleans up pacman cache periodically.\n"
txt+="Below you'll see settings and their current values.\n\n"
txt+="Current status:\n"
txt+="${indent}Package cache usage: $du\n"
txt+="${indent}Free space on disk: $df</b>\n"
txt+="\n"
# List possible period values, and set the default
for xx in daily weekly monthly ; do
test -n "$period_vals" && period_vals+="!" # separator
test "$xx" = "$current_period" && period_vals+="^" # default
period_vals+="$xx"
done
cmd_yad=(
eos_yad --form --width=500 --image=$image
--title='Pacman cache cleaner service manager'
--text="$txt"
--field='Cleaning period!How often the package cache will be cleaned':CB "$period_vals"
--field='Number of the latest package versions to keep!Keep only the selected number of each package':NUM "$current_keepcount"
--field='Remove uninstalled but still cached packages now!One time operation':CHK "$current_delete_uninstalled"
)
result="$("${cmd_yad[@]}" 2>/dev/null)"
[ -n "$result" ] || return # package kde-gtk-config should remove possible gtk related yad warnings...
# New values
period="$( echo "$result" | cut -d '|' -f $field_nr)" ; ((++field_nr))
keepcount="$( echo "$result" | cut -d '|' -f $field_nr)" ; ((++field_nr))
delete_uninstalled="$(echo "$result" | cut -d '|' -f $field_nr)" ; ((++field_nr))
# ad hoc value checks
[ "$(echo "$keepcount" | sed 's|[0-9]||g')" = "" ] || DIE "sorry, keep count value '$keepcount' is not a number"
[ $keepcount -le $keepcount_max ] || DIE "sorry, keep count value '$keepcount' is more than $keepcount_max and not supported"
case "$period" in
daily | weekly | monthly) ;;
*) DIE "sorry, cleaning period '$period' is unsupported" ;;
esac
case "$delete_uninstalled" in
TRUE | FALSE) ;;
*) DIE "sorry, value '$delete_uninstalled' for removing uninstalled packages is unsupported" ;;
esac
fi
# Build the final paccache command list.
# Clean up cache now and periodically
if [ "$delete_uninstalled" = "TRUE" ] ; then
cmd_ruk="paccache $options -ruk0"
fi
if [ "$keepcount $period $delete_uninstalled" != "$current_keepcount $current_period $current_delete_uninstalled" ] || [ "$first_time" = "yes" ] ; then
cmd_period="paccache $options -rk$keepcount"
if [ ! -r $service ] ; then
AddCommand "cp $dir2/$pt $timer"
AddCommand "cp $dir2/paccache.service $service"
fi
# Allow ExecStart line with both /usr/bin/paccache or just paccache.
AddCommand "sed -i $service -e 's|^ExecStart=paccache .*$|ExecStart=$cmd_period|' -e 's|^ExecStart=/usr/bin/paccache .*$|ExecStart=$cmd_period|'"
AddCommand "sed -i $timer -e 's|^OnCalendar=.*$|OnCalendar=$period|'"
AddCommand "systemctl enable --now $pt"
AddCommand "systemctl daemon-reload"
fi
if [ -n "$cmd_ruk" ] ; then
AddCommand "$cmd_ruk"
fi
# Execute the command list.
if [ "$cmds" != ":" ] ; then
AddCommand --silent "echo Service reloaded."
if [ "$verbose" = "yes" ] ; then
# pkexec bash -c "cat '$vfile' ; $cmds"
RunInTerminal "cat \"$vfile\"; $EOS_ROOTER \"$cmds\""
rm -f "$vfile"
else
# pkexec bash -c "$cmds"
if [ "$verbose" = "yes" ] ; then
RunInTerminal "cat \"$vfile\"; $EOS_ROOTER \"$cmds\""
else
RunInTerminal "echo \"Running selected commands.\"; $EOS_ROOTER \"$cmds\""
fi
fi
else
# echo "No change to settings."
local msg="Nothing to do (settings not changed)"
eos_yad --form --image=$ICO_INFO --title="$progname" --text="$msg" --button=yad-quit:1 --scroll
fi
}
_paccache_cleaner_manager "$@"