-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPackage-Recompiler.sh
344 lines (316 loc) · 9.24 KB
/
Package-Recompiler.sh
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
trap 'echo Got SIGINT, ignoring.' 2
# Recompile Packages Automatically
#Paths and Filename-Definitions
db_folder='/var/db/arch-recompiler'
deps_db='deps.db'
expl_db='explicit.db'
igno_db='ignore.list'
#Definitions
default_igno_list='linux
linux-lts
linux-headers
linux-lts-headers'
#In-Memory Databases
declare -A deps_indb
declare -A expl_indb
igno_indb=()
deps_worklist=()
expl_worklist=()
#Flags
create_deps_db=0
create_expl_db=0
create_igno_db=0
fix_filerights=0
echo 'Init completed.'
if [ ! -d "$db_folder" ]; then
echo 'db-folder does not exist, creating new database-folder...'
mkdir -p "$db_folder" >/dev/null 2>&1
if test $? -ne 0; then
echo "can't create dir, trying with sudo"
sudo mkdir -p "$db_folder" >/dev/null 2>&1
if test $? -ne 0; then
echo "mkdir failed.";exit 1
fi
echo "changing rights for the folder with sudo..."
sudo chown $(whoami): "$db_folder"
if test $? -ne 0; then
echo "chown failed.";exit 1
fi
sudo chmod 700 "$db_folder"
if test $? -ne 0; then
echo "chmod failed.";exit 1
fi
fi
fi
echo 'Checking Database...'
if [ ! -f "$db_folder/$deps_db" ]; then
echo 'depency-packages database-file does not exist, going to create a new one...'
create_deps_db=1
fi
if [ ! -f "$db_folder/$expl_db" ]; then
echo 'explicit-packages database-file does not exist, going to create a new one...'
create_expl_db=1
fi
if [ ! -f "$db_folder/$igno_db" ]; then
echo 'ignore-package list-file does not exist, going to create a new one with default-list...'
create_igno_db=1
fi
echo 'Checking file-rights...'
touch "$db_folder/$deps_db" >/dev/null 2>&1
if test $? -ne 0; then
fix_filerights=1
fi
touch "$db_folder/$expl_db" >/dev/null 2>&1
if test $? -ne 0; then
fix_filerights=1
fi
touch "$db_folder/$igno_db" >/dev/null 2>&1
if test $? -ne 0; then
fix_filerights=1
fi
if [ "$fix_filerights" -eq "1" ]; then
echo "insufficient file-rights, try to fix this with sudo..."
sudo chown $(whoami): -R "$db_folder"
if test $? -ne 0; then
echo "chown failed.";exit 1
fi
sudo chmod 600 -R "$db_folder"
if test $? -ne 0; then
echo "chmod failed.";exit 1
fi
sudo chmod 700 "$db_folder"
if test $? -ne 0; then
echo "chmod failed.";exit 1
fi
fi
unset fix_filerights
if [ "$create_deps_db" -eq "1" -o "$create_expl_db" -eq "1" -o "$create_igno_db" -eq "1" ]; then
echo "Creating databases ..."
if [ "$create_deps_db" -eq "1" ]; then
echo " Depency-packages database..."
echo "" > "$db_folder/$deps_db" 2>&1
if test $? -ne 0; then
echo "failed.";exit 1
fi
fi
if [ "$create_expl_db" -eq "1" ]; then
echo " Explicit-packages database..."
echo "" > "$db_folder/$expl_db" 2>&1
if test $? -ne 0; then
echo "failed.";exit 1
fi
fi
if [ "$create_igno_db" -eq "1" ]; then
echo " Ignore-packages list..."
echo "$default_igno_list" > "$db_folder/$igno_db" 2>&1
if test $? -ne 0; then
echo "failed.";exit 1
fi
fi
fi
echo "Locking databases..."
if [ -f "$db_folder/$deps_db.lock" ]; then
echo "ERROR: Depency-packages database locked.";exit 1
fi
touch "$db_folder/$deps_db.lock" >/dev/null 2>&1
if test $? -ne 0; then
echo "ERROR: Can't lock depency-packages database.";exit 1
fi
if [ -f "$db_folder/$expl_db.lock" ]; then
echo "ERROR: Explicit-packages database locked.";exit 1
fi
touch "$db_folder/$expl_db.lock" >/dev/null 2>&1
if test $? -ne 0; then
echo "ERROR: Can't lock explicit-packages database.";exit 1
fi
if [ -f "$db_folder/$igno_db.lock" ]; then
echo "ERROR: Ignore-packages database locked.";exit 1
fi
touch "$db_folder/$igno_db.lock" >/dev/null 2>&1
if test $? -ne 0; then
echo "ERROR: Can't lock ignore-packages database.";exit 1
fi
echo "Loading databases..."
tmp1=""
tmp2=""
echo " Depency-packages database..."
while read e; do
tmp1=$(echo $e | cut -d' ' -f1)
tmp2=$(echo $e | cut -d' ' -f2)
if [ ! -z "$tmp1" -a ! -z "$tmp2" ]; then
deps_indb["$tmp1"]="$tmp2"
fi
done <"$db_folder/$deps_db"
tmp1=""
tmp2=""
echo " Explicit-packages database..."
while read e; do
tmp1=$(echo $e | cut -d' ' -f1)
tmp2=$(echo $e | cut -d' ' -f2)
if [ ! -z "$tmp1" -a ! -z "$tmp2" ]; then
expl_indb["$tmp1"]="$tmp2"
fi
done <"$db_folder/$expl_db"
unset tmp1 tmp2
echo " Ignore-packages database..."
while read e; do
if [ ! -z "$e" ]; then
igno_indb+=("$e")
fi
done <"$db_folder/$igno_db"
unset e
echo "Cleaning database for uninstalled packages..."
echo " Depency-packages database..."
for e in "${!deps_indb[@]}"; do
yaourt -Qidn "$e" >/dev/null 2>&1
if test $? -ne 0; then
echo "Delete $e from database, its no longer installed (as depency)."
unset -v deps_indb[$e]
grep -v "^$e " "$db_folder/$deps_db" > "$db_folder/$deps_db.bak" && mv "$db_folder/$deps_db.bak" "$db_folder/$deps_db"
if test $? -ne 0; then
echo "ERROR: Can't update depency-packages database-file."; exit 1
fi
fi
done
echo " Explicit-packages database..."
for e in "${!expl_indb[@]}"; do
yaourt -Qien "$e" >/dev/null 2>&1
if test $? -ne 0; then
echo "Delete $e from database, its no longer (explicit) installed."
unset -v expl_indb[$e]
grep -v "^$e " "$db_folder/$expl_db" > "$db_folder/$expl_db.bak" && mv "$db_folder/$expl_db.bak" "$db_folder/$expl_db"
if test $? -ne 0; then
echo "ERROR: Can't update explicit-packages database."; exit 1
fi
fi
done
echo "Calculating worklist..."
tmp1=""
tmp2=""
echo " Depency-packages..."
oldifs="$IFS"
IFS=$'\n'
for e in `yaourt -Qdn`; do
tmp1=$(echo $e | cut -d' ' -f1)
tmp2=$(echo $e | cut -d' ' -f2)
if [ ! -z "$tmp1" -a ! -z "$tmp2" ]; then
if [ "${deps_indb[$tmp1]}" = "$tmp2" ]; then
continue
fi
if [[ " ${igno_indb[@]} " =~ " ${tmp1} " ]]; then
continue
fi
deps_worklist+=("$tmp1")
fi
done
tmp1=""
tmp2=""
echo " Explicit-packages..."
for e in `yaourt -Qen`; do
tmp1=$(echo $e | cut -d' ' -f1)
tmp2=$(echo $e | cut -d' ' -f2)
if [ ! -z "$tmp1" -a ! -z "$tmp2" ]; then
if [ "${expl_indb[$tmp1]}" = "$tmp2" ]; then
continue
fi
if [[ " ${igno_indb[@]} " =~ " ${tmp1} " ]]; then
continue
fi
expl_worklist+=("$tmp1")
fi
done
IFS="$oldifs"
unset tmp1 tmp2 e
echo "Compiling packages..."
tmp1=""
tmp2=""
echo " Depency-Packages"
for e in "${deps_worklist[@]}"; do
echo " Package: '$e'"
yaourt -Sy > /dev/null
tmp1=$(yaourt -Qidn "$e" | grep "^Version" | cut -d':' -f2 | tr -d '[:space:]')
tmp2=$(yaourt -Si "$e" | grep "^Version" | cut -d':' -f2 | tr -d '[:space:]')
if [ ! -z "$tmp1" ]; then
if [ -z "$tmp2" ]; then
echo "ERROR: we seem to have no Internet-connection..."; exit 1
fi
if [ "$tmp1" != "$tmp2" ]; then
echo "local / remote version missmatch, can't recompile."
continue
fi
else
echo "Warning: Package '$e' has been uninstalled."
continue
fi
yaourt -Sb $e --asdeps --noconfirm > /dev/null 2>&1
if test $? -ne 0; then
echo "Warning: Package '$e' could not be compiled, maybe you want to add it to ignore list"
fi
tmp1=$(yaourt -Qidn "$e" | grep "^Version" | cut -d':' -f2 | tr -d '[:space:]')
if [ -z "$tmp1" ]; then
echo "Warning: Can't fetch version of '$e', ignoring."
continue
fi
grep -v "^$e " "$db_folder/$deps_db" > "$db_folder/$deps_db.bak" &&
echo "$e $tmp1" >> "$db_folder/$deps_db.bak" &&
mv "$db_folder/$deps_db.bak" "$db_folder/$deps_db"
if test $? -ne 0; then
echo "ERROR: Database-File action failed."; exit 1
fi
done
unset deps_worklist
tmp1=""
echo " Explicit-Packages"
for e in "${expl_worklist[@]}"; do
echo " Package: $e"
yaourt -Sy > /dev/null
tmp1=$(yaourt -Qien "$e" | grep "^Version" | cut -d':' -f2 | tr -d '[:space:]')
tmp2=$(yaourt -Si "$e" | grep "^Version" | cut -d':' -f2 | tr -d '[:space:]')
if [ ! -z "$tmp1" ]; then
if [ -z "$tmp2" ]; then
echo "ERROR: we seem to have no Internet-connection..."; exit 1
fi
if [ "$tmp1" != "$tmp2" ]; then
echo "Warning: local / remote version missmatch, can't recompile."
continue
fi
else
echo "Warning: Package '$e' has been uninstalled."
continue
fi
yaourt -Sb $e --asexplicit --noconfirm > /dev/null 2>&1
if test $? -ne 0; then
echo "Warning: Package '$e' could not be compiled, maybe you want to add it to ignore list"
fi
tmp1=$(yaourt -Qien "$e" | grep "^Version" | cut -d':' -f2 | tr -d '[:space:]')
if [ $? -ne 0 -o -z "$tmp1" ]; then
echo "Warning: Can't fetch version of '$e', ignoring."
continue
fi
grep -v "^$e " "$db_folder/$expl_db" > "$db_folder/$expl_db.bak" &&
echo "$e $tmp1" >> "$db_folder/$expl_db.bak" &&
mv "$db_folder/$expl_db.bak" "$db_folder/$expl_db"
if test $? -ne 0; then
echo "ERROR: Database-File action failed."; exit 1
fi
done
unset tmp1 tmp2 expl_worklist
echo "Unlocking databases..."
[ -f "$db_folder/$deps_db.lock" ] && (
rm "$db_folder/$deps_db.lock" >/dev/null 2>&1
if test $? -ne 0; then
echo "ERROR: Can't unlock depency-packages database.";exit 1
fi
)
[ -f "$db_folder/$expl_db.lock" ] && (
rm "$db_folder/$expl_db.lock" >/dev/null 2>&1
if test $? -ne 0; then
echo "ERROR: Can't unlock explicit-packages database.";exit 1
fi
)
[ -f "$db_folder/$igno_db.lock" ] && (
rm "$db_folder/$igno_db.lock" >/dev/null 2>&1
if test $? -ne 0; then
echo "ERROR: Can't unlock ignore-packages database.";exit 1
fi
)