#!/bin/sh
# NetworkManager dispatcher: the list of known vulnerabilities is due as soon as the phone reaches the
# internet, not only at the next run of the timer. A new phone would otherwise show an empty
# list for hours. "up" also covers systems whose connectivity check is switched off.
case "$2:${CONNECTIVITY_STATE:-}" in
connectivity-change:FULL | up:*) ;;
*) exit 0 ;;
esac

# The kernel list belongs to one kernel release; after a kernel update or a start of the fallback
# kernel it is due at once, not with the next timer run. A kernel the server has no list for is
# asked once per start: plinius-cve-update notes the server's answer with the boot. A download
# that did not reach the server (no DNS yet right after "up") leaves no mark, so the next
# connection tries again.
kernel_list=/var/lib/plinius-cve/kernel-cves.json
unlisted=/var/lib/plinius-cve/kernel-unlisted
release=$(uname -r)
if ! grep -qF "\"release\":\"$release\"" "$kernel_list" 2>/dev/null &&
	[ "$(cat "$unlisted" 2>/dev/null)" != "$release $(cat /proc/sys/kernel/random/boot_id)" ]; then
	exec systemctl start --no-block plinius-cve-update.service
fi

# Written by a successful download only, so that a failed one is tried again at the next connection;
# older than the timer's six hours, the phone missed a run while offline
list=/var/lib/plinius-cve/arch-issues.json
[ -z "$(find "$list" -mmin -360 2>/dev/null)" ] || exit 0
systemctl start --no-block plinius-cve-update.service
