-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharch-updater.sh
More file actions
505 lines (442 loc) · 25.3 KB
/
Copy patharch-updater.sh
File metadata and controls
505 lines (442 loc) · 25.3 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
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
#!/bin/bash
# ==========================================
# 0. GLOBAL LOGLAMA (SESSİZ KAPANMAYI ENGELLER)
# ==========================================
mkdir -p "$HOME/.cache"
MAIN_LOG="$HOME/.cache/arch_updater_main.log"
# Betiğin tüm standart ve hata çıktılarını log dosyasına yönlendir (aynı zamanda terminale de basar)
exec > >(tee -a "$MAIN_LOG") 2>&1
echo -e "\n=========================================="
echo "Arch Updater Başladı: $(date)"
echo "=========================================="
REQUIRED_PKGS=("topgrade" "yad" "notify-send" "checkupdates")
for pkg in "${REQUIRED_PKGS[@]}"; do
if ! command -v "$pkg" &> /dev/null; then
echo "[Hata] $pkg yüklü değil. Çıkılıyor."
exit 1
fi
done
# ==========================================
# 0.1 MANUAL CONTINUE (CONTINUE ARGUMENT)
# ==========================================
BLOCK_FILE="$HOME/.cache/arch_updater_blocked"
if [ "$1" == "continue" ]; then
rm -f "$BLOCK_FILE"
echo "[Bilgi] Arch Updater: Otomatik güncelleme kilidi kaldırıldı."
notify-send -a "Arch Updater" -u normal "Servis Kilidi Açıldı" "Otomatik güncelleme kilidi başarıyla kaldırıldı."
exit 0
fi
if [ -f "$BLOCK_FILE" ]; then
echo "[Uyarı] Sistem kilitli (BLOCK_FILE mevcut). Çıkılıyor."
exit 0
fi
# ==========================================
# 1. ENVIRONMENT VARIABLES AND DISPLAY CONTROL
# ==========================================
XDG_RUNTIME_DIR="/run/user/$(id -u)"
export XDG_RUNTIME_DIR
export DISPLAY=:0
export WAYLAND_DISPLAY=${WAYLAND_DISPLAY:-wayland-0}
export DBUS_SESSION_BUS_ADDRESS="unix:path=${XDG_RUNTIME_DIR}/bus"
echo "[Bilgi] Display hazırlığı bekleniyor..."
WAIT_TIME=0
MAX_WAIT=120
while [ ! -S "$XDG_RUNTIME_DIR/$WAYLAND_DISPLAY" ] && [ ! -e "/tmp/.X11-unix/X${DISPLAY#*:}" ]; do
sleep 5
WAIT_TIME=$((WAIT_TIME + 5))
if [ "$WAIT_TIME" -ge "$MAX_WAIT" ]; then
echo "[Hata] Display 120 saniye içinde hazır olamadı. Çıkılıyor."
exit 0
fi
done
echo "[Bilgi] Display hazır."
# ==========================================
# 2. SINGLE INSTANCE LOCK
# ==========================================
SCRIPT_PATH=$(realpath "$0")
LOCK_FILE="/tmp/arch_updater.lock"
exec 9> "$LOCK_FILE"
if ! flock -n 9; then
echo "[Uyarı] Betik zaten çalışıyor (Lock file mevcut). Çıkılıyor."
exit 0
fi
# ==========================================
# 3. SETTINGS AND VARIABLES
# ==========================================
LOG_FILE="$HOME/.cache/arch_updater_last_run"
LAST_CHECK_LOG="$HOME/.cache/arch_updater_last_check"
NEWS_ACK_LOG="$HOME/.cache/arch_updater_news_ack"
REBOOT_LOG="$HOME/.cache/arch_updater_reboot_time"
POST_UPDATE_FLAG="$HOME/.cache/arch_updater_post_action"
RETRY_COUNT_FILE="$HOME/.cache/arch_updater_news_retry"
CONFIG_DIR="$HOME/.config/arch-updater"
CONFIG_FILE="$CONFIG_DIR/settings.conf"
RSS_FEED_URL="https://archlinux.org/feeds/news/"
NEWS_PAGE_URL="https://archlinux.org/news/"
LANG_CHECK=$(echo "$LANG" | grep -iq "tr" && echo "TR" || echo "EN")
mkdir -p "$CONFIG_DIR"
[ ! -f "$LOG_FILE" ] && echo 0 > "$LOG_FILE"
[ ! -f "$LAST_CHECK_LOG" ] && echo "" > "$LAST_CHECK_LOG"
[ ! -f "$NEWS_ACK_LOG" ] && echo 0 > "$NEWS_ACK_LOG"
[ ! -f "$RETRY_COUNT_FILE" ] && echo 0 > "$RETRY_COUNT_FILE"
[ ! -f "$CONFIG_FILE" ] && touch "$CONFIG_FILE"
NEWS_ACK_EPOCH=$(cat "$NEWS_ACK_LOG")
NEWS_RETRIES=$(cat "$RETRY_COUNT_FILE")
# shellcheck disable=SC1090
[ -f "$CONFIG_FILE" ] && source "$CONFIG_FILE"
BOOT_TIME=$(awk '/btime/ {print $2}' /proc/stat)
PENDING_REBOOT=0
if [ -f "$REBOOT_LOG" ]; then
LOGGED_REBOOT_TIME=$(cat "$REBOOT_LOG")
if [ "$LOGGED_REBOOT_TIME" -gt "$BOOT_TIME" ]; then PENDING_REBOOT=1; fi
fi
update_config() {
local key="$1"
local val="$2"
if grep -q "^${key}=" "$CONFIG_FILE" 2>/dev/null; then
sed -i "s/^${key}=.*/${key}=${val}/" "$CONFIG_FILE"
else
echo "${key}=${val}" >> "$CONFIG_FILE"
fi
}
# ==========================================
# 4. GÜNDE 1 KEZ ÇALIŞMA KONTROLÜ
# ==========================================
TODAY=$(date +%Y-%m-%d)
if [[ "$1" != "--retry" && "$1" != "--criticalupdate" ]]; then
LAST_CHECK_DATE=$(cat "$LAST_CHECK_LOG")
if [ "$LAST_CHECK_DATE" = "$TODAY" ]; then
echo "[Bilgi] Bugün zaten kontrol edilmiş ($TODAY). Çıkılıyor."
exit 0
fi
echo "$TODAY" > "$LAST_CHECK_LOG"
fi
# ==========================================
# 5. İNTERNET VE METERED KONTROLÜ
# ==========================================
if ! ping -c 1 -W 3 archlinux.org &> /dev/null; then
echo "[Hata] İnternet bağlantısı yok veya archlinux.org'a ulaşılamıyor. Çıkılıyor."
exit 0
fi
METERED_STATUS=$(nmcli -t -f GENERAL.METERED dev show 2>/dev/null | grep -iE "^yes|^yes \(guessed\)" | head -n 1)
if [ -n "$METERED_STATUS" ]; then
if [ "$IGNORE_METERED" = "1" ]; then
echo "[Bilgi] Sınırlı ağ tespit edildi ancak ayar gereği yoksayılıyor."
elif [ "$IGNORE_METERED" = "0" ]; then
echo "[Bilgi] Sınırlı ağ tespit edildi. Ayar gereği çıkılıyor."
exit 0
else
echo "[Bilgi] Sınırlı ağ için kullanıcıya soruluyor..."
if [ "$LANG_CHECK" = "TR" ]; then
M_TITLE="Sınırlı İnternet Kotası"
M_MSG="Bağlanılan internet sınırlı kotaya sahip yine de güncellemeye devam edilsin mi?"
M_CHK="Bu ayar kaydedilsin mi?"
BTN_Y="Evet"; BTN_N="Hayır"
else
M_TITLE="Metered Connection"
M_MSG="Should updates continue even if your internet connection has a limited data allowance?"
M_CHK="Save this setting?"
BTN_Y="Yes"; BTN_N="No"
fi
YAD_OUT=$(yad --title="$M_TITLE" --image=network-wireless --window-icon=dialog-warning \
--text="$M_MSG\n" --form --field="$M_CHK:CHK" FALSE \
--button="$BTN_Y:0" --button="$BTN_N:1" --center --ontop \
--timeout=60 --timeout-indicator=bottom)
YAD_EXIT=$?
CHK_VAL=$(echo "$YAD_OUT" | awk -F'|' '{print $1}')
if [ "$YAD_EXIT" -eq 0 ]; then
[ "$CHK_VAL" = "TRUE" ] && update_config "IGNORE_METERED" "1"
elif [ "$YAD_EXIT" -eq 1 ]; then
[ "$CHK_VAL" = "TRUE" ] && update_config "IGNORE_METERED" "0"
echo "[Bilgi] Kullanıcı sınırlı ağda güncellemeyi reddetti. Çıkılıyor."
exit 0
else
echo "[Bilgi] Kullanıcı sınırlı ağ uyarısına yanıt vermedi (timeout/cancel). Çıkılıyor."
exit 0
fi
fi
fi
# ==========================================
# 6. PRE-UPDATE KONTROLÜ (GÜNCELLEME VAR MI?)
# ==========================================
echo "[Bilgi] Güncellemeler kontrol ediliyor..."
PACMAN_UPDATES=$(checkupdates 2>/dev/null | wc -l)
AUR_UPDATES=0
if command -v yay &> /dev/null; then AUR_UPDATES=$(yay -Qua 2>/dev/null | wc -l)
elif command -v paru &> /dev/null; then AUR_UPDATES=$(paru -Qua 2>/dev/null | wc -l); fi
TOTAL_UPDATES=$((PACMAN_UPDATES + AUR_UPDATES))
SKIP_UPDATES=0
PACMAN_LOG="/var/log/pacman.log"
PACMAN_LOG_BEFORE=$(wc -l < "$PACMAN_LOG" 2>/dev/null || echo 0)
echo "[Bilgi] Bulunan Güncellemeler: Pacman ($PACMAN_UPDATES), AUR ($AUR_UPDATES)"
if [ "$TOTAL_UPDATES" -eq 0 ]; then
if [ "$PENDING_REBOOT" -eq 1 ]; then
echo "[Bilgi] Güncelleme yok ancak bekleyen yeniden başlatma var."
SKIP_UPDATES=1
else
echo "[Bilgi] Bekleyen güncelleme yok. Çıkılıyor."
exit 0
fi
fi
# ==========================================
# 7. UPDATE DELAY CONTROL (Zaman Uyarıları & Hedef Gün)
# ==========================================
LAST_UPG_STR=$(grep -E "starting full system upgrade" /var/log/pacman.log 2>/dev/null | tail -n 1 | awk -F'[\\[\\]]' '{print $2}')
if [ -n "$LAST_UPG_STR" ]; then
LAST_PACMAN_EPOCH=$(date -d "$LAST_UPG_STR" +%s 2>/dev/null)
else
LAST_PACMAN_EPOCH=$(date +%s)
fi
CURRENT_EPOCH=$(date +%s)
DIFF_SEC=$((CURRENT_EPOCH - LAST_PACMAN_EPOCH))
DIFF_DAYS=$((DIFF_SEC / 86400))
ZAMAN_STR="${DIFF_DAYS} gün"
ZAMAN_STR_EN="${DIFF_DAYS} days"
# --- HAFTANIN BELİRLİ GÜNÜ ÇALIŞTIRMA KORUMASI ---
CURRENT_DOW=$(date +%u)
TARGET_DOW=${TARGET_DOW:-1}
if [[ "$1" != "--retry" && "$1" != "--criticalupdate" ]] && [ "$CURRENT_DOW" -ne "$TARGET_DOW" ]; then
echo "[Bilgi] Bugün hedef güncelleme günü değil (Hedef: $TARGET_DOW, Bugün: $CURRENT_DOW). Çıkılıyor."
exit 0
fi
BLOCK_AUTO=0
WARN_MSG=""
WARN_MSG_EN=""
if [ "$DIFF_DAYS" -ge 1825 ]; then
WARN_MSG="Sistem 5 yıldan fazla olan $ZAMAN_STR süredir güncelleme yapmadı. Sorun yaşanma olasılığı çok yüksek, otomatik güncelleme engellendi. Güncelleme yapmanız önerilmez, önemli dosyaların yedeğini alıp temiz bir Arch Linux kurulumu yapmanız veya rolling release olmayan bir linux dağıtımı kullanmanız önerilir. Güncellemeyi manuel olarak (sudo pacman -Sy archlinux-keyring ve sudo pacman -Syu) başarıyla tamamlarsanız \"arch-updater continue\" yazarak servisin çalışmasına izin verebilirsiniz."
WARN_MSG_EN="The system has not been updated for over 5 years ($ZAMAN_STR_EN). The probability of encountering issues is very high, automatic updates are blocked. Updating is not recommended; it is advised to back up important files and perform a clean Arch Linux installation. If you successfully complete the update manually, you can allow the service to run by typing \"arch-updater continue\"."
BLOCK_AUTO=1
elif [ "$DIFF_DAYS" -ge 1095 ]; then
WARN_MSG="Sistem 3 yıldan fazla olan $ZAMAN_STR süredir güncelleme yapmadı. Sorun yaşanma olasılığı çok yüksek, otomatik güncelleme engellendi. Güncelleme yapmanız önerilmez, önemli dosyaların yedeğini alıp temiz bir Arch Linux kurulumu yapmanız önerilir. Güncellemeyi manuel olarak başarıyla tamamlarsanız \"arch-updater continue\" yazarak servisin çalışmasına izin verebilirsiniz."
WARN_MSG_EN="The system has not been updated for over 3 years ($ZAMAN_STR_EN). The probability of encountering issues is very high, automatic updates are blocked. Updating is not recommended. If you successfully complete the update manually, you can allow the service to run by typing \"arch-updater continue\"."
BLOCK_AUTO=1
elif [ "$DIFF_DAYS" -ge 912 ]; then
WARN_MSG="Sistem 2.5 yıldan fazla olan $ZAMAN_STR süredir güncelleme yapmadı. Sorun yaşanma olasılığı çok yüksek, otomatik güncelleme engellendi. Güncellemeyi manuel olarak başarıyla tamamlarsanız \"arch-updater continue\" yazarak servisin çalışmasına izin verebilirsiniz."
WARN_MSG_EN="The system has not been updated for over 2.5 years ($ZAMAN_STR_EN). Automatic updates are blocked. If you successfully complete the update manually, you can allow the service to run by typing \"arch-updater continue\"."
BLOCK_AUTO=1
elif [ "$DIFF_DAYS" -ge 730 ]; then
WARN_MSG="Sistem 2 yıldan fazla olan $ZAMAN_STR süredir güncelleme yapmadı. Sorun yaşanma olasılığı çok yüksek, otomatik güncelleme engellendi. Güncellemeyi manuel olarak başarıyla tamamlarsanız \"arch-updater continue\" yazarak servisin çalışmasına izin verebilirsiniz."
WARN_MSG_EN="The system has not been updated for over 2 years ($ZAMAN_STR_EN). Automatic updates are blocked. If you successfully complete the update manually, you can allow the service to run by typing \"arch-updater continue\"."
BLOCK_AUTO=1
elif [ "$DIFF_DAYS" -ge 547 ]; then
WARN_MSG="Sistem 1.5 yıldan fazla olan $ZAMAN_STR süredir güncelleme yapmadı. Sorun yaşanma olasılığı çok yüksek, otomatik güncelleme engellendi. Güncellemeyi manuel olarak başarıyla tamamlarsanız \"arch-updater continue\" yazarak servisin çalışmasına izin verebilirsiniz."
WARN_MSG_EN="The system has not been updated for over 1.5 years ($ZAMAN_STR_EN). Automatic updates are blocked. If you successfully complete the update manually, you can allow the service to run by typing \"arch-updater continue\"."
BLOCK_AUTO=1
elif [ "$DIFF_DAYS" -ge 365 ]; then
WARN_MSG="Sistem 1 yıldan fazla olan $ZAMAN_STR süredir güncelleme yapmadı. Sorun yaşanma olasılığı çok yüksek, otomatik güncelleme engellendi. Güncellemeyi manuel olarak başarıyla tamamlarsanız \"arch-updater continue\" yazarak servisin çalışmasına izin verebilirsiniz."
WARN_MSG_EN="The system has not been updated for over 1 year ($ZAMAN_STR_EN). Automatic updates are blocked. If you successfully complete the update manually, you can allow the service to run by typing \"arch-updater continue\"."
BLOCK_AUTO=1
elif [ "$DIFF_DAYS" -ge 270 ]; then
WARN_MSG="Sistem 9 aydan fazla olan $ZAMAN_STR süredir güncelleme yapmadı. Sorun yaşanma olasılığı çok yüksek, otomatik güncelleme engellendi. Güncellemeyi manuel olarak başarıyla tamamlarsanız \"arch-updater continue\" yazarak servisin çalışmasına izin verebilirsiniz."
WARN_MSG_EN="The system has not been updated for over 9 months ($ZAMAN_STR_EN). Automatic updates are blocked. If you successfully complete the update manually, you can allow the service to run by typing \"arch-updater continue\"."
BLOCK_AUTO=1
elif [ "$DIFF_DAYS" -ge 180 ]; then
WARN_MSG="Sistem 6 aydan fazla olan $ZAMAN_STR süredir güncelleme yapmadı. Sorun yaşanma olasılığı yüksek, otomatik güncelleme engellendi. Güncellemeyi manuel olarak başarıyla tamamlarsanız \"arch-updater continue\" yazarak servisin çalışmasına izin verebilirsiniz."
WARN_MSG_EN="The system has not been updated for over 6 months ($ZAMAN_STR_EN). Automatic updates are blocked. If you successfully complete the update manually, you can allow the service to run by typing \"arch-updater continue\"."
BLOCK_AUTO=1
elif [ "$DIFF_DAYS" -ge 90 ]; then
WARN_MSG="Sistem 3 aydan fazla olan $ZAMAN_STR süredir güncelleme yapmadı. Sorun yaşanma olasılığı yüksek, otomatik güncelleme engellendi. Güncellemeyi manuel olarak başarıyla tamamlarsanız \"arch-updater continue\" yazarak servisin çalışmasına izin verebilirsiniz."
WARN_MSG_EN="The system has not been updated for over 3 months ($ZAMAN_STR_EN). Automatic updates are blocked. If you successfully complete the update manually, you can allow the service to run by typing \"arch-updater continue\"."
BLOCK_AUTO=1
elif [ "$DIFF_DAYS" -ge 60 ]; then
WARN_MSG="Sistem 2 aydan fazla olan $ZAMAN_STR süredir güncelleme yapmadı. Sorun yaşanma olasılığı yüksek, güncelleme sonrası loga bakmanız önerilir."
WARN_MSG_EN="The system has not been updated for over 2 months ($ZAMAN_STR_EN). The probability of encountering issues is high; checking the log after updating is recommended."
elif [ "$DIFF_DAYS" -ge 45 ]; then
WARN_MSG="Sistem 1.5 aydan fazla olan $ZAMAN_STR süredir güncelleme yapmadı. Sorun yaşanma olasılığı orta, güncelleme sonrası loga bakmanız önerilir."
WARN_MSG_EN="The system has not been updated for over 1.5 months ($ZAMAN_STR_EN). The probability of encountering issues is medium; checking the log after updating is recommended."
elif [ "$DIFF_DAYS" -ge 30 ]; then
WARN_MSG="Sistem 1 aydan fazla olan $ZAMAN_STR süredir güncelleme yapmadı. Sorun yaşanma olasılığı orta, güncelleme sonrası loga bakmanız önerilir."
WARN_MSG_EN="The system has not been updated for over 1 month ($ZAMAN_STR_EN). The probability of encountering issues is medium; checking the log after updating is recommended."
elif [ "$DIFF_DAYS" -ge 21 ]; then
WARN_MSG="Sistem 3 haftadan fazla olan $ZAMAN_STR süredir güncelleme yapmadı. Sorun yaşanma olasılığı az, güncelleme sonrası loga bakmanız önerilir."
WARN_MSG_EN="The system has not been updated for over 3 weeks ($ZAMAN_STR_EN). The probability of encountering issues is low; checking the log after updating is recommended."
elif [ "$DIFF_DAYS" -ge 14 ]; then
WARN_MSG="Sistem 2 haftadan fazla olan $ZAMAN_STR süredir güncelleme yapmadı. Sorun yaşanma olasılığı az, güncelleme sonrası loga bakmanız önerilir."
WARN_MSG_EN="The system has not been updated for over 2 weeks ($ZAMAN_STR_EN). The probability of encountering issues is low; checking the log after updating is recommended."
elif [ "$DIFF_DAYS" -ge 10 ]; then
WARN_MSG="Sistem 1.5 haftadan fazla olan $ZAMAN_STR süredir güncelleme yapmadı. Sorun yaşanma olasılığı az, güncelleme sonrası loga bakmanız önerilir."
WARN_MSG_EN="The system has not been updated for over 1.5 weeks ($ZAMAN_STR_EN). The probability of encountering issues is low; checking the log after updating is recommended."
fi
if [ -n "$WARN_MSG" ]; then
if [ "$LANG_CHECK" = "TR" ]; then
N_TITLE="⚠️ Güncelleme Uyarısı"
N_MSG="$WARN_MSG"
else
N_TITLE="⚠️ Update Warning"
N_MSG="$WARN_MSG_EN"
fi
if [ "$BLOCK_AUTO" -eq 1 ]; then
echo "[Hata] $DIFF_DAYS gündür güncelleme yapılmadı. Otomatik güncelleme engellendi."
notify-send -a "Arch Updater" -u critical -t 0 "$N_TITLE" "$N_MSG"
touch "$BLOCK_FILE"
exit 1
else
echo "[Uyarı] $DIFF_DAYS gündür güncelleme yapılmadı. Uyarı bildirimi gönderildi."
notify-send -a "Arch Updater" -u normal "$N_TITLE" "$N_MSG"
fi
fi
# ==========================================
# 8. UPDATE BLOCKS (NEWS + TOPGRADE)
# ==========================================
JUST_UPDATED=0
if [ "$SKIP_UPDATES" -eq 0 ]; then
# --- A. NEWS CHECK ---
echo "[Bilgi] Arch Linux haberleri kontrol ediliyor..."
RSS_PUBDATE=$(curl -s "$RSS_FEED_URL" | grep -m 2 -o '<pubDate>.*</pubDate>' | tail -n 1 | sed -e 's/<[^>]*>//g')
if [ -n "$RSS_PUBDATE" ]; then LATEST_NEWS_EPOCH=$(date -d "$RSS_PUBDATE" +%s 2>/dev/null || echo 0); else LATEST_NEWS_EPOCH=0; fi
if [ "$LATEST_NEWS_EPOCH" -gt "$NEWS_ACK_EPOCH" ]; then
if [ "$NEWS_RETRIES" -ge 3 ]; then
echo "[Hata] Haberler 3 kez onaylanmadı. Güvenlik gereği çıkılıyor."
notify-send -a "Arch Updater" -u critical "Güncelleme İptal Edildi" "Haberler onaylanmadığı için sistem güvenliği gereği güncelleme iptal edildi."
echo 0 > "$RETRY_COUNT_FILE"
exit 0
fi
if [ "$LANG_CHECK" = "TR" ]; then
MSG="Sistemin zarar görmemesi için güncelleme durduruldu!\nLütfen manuel müdahale gerektiren haberi okuyun.\n\nTarih: $RSS_PUBDATE"
BTN="Haberi Aç"; TITLE="⚠️ Kritik Arch Haberi Bekliyor"
else
MSG="Updates are paused for your safety!\nPlease read the announcement requiring manual intervention.\n\nDate: $RSS_PUBDATE"
BTN="Open News"; TITLE="⚠️ Critical Arch News Pending"
fi
echo "[Uyarı] Okunmamış Arch haberi bulundu. Kullanıcıdan onay bekleniyor..."
ACTION=$(notify-send -a "Arch Updater" -u critical -t 0 -A "open=$BTN" "$TITLE" "$MSG")
if [ "$ACTION" = "open" ]; then
echo "[Bilgi] Kullanıcı haberi açtı."
xdg-open "$NEWS_PAGE_URL"
echo "$LATEST_NEWS_EPOCH" > "$NEWS_ACK_LOG"
echo 1 > "$POST_UPDATE_FLAG"
echo 0 > "$RETRY_COUNT_FILE"
else
echo "[Bilgi] Kullanıcı haberi açmadı. Deneme sayısı artırılıyor."
echo $((NEWS_RETRIES + 1)) > "$RETRY_COUNT_FILE"
fi
echo "[Bilgi] Sonraki deneme için systemd timer ayarlanıyor..."
systemd-run --user --on-active="2h" /bin/bash "$SCRIPT_PATH" --retry
exit 0
fi
# --- B. SILENT UPDATE ---
if [ "$1" == "--criticalupdate" ]; then
if [ "$LANG_CHECK" = "TR" ]; then
START_TITLE="🚨 Kritik Sistem Güncellemesi"
START_MSG="Kritik güncelleme komutu alındı. Rutin bekleme süresi es geçilerek işlemler arka planda başlatıldı."
else
START_TITLE="🚨 Critical System Update"
START_MSG="Critical update command received. Bypassing wait period, updates have started in the background."
fi
else
if [ "$LANG_CHECK" = "TR" ]; then
START_TITLE="Sistem Güncelleniyor"
START_MSG="Haftalık güvenli güncelleme gününüz geldi! Güncelleme arka planda başladı, lütfen bildirim gelene kadar sistemi kapatmayın."
else
START_TITLE="System Updating"
START_MSG="Your safe weekly update day is here! The update has started in the background; please do not restart until completion."
fi
fi
notify-send -a "Arch Updater" -u critical -t 5000 "$START_TITLE" "$START_MSG"
echo -e "\n=== UPDATE LOG: $(date) ===" > /tmp/arch_updater_topgrade.log
echo "[Bilgi] Topgrade başlatılıyor..."
topgrade -y 2>&1 | tee -a /tmp/arch_updater_topgrade.log > /dev/null
TOPGRADE_EXIT=${PIPESTATUS[0]}
echo "[Bilgi] Topgrade çıkış kodu: $TOPGRADE_EXIT"
# --- BACKUP PROCESS (Last 7 Logs) ---
LOG_BACKUP_DIR="$HOME/.cache/arch-updater/logs"
mkdir -p "$LOG_BACKUP_DIR"
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
cp "/tmp/arch_updater_topgrade.log" "$LOG_BACKUP_DIR/arch_updater_${TIMESTAMP}.log" 2>/dev/null
# shellcheck disable=SC2010
ls -tp "$LOG_BACKUP_DIR"/arch_updater_*.log 2>/dev/null | grep -v '/$' | tail -n +8 | xargs -I {} rm -- "{}" 2>/dev/null
if [ "$TOPGRADE_EXIT" -eq 0 ]; then
echo "[Bilgi] Güncelleme başarıyla tamamlandı."
date +%s > "$LOG_FILE"
JUST_UPDATED=1
# --- C. POST-UPDATE NEWS REMINDER ---
if [ -f "$POST_UPDATE_FLAG" ] && [ "$(cat "$POST_UPDATE_FLAG")" = "1" ]; then
if [ "$LANG_CHECK" = "TR" ]; then
P_TITLE="Güncelleme Başarılı & Aksiyon Gerekli"
P_MSG="Arka plan güncellemesi bitti. Lütfen News'e göre yapılması gereken manuel değişiklikleri tamamlayınız."
else
P_TITLE="Update Successful & Action Required"
P_MSG="Background update complete. Please make any manual changes required by the News."
fi
notify-send -a "Arch Updater" -u critical -t 0 "$P_TITLE" "$P_MSG"
rm -f "$POST_UPDATE_FLAG"
fi
else
echo "[Hata] Topgrade bir hata döndürdü. Detaylar /tmp/arch_updater_topgrade.log dosyasında."
if [ "$LANG_CHECK" = "TR" ]; then
E_TITLE="Güncelleme Başarısız!"
E_MSG="Arka planda çalışırken bir hata oluştu. Lütfen /tmp/arch_updater_topgrade.log dosyasını inceleyin."
else
E_TITLE="Update Failed!"
E_MSG="An error occurred in the background. Check /tmp/arch_updater_topgrade.log."
fi
notify-send -a "Arch Updater" -u critical -t 0 "$E_TITLE" "$E_MSG"
exit 1
fi
fi
# ==========================================
# 9. REBOOT CHECK (Advanced)
# ==========================================
echo "[Bilgi] Yeniden başlatma gereksinimi kontrol ediliyor..."
REBOOT_NEEDED=0
if [ "$SKIP_UPDATES" -eq 0 ]; then
CURRENT_KERNEL=$(uname -r)
# 1. PACMAN LOG CHECK
if [ "$JUST_UPDATED" -eq 1 ]; then
PACMAN_LOG_AFTER=$(wc -l < "$PACMAN_LOG" 2>/dev/null || echo 0)
NEW_LINES=$((PACMAN_LOG_AFTER - PACMAN_LOG_BEFORE))
if [ "$NEW_LINES" -gt 0 ]; then
CRITICAL_PKGS="linux|linux-lts|linux-zen|linux-hardened|linux-firmware.*|.*-ucode|systemd.*|glibc|dbus|nvidia.*|mesa|wayland|sddm|gdm|lightdm"
if tail -n "$NEW_LINES" "$PACMAN_LOG" | grep -iE "\[ALPM\] (upgraded|installed|removed) ($CRITICAL_PKGS) \(" > /dev/null 2>&1; then
echo "[Bilgi] Kritik paket güncellemesi tespit edildi."
REBOOT_NEEDED=1
fi
fi
fi
# 2. SERVICE UPDATE CHECK
if command -v needrestart &> /dev/null; then
(sudo needrestart -b 2>/dev/null | grep -iq "NEEDRESTART-SVC-") && REBOOT_NEEDED=1
fi
# 3. KERNEL MODULE CHECK
if [ ! -d "/usr/lib/modules/$CURRENT_KERNEL" ]; then
echo "[Bilgi] Eski kernel modülleri silinmiş. Reboot gerekiyor."
REBOOT_NEEDED=1
fi
fi
if [ "$REBOOT_NEEDED" -eq 1 ] || [ "$PENDING_REBOOT" -eq 1 ]; then
echo "[Bilgi] Sistemi yeniden başlatmak gerekiyor. Kullanıcıya soruluyor..."
date +%s > "$REBOOT_LOG"
if [ "$LANG_CHECK" = "TR" ]; then
TITLE="Sistem Yeniden Başlatma Gerekli"
MSG="UYARI: Çekirdek (Kernel), donanım yazılımı (Firmware) veya kritik servisler güncellendi. İstikrar için sistemi şimdi yeniden başlatmak ister misiniz?"
BTN="Yeniden Başlat"
else
TITLE="System Reboot Required"
MSG="WARNING: Kernel, firmware, or core services have been updated. Would you like to reboot now for stability?"
BTN="Reboot Now"
fi
ACTION=$(notify-send -u critical -t 15000 -A "reboot=$BTN" "$TITLE" "$MSG")
if [ "$ACTION" = "reboot" ]; then
echo "[Bilgi] Kullanıcı yeniden başlatmayı onayladı."
systemctl reboot
exit 0
else
echo "[Bilgi] Kullanıcı yeniden başlatmayı erteledi. Hatırlatıcı kuruluyor..."
systemd-run --user --on-active="1h" /bin/bash "$SCRIPT_PATH" --retry
fi
else
# --- SUCCESSFUL UPDATE WITH NO REBOOT REQUIRED ---
if [ "$JUST_UPDATED" -eq 1 ]; then
echo "[Bilgi] Yeniden başlatma gerektirmeyen başarılı güncelleme."
if [ "$LANG_CHECK" = "TR" ]; then
SUCCESS_TITLE="Güncelleme Başarılı"
SUCCESS_MSG="Canlı (Live) aynalardan güncellemeler yapıldı. Yeniden başlatma zorunlu değil."
else
SUCCESS_TITLE="Update Successful"
SUCCESS_MSG="Live updates have been applied. Restart is not required."
fi
notify-send -a "Arch Updater" -u normal "$SUCCESS_TITLE" "$SUCCESS_MSG"
fi
fi
echo "[Bilgi] Betik tamamen ve sorunsuz bitti. Çıkılıyor."
exit 0