Files
kindle-calendar/scripts/sync-layered-clock-to-kindle.sh
2026-03-16 09:00:35 +08:00

71 lines
2.4 KiB
Bash

#!/usr/bin/env sh
set -eu
ROOT_DIR="$(CDPATH= cd -- "$(dirname "$0")/.." && pwd)"
KINDLE_TARGET=${1:-kindle}
CLOCK_REGION_JSON="$ROOT_DIR/calendar/dist/clock-region.json"
clock_region_value() {
key=$1
python3 - "$CLOCK_REGION_JSON" "$key" <<'PY'
import json
import pathlib
import sys
path = pathlib.Path(sys.argv[1])
key = sys.argv[2]
if not path.exists():
defaults = {"x": 262, "y": 55, "width": 220, "height": 220}
print(defaults[key])
raise SystemExit(0)
data = json.loads(path.read_text())
print(data[key])
PY
}
clock_x=$(clock_region_value x)
clock_y=$(clock_region_value y)
clock_width=$(clock_region_value width)
clock_height=$(clock_region_value height)
rsync -av --no-o --no-g \
"$ROOT_DIR/dash/src/dash.sh" \
"$KINDLE_TARGET":/mnt/us/dashboard/
rsync -av --no-o --no-g \
"$ROOT_DIR/dash/src/local/fetch-dashboard.sh" \
"$ROOT_DIR/dash/src/local/clock-index.sh" \
"$ROOT_DIR/dash/src/local/render-clock.lua" \
"$ROOT_DIR/dash/src/local/render-clock.sh" \
"$KINDLE_TARGET":/mnt/us/dashboard/local/
rsync -av --no-o --no-g \
"$ROOT_DIR/calendar/dist/kindlebg.png" \
"$KINDLE_TARGET":/mnt/us/dashboard/
ssh "$KINDLE_TARGET" "chmod +x /mnt/us/dashboard/dash.sh /mnt/us/dashboard/local/fetch-dashboard.sh /mnt/us/dashboard/local/clock-index.sh /mnt/us/dashboard/local/render-clock.sh"
ssh "$KINDLE_TARGET" "mkdir -p /mnt/us/dashboard/local/state"
ssh "$KINDLE_TARGET" "date '+%s' >/mnt/us/dashboard/local/state/background-updated-at"
ssh "$KINDLE_TARGET" "tmp=\$(mktemp) && awk \
-v x='$clock_x' \
-v y='$clock_y' \
-v w='$clock_width' \
-v h='$clock_height' \
'BEGIN { seen_x=0; seen_y=0; seen_w=0; seen_h=0 } \
/^export CLOCK_REGION_X=/ { print \"export CLOCK_REGION_X=\" x; seen_x=1; next } \
/^export CLOCK_REGION_Y=/ { print \"export CLOCK_REGION_Y=\" y; seen_y=1; next } \
/^export CLOCK_REGION_WIDTH=/ { print \"export CLOCK_REGION_WIDTH=\" w; seen_w=1; next } \
/^export CLOCK_REGION_HEIGHT=/ { print \"export CLOCK_REGION_HEIGHT=\" h; seen_h=1; next } \
{ print } \
END { \
if (!seen_x) print \"export CLOCK_REGION_X=\" x; \
if (!seen_y) print \"export CLOCK_REGION_Y=\" y; \
if (!seen_w) print \"export CLOCK_REGION_WIDTH=\" w; \
if (!seen_h) print \"export CLOCK_REGION_HEIGHT=\" h; \
}' /mnt/us/dashboard/local/env.sh >\"\$tmp\" && cat \"\$tmp\" >/mnt/us/dashboard/local/env.sh && rm -f \"\$tmp\""
echo "Layered clock runtime synced to $KINDLE_TARGET"