aboutsummaryrefslogtreecommitdiffstats
path: root/bin/updates-sample.sh
blob: 0c9ee3aa58175a7a5e3c770a21f59861468895b4 (plain)
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
#!/bin/bash
# Sample nvtable's package-update table into a cache file, for
# widgets/updates.lua. Replaces the table the login shell used to print.
#
# nvchecker itself is slow (one network request per stanza), so it is started
# in the background at most every 30 minutes, fully detached: conky's execi
# waits for stdout to close, and a child holding it would stall the dashboard.
# nvtable then reads whatever results the last completed run left, so a fresh
# nvchecker run shows up on the next sample.

set -u

CACHE_DIR="${XDG_CACHE_HOME:-$HOME/.cache}/udt"
CACHE="$CACHE_DIR/updates.txt"
STAMP="$CACHE_DIR/nvchecker.stamp"
NVCHECK_EVERY=1800

# conky launched from the compositor does not inherit the login shell's PATH.
PATH="$HOME/bin:$HOME/.local/bin:$PATH"

command -v nvtable >/dev/null || { echo "updates-sample: nvtable not found" >&2; exit 1; }

mkdir -p "$CACHE_DIR"

if command -v nvchecker >/dev/null; then
    last=$(cat "$STAMP" 2>/dev/null || echo 0)
    now=$(date +%s)
    if (( now - ${last:-0} >= NVCHECK_EVERY )); then
        echo "$now" > "$STAMP"
        setsid nvchecker </dev/null >/dev/null 2>&1 &
    fi
fi

TMP="$CACHE.tmp.$$"
trap 'rm -f "$TMP"' EXIT

# -C and -E: plain ASCII, which the widget parses and colours itself.
# Keep the old cache when nvtable fails, rather than replacing it with nothing.
nvtable -C -E > "$TMP" 2>/dev/null && mv -f "$TMP" "$CACHE"