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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
|
#!/bin/bash
# Helper functions for the Hibbian build scripts. This file expects callers to
# define the shared configuration variables they need before sourcing it.
log_step() {
local message="==== $(date) $* ===="
if [[ "${LOG_TO_STDOUT:-0}" == "1" ]]; then
echo "$message" | tee -a "$LOGFILE"
else
echo "$message" >> "$LOGFILE"
fi
}
ensure_git_repo_present() {
local repo_url="$1"
local repo_dir="$2"
if [[ -d "$repo_dir/.git" ]]; then
return 0
fi
if [[ -e "$repo_dir" ]]; then
echo "ERROR: $repo_dir exists but is not a git repository"
exit 1
fi
echo "Cloning $repo_url into $repo_dir..."
git clone "$repo_url" "$repo_dir"
}
ensure_git_repo_updated() {
local repo_url="$1"
local repo_dir="$2"
local log_file="$3"
if [[ -d "$repo_dir/.git" ]]; then
echo "Updating existing repo..." >> "$log_file"
git -C "$repo_dir" pull --ff-only >> "$log_file" 2>&1
elif [[ -e "$repo_dir" ]]; then
echo "ERROR: $repo_dir exists but is not a git repository" >> "$log_file"
exit 1
else
echo "Cloning repo..." >> "$log_file"
git clone "$repo_url" "$repo_dir" >> "$log_file" 2>&1
fi
}
ensure_pi_gen() {
ensure_git_repo_present "$PI_GEN_REPO" "$PI_GEN_DIR"
}
get_today_build() {
local dir="$1"
local suffix="$2"
local today
today=$(date +%Y-%m-%d)
for f in "$dir"/"${today}"-*"$suffix"; do
[[ -e "$f" && -s "$f" ]] && { echo "$f"; return 0; }
done
return 1
}
find_single_artifact() {
local dir="$1"
local pattern="$2"
local description="$3"
local artifacts=()
mapfile -t artifacts < <(find "$dir" -maxdepth 1 -type f -name "$pattern" -print 2>/dev/null | sort)
if (( ${#artifacts[@]} == 0 )); then
echo "ERROR: No $description produced" >> "$LOGFILE"
return 1
fi
if (( ${#artifacts[@]} > 1 )); then
echo "ERROR: Multiple $description files found; refusing to guess which one to publish" >> "$LOGFILE"
printf ' %s\n' "${artifacts[@]}" >> "$LOGFILE"
return 1
fi
printf '%s\n' "${artifacts[0]}"
}
has_today_remote_arm64() {
ssh "$REMOTE" "
for f in $REMOTE_DIR/$ZIP_NAME; do
[ -e \"\$f\" ] && [ -s \"\$f\" ] && exit 0
done
exit 1
"
}
generate_links_notes() {
# Create/update "latest" symlink
ln -sfn "$FINAL_NAME" "$AMD64_DIR/hibbian-latest.iso"
ln -sfn "$FINAL_NAME.sha256" "$AMD64_DIR/hibbian-latest.iso.sha256"
# Generate release notes
generate_release_notes
ln -sfn "${FINAL_NAME%.iso}-packages.txt" "$AMD64_DIR/hibbian-latest-packages.txt"
ln -sfn "${FINAL_NAME%.iso}-release-notes.txt" "$AMD64_DIR/hibbian-latest-release-notes.txt"
ln -sfn "${FINAL_NAME%.iso}-changes.txt" "$AMD64_DIR/hibbian-latest-changes.txt"
}
ensure_all_diffs() {
echo "Checking diffs..."
# Collect package files (oldest → newest)
mapfile -t FILES < <(ls -1 "$OUTPUT_DIR/amd64"/*-packages.txt 2>/dev/null | sort)
# Need at least 2 files to compare
if (( ${#FILES[@]} < 2 )); then
echo "Not enough package lists to generate diffs"
return
fi
# Iterate safely: previous → current
for ((i=1; i<${#FILES[@]}; i++)); do
PREVIOUS="${FILES[$((i-1))]}"
CURRENT="${FILES[$i]}"
# Skip if either file is missing or empty
[[ ! -s "$PREVIOUS" || ! -s "$CURRENT" ]] && {
echo "Skipping empty/missing pair:"
echo " PREV: $PREVIOUS"
echo " CURR: $CURRENT"
continue
}
base=$(basename "$CURRENT" -packages.txt)
DIFF_FILE="$AMD64_DIR/${base}-changes.txt"
# Skip if already exists
[[ -f "$DIFF_FILE" ]] && continue
echo "Generating diff for $base..."
# Use temp files (safe + debuggable)
TMP_PREV=$(mktemp)
TMP_CURR=$(mktemp)
sort "$PREVIOUS" > "$TMP_PREV"
sort "$CURRENT" > "$TMP_CURR"
{
echo "Changes since last build"
echo "========================"
echo ""
echo "Previous: $(basename "$PREVIOUS")"
echo "Current: $(basename "$CURRENT")"
echo ""
echo "New packages:"
comm -13 "$TMP_PREV" "$TMP_CURR" || true
echo ""
echo "Removed packages:"
comm -23 "$TMP_PREV" "$TMP_CURR" || true
echo ""
echo "Version changes:"
join -j1 "$TMP_PREV" "$TMP_CURR" \
| awk '$2 != $3 { printf "%-30s %s -> %s\n", $1, $2, $3 }'
} > "$DIFF_FILE"
rm -f "$TMP_PREV" "$TMP_CURR"
done
}
rebuild_metrics() {
METRICS_FILE="$OUTPUT_DIR/build-history.csv"
> "$METRICS_FILE"
for pkg in "$OUTPUT_DIR"/amd64/*-packages.txt; do
base=$(basename "$pkg" -packages.txt)
DATE=$(date -r "$pkg" "+%Y-%m-%d %H:%M")
PKG_COUNT=$(wc -l < "$pkg" 2>/dev/null || echo 0)
CHANGES_FILE="$AMD64_DIR/${base}-changes.txt"
UPDATED_COUNT=$(grep -c "->" "$CHANGES_FILE" 2>/dev/null || echo 0)
echo "$DATE,$base,$PKG_COUNT,$UPDATED_COUNT" >> "$METRICS_FILE"
done
}
generate_index() {
INDEX_FILE="$OUTPUT_DIR/index.html"
echo "Generating HTML index..."
cat > "$INDEX_FILE" <<EOF
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hibbian Downloads</title>
<style>
body { font-family: Arial, sans-serif; margin: 40px; }
h1 { color: #333; }
table { border-collapse: collapse; width: 100%; }
th, td { padding: 10px; border-bottom: 1px solid #ccc; text-align: left; }
a { text-decoration: none; color: #0066cc; }
ul { list-style: none; padding: 0; }
li { margin-bottom: 10px; }
strong { color: #333; }
code {
font-family: monospace;
font-size: 0.9em;
background: #f4f4f4;
padding: 2px 4px;
}
</style>
</head>
<body>
<h1>Hibbian Weekly Builds</h1>
EOF
echo "<h2>Latest Downloads</h2>" >> "$INDEX_FILE"
echo "<ul>" >> "$INDEX_FILE"
# --- AMD64 ---
LATEST_AMD=$(readlink "$AMD64_DIR/hibbian-latest.iso" 2>/dev/null || true)
if [[ -n "$LATEST_AMD" ]]; then
size=$(get_size "$AMD64_DIR/$LATEST_AMD")
checksum_link=$( [[ -f "$AMD64_DIR/hibbian-latest.iso.sha256" ]] && echo "<a href=\"amd64/hibbian-latest.iso.sha256\">checksum</a>" || echo "-" )
notes_link=$( [[ -f "$AMD64_DIR/hibbian-latest-release-notes.txt" ]] && echo "<a href=\"amd64/hibbian-latest-release-notes.txt\">notes</a>" || echo "-" )
changes_link=$( [[ -f "$AMD64_DIR/hibbian-latest-changes.txt" ]] && echo "<a href=\"amd64/hibbian-latest-changes.txt\">changes</a>" || echo "-" )
echo "<li>
<strong>PC (amd64):</strong>
<a href=\"amd64/hibbian-latest.iso\">Download ($size)</a> |
$checksum_link |
$notes_link |
$changes_link
</li>" >> "$INDEX_FILE"
fi
# --- ARM64 ---
LATEST_ARM=$(readlink "$ARM64_DIR/hibbian-latest.img.zip" 2>/dev/null || true)
if [[ -n "$LATEST_ARM" ]]; then
arm_path="$ARM64_DIR/$LATEST_ARM"
arm_base="${LATEST_ARM%.img.zip}"
build_date=$(extract_date "$LATEST_ARM")
size=$(get_size "$arm_path")
checksum_link=$( [[ -f "$OUTPUT_DIR/arm64/${arm_base}.img.sha256" ]] && echo "<a href=\"arm64/${arm_base}.img.sha256\">checksum</a>" || echo "-" )
# Match amd64 metadata
amd64_iso=$(find_matching_amd64 "$build_date")
if [[ -n "$amd64_iso" ]]; then
amd_base=$(basename "$amd64_iso" .iso)
notes_link="<a href=\"amd64/${amd_base}-release-notes.txt\">notes</a>"
changes_link="<a href=\"amd64/${amd_base}-changes.txt\">changes</a>"
else
notes_link="-"
changes_link="-"
fi
echo "<li>
<strong>Raspberry Pi (arm64):</strong>
<a href=\"arm64/$LATEST_ARM\">Download ($size, zipped)</a> |
$checksum_link |
$notes_link |
$changes_link
</li>" >> "$INDEX_FILE"
fi
get_latest_summary
cat >> "$INDEX_FILE" <<EOF
<div style="border:1px solid #ccc; padding:15px; margin-bottom:20px; border-radius:8px; background:#f9f9f9;">
<h2>Latest Changes Summary</h2>
<p>
🆕 New: $NEW_COUNT
🔄 Updated: $UPDATED_COUNT
(Major: $MAJOR_COUNT, Minor: $MINOR_COUNT, Patch: $PATCH_COUNT)
❌ Removed: $REMOVED_COUNT
</p>
<pre style="background:#fff; padding:10px; border:1px solid #eee;">
$TOP_CHANGES
</pre>
</div>
<h2>PC (AMD64)</h2>
<table>
<tr>
<th>Filename</th>
<th>Checksum</th>
<th>Date</th>
<th>Release Notes</th>
<th>Changes</th>
</tr>
EOF
LATEST_AMD=$(readlink "$OUTPUT_DIR/amd64/hibbian-latest.iso" 2>/dev/null || true)
for iso in "$OUTPUT_DIR"/amd64/*.iso; do
[[ -e "$iso" ]] || continue
fname=$(basename "$iso")
base="${fname%.iso}"
checksum_file="$OUTPUT_DIR/amd64/$fname.sha256"
notes_file="$OUTPUT_DIR/amd64/${base}-release-notes.txt"
changes_file="$OUTPUT_DIR/amd64/${base}-changes.txt"
date_file="$iso"
if [[ -L "$iso" ]]; then
date_file="$(dirname "$iso")/$(readlink "$iso")"
fi
mod_date=$(date -r "$date_file" "+%Y-%m-%d")
# Latest label
if [[ "$fname" == "$LATEST_AMD" ]]; then
label=" <span class=\"latest\">(latest)</span>"
else
label=""
fi
# Checksum
if [[ -f "$checksum_file" ]]; then
checksum=$(cut -d ' ' -f1 "$checksum_file")
checksum_display="<code>$checksum</code><br><a href=\"amd64/$fname.sha256\">download</a>"
else
checksum_display="-"
fi
# Links
notes_link=$( [[ -f "$notes_file" ]] && echo "<a href=\"amd64/${base}-release-notes.txt\">notes</a>" || echo "-" )
changes_link=$( [[ -f "$changes_file" ]] && echo "<a href=\"amd64/${base}-changes.txt\">changes</a>" || echo "-" )
echo "<tr>
<td><a href=\"amd64/$fname\">$fname</a>$label</td>
<td>$checksum_display</td>
<td>$mod_date</td>
<td>$notes_link</td>
<td>$changes_link</td>
</tr>" >> "$INDEX_FILE"
done
echo "</table>" >> "$INDEX_FILE"
echo "<h2>arm64 (Raspberry Pi)</h2>" >> "$INDEX_FILE"
echo "<table>
<tr>
<th>File</th>
<th>Checksum</th>
<th>Date</th>
<th>Notes</th>
<th>Changes</th>
</tr>" >> "$INDEX_FILE"
LATEST_ARM=$(readlink "$OUTPUT_DIR/arm64/hibbian-latest.img.zip" 2>/dev/null || true)
for img in "$OUTPUT_DIR"/arm64/*.img.zip; do
[[ -e "$img" ]] || continue
fname=$(basename "$img")
base="${fname%.img.zip}"
checksum_file="$OUTPUT_DIR/arm64/$base.img.sha256"
build_date=$(extract_date "$fname")
date_file="$img"
if [[ -L "$img" ]]; then
date_file="$(dirname "$img")/$(readlink "$img")"
fi
mod_date=$(date -r "$date_file" "+%Y-%m-%d")
# Latest label
if [[ "$fname" == "$LATEST_ARM" ]]; then
label=" <span class=\"latest\">(latest)</span>"
else
label=""
fi
# Checksum
if [[ -f "$checksum_file" ]]; then
checksum=$(cut -d ' ' -f1 "$checksum_file")
checksum_display="<code>$checksum</code><br><a href=\"arm64/$base.img.sha256\">download</a>"
else
checksum_display="-"
fi
# Find matching amd64 build
amd64_iso=$(find_matching_amd64 "$build_date")
if [[ -n "$amd64_iso" ]]; then
amd64_base=$(basename "$amd64_iso" .iso)
notes_link="<a href=\"amd64/${amd64_base}-release-notes.txt\">notes</a>"
changes_link="<a href=\"amd64/${amd64_base}-changes.txt\">changes</a>"
else
notes_link="-"
changes_link="-"
fi
echo "<tr>
<td><a href=\"arm64/$fname\">$fname</a>$label</td>
<td>$checksum_display</td>
<td>$mod_date</td>
<td>$notes_link</td>
<td>$changes_link</td>
</tr>" >> "$INDEX_FILE"
done
cat >> "$INDEX_FILE" <<EOF
</table>
<h2>Build Trends</h2>
<pre>
EOF
tail -n 10 "$OUTPUT_DIR/build-history.csv" >> "$INDEX_FILE"
cat >> "$INDEX_FILE" <<EOF
</pre>
</body>
</html>
EOF
}
has_today_build() {
local dir="$1"
local suffix="$2"
get_today_build "$dir" "$suffix" >/dev/null
}
get_size() {
local file="$1"
du -h "$file" 2>/dev/null | cut -f1
}
find_matching_amd64() {
local date="$1"
if [[ -z "$date" ]]; then
return 0
fi
find "$OUTPUT_DIR/amd64" -maxdepth 1 -type f -name "${date}-Hibbian-amd64.iso" -print -quit 2>/dev/null || true
}
generate_release_notes() {
NOTES_FILE="$AMD64_DIR/${FINAL_NAME%.iso}-release-notes.txt"
PKG_FILE="$AMD64_DIR/${FINAL_NAME%.iso}-packages.txt"
echo "Generating release notes for $FINAL_NAME..."
# Locate chroot dynamically
CHROOT_DIR="${CHROOT_DIR:-$WORKDIR/config/chroot}"
if [[ ! -d "$CHROOT_DIR" ]]; then
echo "⚠️ No chroot found, cannot generate package list"
return
fi
sudo chroot "$CHROOT_DIR" dpkg-query -W -f='${Package} ${Version} ${Section}\n' \
| awk '$3 ~ /^hamradio/ { print $1, $2 }' \
| sort > "$PKG_FILE"
{
echo "Hibbian Live Build"
echo "=================="
echo ""
echo "ISO: $FINAL_NAME"
echo "Generated: $(date)"
echo ""
echo "Hamradio Packages"
echo "-----------------"
cat "$PKG_FILE"
} > "$NOTES_FILE"
}
get_latest_summary() {
LATEST_CHANGES=$(find "$OUTPUT_DIR/amd64" -maxdepth 1 -type f -name '*-changes.txt' -printf '%T@ %p\n' 2>/dev/null | sort -nr | head -n 1 | cut -d' ' -f2- || true)
if [[ -z "$LATEST_CHANGES" || ! -s "$LATEST_CHANGES" ]]; then
NEW_COUNT=0
REMOVED_COUNT=0
UPDATED_COUNT=0
MAJOR_COUNT=0
MINOR_COUNT=0
PATCH_COUNT=0
TOP_CHANGES="No data yet"
return
fi
# Single-pass awk for all counts + classification
read -r NEW_COUNT REMOVED_COUNT UPDATED_COUNT MAJOR_COUNT MINOR_COUNT PATCH_COUNT < <(
awk '
function splitver(v, a, tmp) {
# Remove epoch if present (e.g. 1:2.3.4)
split(v, tmp, ":")
v = tmp[length(tmp)]
# Remove Debian revision (-1, -2, etc.)
split(v, tmp, "-")
v = tmp[1]
# Split into version parts
split(v, a, ".")
}
BEGIN {
section=""
new=0
removed=0
updated=0
major=0
minor=0
patch=0
}
/^New packages:/ { section="new"; next }
/^Removed packages:/ { section="removed"; next }
/^Version changes:/ { section="updated"; next }
/^$/ { section=""; next }
{
if (section == "new" && NF) {
new++
}
else if (section == "removed" && NF) {
removed++
}
else if (section == "updated" && /->/) {
updated++
oldv = $2
newv = $4
splitver(oldv, o)
splitver(newv, n)
if (o[1] != n[1]) major++
else if (o[2] != n[2]) minor++
else if (o[3] != n[3]) patch++
}
}
END {
print new, removed, updated, major, minor, patch
}
' "$LATEST_CHANGES"
)
# Top changes (prioritise major/minor first, fallback to any)
TOP_CHANGES=$(awk '
function splitver(v, a, tmp) {
split(v, tmp, ":"); v = tmp[length(tmp)]
split(v, tmp, "-"); v = tmp[1]
split(v, a, ".")
}
/^Version changes:/ { flag=1; next }
/^$/ { flag=0 }
flag && /->/ {
oldv = $2
newv = $4
splitver(oldv, o)
splitver(newv, n)
if (o[1] != n[1] || o[2] != n[2]) {
print
count++
if (count == 5) exit
}
}
' "$LATEST_CHANGES")
# Fallback to any changes if no major/minor found
if [[ -z "$TOP_CHANGES" ]]; then
TOP_CHANGES=$(awk '
/^Version changes:/ { flag=1; next }
/^$/ { flag=0 }
flag && /->/ {
print
count++
if (count == 5) exit
}
' "$LATEST_CHANGES")
fi
if [[ -z "$TOP_CHANGES" ]]; then
TOP_CHANGES="No version changes"
fi
return 0
}
prune_old_builds() {
echo "Pruning old builds (keeping latest 2 images per architecture)..."
# Keep the two newest dated amd64 ISOs. The hibbian-latest symlinks do not
# match this pattern, so they are preserved.
mapfile -t AMD64_ISOS < <(ls -t "$AMD64_DIR"/[0-9][0-9][0-9][0-9]-*.iso 2>/dev/null)
for iso in "${AMD64_ISOS[@]:2}"; do
base=$(basename "$iso" .iso)
echo "Removing old amd64 build: $base"
rm -f "$AMD64_DIR/$base.iso"
rm -f "$AMD64_DIR/$base.iso.sha256"
rm -f "$AMD64_DIR/$base-packages.txt"
rm -f "$AMD64_DIR/$base-release-notes.txt"
rm -f "$AMD64_DIR/$base-changes.txt"
done
# Keep the two newest dated arm64 images. The hibbian-latest symlink does not
# match this pattern, so it is preserved.
mapfile -t ARM64_IMAGES < <(ls -t "$ARM64_DIR"/[0-9][0-9][0-9][0-9]-*.img.zip 2>/dev/null)
for image in "${ARM64_IMAGES[@]:2}"; do
base=$(basename "$image" .img.zip)
echo "Removing old arm64 build: $base"
rm -f "$ARM64_DIR/$base.img.zip"
rm -f "$ARM64_DIR/$base.img.sha256"
rm -f "$ARM64_DIR/$base-packages.txt"
rm -f "$ARM64_DIR/$base-release-notes.txt"
rm -f "$ARM64_DIR/$base-changes.txt"
done
}
verify_remote() {
echo "Verifying remote artefacts..."
if [[ "$REMOTE_TARGET" != *:* ]]; then
echo "❌ ERROR: REMOTE_TARGET must be in user@host:/path format"
return 1
fi
local remote_host="${REMOTE_TARGET%%:*}"
local remote_path="${REMOTE_TARGET#*:}"
local remote_path_q
local remote_file_script
local remote_link_script
local remote_file_command
local remote_link_command
printf -v remote_path_q "%q" "${remote_path%/}"
remote_file_script="cd $remote_path_q && find . -type f -print | LC_ALL=C sort | while IFS= read -r file; do sha256sum \"\$file\"; done"
remote_link_script="cd $remote_path_q && find . -type l -print | LC_ALL=C sort | while IFS= read -r link; do printf \"%s -> %s\\n\" \"\$link\" \"\$(readlink \"\$link\")\"; done"
remote_file_command="'${remote_file_script//\'/\'\\\'\'}'"
remote_link_command="'${remote_link_script//\'/\'\\\'\'}'"
local local_files
local remote_files
local local_links
local remote_links
local_files=$(mktemp)
remote_files=$(mktemp)
local_links=$(mktemp)
remote_links=$(mktemp)
(
cd "$OUTPUT_DIR"
find . -type f -print | LC_ALL=C sort | while IFS= read -r file; do
sha256sum "$file"
done
) > "$local_files"
if ! ssh "$remote_host" "bash -lc $remote_file_command" > "$remote_files"; then
echo "❌ ERROR: Remote checksum collection failed"
rm -f "$local_files" "$remote_files" "$local_links" "$remote_links"
return 1
fi
if ! diff -u "$local_files" "$remote_files"; then
echo "❌ ERROR: Remote file checksums do not match local output"
rm -f "$local_files" "$remote_files" "$local_links" "$remote_links"
return 1
fi
(
cd "$OUTPUT_DIR"
find . -type l -print | LC_ALL=C sort | while IFS= read -r link; do
printf "%s -> %s\n" "$link" "$(readlink "$link")"
done
) > "$local_links"
if ! ssh "$remote_host" "bash -lc $remote_link_command" > "$remote_links"; then
echo "❌ ERROR: Remote symlink collection failed"
rm -f "$local_files" "$remote_files" "$local_links" "$remote_links"
return 1
fi
if ! diff -u "$local_links" "$remote_links"; then
echo "❌ ERROR: Remote symlinks do not match local output"
rm -f "$local_files" "$remote_files" "$local_links" "$remote_links"
return 1
fi
rm -f "$local_files" "$remote_files" "$local_links" "$remote_links"
echo "✅ Remote verification successful"
}
run_stats() {
echo "Regenerating stats (best effort)..."
ensure_all_diffs # only depends on packages.txt
rebuild_metrics # skips missing safely
generate_index # renders whatever exists
}
run_sync() {
echo "Starting sync to remote..."
# Safety check
if [[ -z "${REMOTE_TARGET:-}" ]]; then
echo "ERROR: REMOTE_TARGET not set"
return 1
fi
if [[ ! -d "$OUTPUT_DIR" ]]; then
echo "ERROR: OUTPUT_DIR does not exist"
return 1
fi
# Perform rsync (mirror mode)
rsync -avz --delete "$OUTPUT_DIR/" "$REMOTE_TARGET"
# Optional: verify after sync
verify_remote || {
echo "ERROR: Remote verification failed"
return 1
}
echo "Sync complete"
}
clean_pi_gen_work() {
# pi-gen leaves a multi-GB "work/" tree behind after a build (one subdir per
# stage, each containing a full rootfs). The only artifacts we keep live in
# deploy/, so wipe everything else the build generated to reclaim disk.
# work/ is created by the sudo build, so it is root-owned.
local pi_gen_dir="$1"
if [[ -d "$pi_gen_dir/work" ]]; then
echo "Removing pi-gen work directory: $pi_gen_dir/work"
sudo rm -rf "$pi_gen_dir/work"
fi
}
prune_old_images() {
# Keep the newest N dated images in a directory, deleting older ones along
# with their sidecar checksum files. Filenames are expected to start with a
# YYYY-MM-DD date so the glob never matches "hibbian-latest"-style symlinks.
local dir="$1"
local keep="${2:-2}"
mapfile -t images < <(ls -t "$dir"/[0-9][0-9][0-9][0-9]-*.img 2>/dev/null)
for image in "${images[@]:$keep}"; do
echo "Removing old image: $(basename "$image")"
rm -f "$image" "$image.sha256"
done
}
extract_date() {
local name="$1"
if [[ "$name" =~ ^([0-9]{4}-[0-9]{2}-[0-9]{2}) ]]; then
echo "${BASH_REMATCH[1]}"
fi
return 0
}
# Discover a local APT caching proxy using Zeroconf.
# Resolve to IPv4 on the host to avoid mDNS dependencies in chroots.
# Honour an explicitly supplied APT_PROXY, including an empty value.
if [ -z "${APT_PROXY+x}" ]; then
APT_PROXY=""
if command -v avahi-browse >/dev/null 2>&1 &&
command -v curl >/dev/null 2>&1; then
APT_PROXY="$(avahi-browse -prt _apt_proxy._tcp 2>/dev/null |
awk -F';' '$1 == "=" && $3 == "IPv4" && !found {
print "http://" $8 ":" $9
found=1
}')" || APT_PROXY=""
# Verify the discovered cache is reachable.
if [ -n "$APT_PROXY" ]; then
if ! curl -fsS --connect-timeout 2 --max-time 5 \
"$APT_PROXY/acng-report.html" >/dev/null 2>&1; then
APT_PROXY=""
fi
fi
fi
fi
if [ "$APT_PROXY" = "DIRECT" ]; then
APT_PROXY=""
fi
export APT_PROXY
echo "APT proxy: ${APT_PROXY:-Direct connection}"
|