Files
kindle-calendar/scripts/sync-layered-clock-to-kindle.sh
2026-03-18 13:35:19 +08:00

201 lines
6.5 KiB
Bash

#!/usr/bin/env sh
set -eu
ROOT_DIR="$(CDPATH= cd -- "$(dirname "$0")/.." && pwd)"
KINDLE_TARGET="kindle"
THEME_FILTER=""
ORIENTATION_FILTER=""
CLOCK_REGION_JSON="$ROOT_DIR/calendar/dist/clock-region.json"
print_usage() {
cat <<'EOF'
用法:
sh scripts/sync-layered-clock-to-kindle.sh [host] [选项]
选项:
--theme <theme-id> 只同步指定主题
--orientation <value> 只同步指定方向;必须和 --theme 一起使用
-h, --help 查看帮助
示例:
sh scripts/sync-layered-clock-to-kindle.sh kindle
sh scripts/sync-layered-clock-to-kindle.sh kindle --theme simple
sh scripts/sync-layered-clock-to-kindle.sh kindle --theme simple --orientation portrait
EOF
}
while [ "$#" -gt 0 ]; do
case "$1" in
--theme)
shift
THEME_FILTER=${1:?"missing theme id"}
;;
--orientation)
shift
ORIENTATION_FILTER=${1:?"missing orientation"}
;;
-h|--help)
print_usage
exit 0
;;
*)
KINDLE_TARGET=$1
;;
esac
shift
done
if [ -n "$ORIENTATION_FILTER" ] && [ -z "$THEME_FILTER" ]; then
echo "--orientation 必须和 --theme 一起使用。" >&2
exit 1
fi
cd "$ROOT_DIR/calendar"
if [ -n "$THEME_FILTER" ]; then
if [ -n "$ORIENTATION_FILTER" ]; then
npm run export:themes -- --theme "$THEME_FILTER" --orientation "$ORIENTATION_FILTER" >/dev/null
CLOCK_REGION_JSON="$ROOT_DIR/calendar/dist/themes/$THEME_FILTER/$ORIENTATION_FILTER/kindlebg.clock-region.json"
else
npm run export:themes -- --theme "$THEME_FILTER" >/dev/null
CLOCK_REGION_JSON=""
fi
else
npm run export:themes >/dev/null
fi
cd "$ROOT_DIR"
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
}
sync_dashboard_runtime() {
rsync -av --no-o --no-g \
"$ROOT_DIR/dash/src/start.sh" \
"$ROOT_DIR/dash/src/dash.sh" \
"$ROOT_DIR/dash/src/stop.sh" \
"$ROOT_DIR/dash/src/launch-from-kual.sh" \
"$ROOT_DIR/dash/src/launch-theme-from-kual.sh" \
"$ROOT_DIR/dash/src/switch-theme.sh" \
"$KINDLE_TARGET":/mnt/us/dashboard/
rsync -av --no-o --no-g \
"$ROOT_DIR/dash/src/local/env.sh" \
"$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" \
"$ROOT_DIR/dash/src/local/touch-home-service.sh" \
"$ROOT_DIR/dash/src/local/theme-menu-service.sh" \
"$ROOT_DIR/dash/src/local/theme-json.lua" \
"$ROOT_DIR/dash/src/local/theme-sync.sh" \
"$KINDLE_TARGET":/mnt/us/dashboard/local/
ssh "$KINDLE_TARGET" "chmod +x /mnt/us/dashboard/start.sh /mnt/us/dashboard/dash.sh /mnt/us/dashboard/stop.sh /mnt/us/dashboard/launch-from-kual.sh /mnt/us/dashboard/launch-theme-from-kual.sh /mnt/us/dashboard/switch-theme.sh /mnt/us/dashboard/local/fetch-dashboard.sh /mnt/us/dashboard/local/clock-index.sh /mnt/us/dashboard/local/render-clock.sh /mnt/us/dashboard/local/touch-home-service.sh /mnt/us/dashboard/local/theme-menu-service.sh /mnt/us/dashboard/local/theme-sync.sh"
ssh "$KINDLE_TARGET" "mkdir -p /mnt/us/dashboard/local/state"
ssh "$KINDLE_TARGET" "date '+%s' >/mnt/us/dashboard/local/state/background-updated-at"
}
sync_kual_extension() {
ssh "$KINDLE_TARGET" "mkdir -p /mnt/us/extensions/kindle-dash"
rsync -av --no-o --no-g \
"$ROOT_DIR/dash/KUAL/kindle-dash/" \
"$KINDLE_TARGET":/mnt/us/extensions/kindle-dash/
}
sync_theme_bundle() {
rsync -av --no-o --no-g \
"$ROOT_DIR/calendar/dist/themes.json" \
"$KINDLE_TARGET":/mnt/us/dashboard/
if [ -z "$THEME_FILTER" ]; then
if [ -f "$ROOT_DIR/calendar/dist/kindlebg.png" ]; then
rsync -av --no-o --no-g \
"$ROOT_DIR/calendar/dist/kindlebg.png" \
"$KINDLE_TARGET":/mnt/us/dashboard/
fi
rsync -av --no-o --no-g \
"$ROOT_DIR/calendar/dist/themes/" \
"$KINDLE_TARGET":/mnt/us/dashboard/themes/
return
fi
rsync -av --no-o --no-g \
"$ROOT_DIR/calendar/dist/themes/$THEME_FILTER.json" \
"$KINDLE_TARGET":/mnt/us/dashboard/themes/
if [ -n "$ORIENTATION_FILTER" ]; then
ssh "$KINDLE_TARGET" "mkdir -p /mnt/us/dashboard/themes/$THEME_FILTER/$ORIENTATION_FILTER"
rsync -av --no-o --no-g \
"$ROOT_DIR/calendar/dist/themes/$THEME_FILTER/$ORIENTATION_FILTER/" \
"$KINDLE_TARGET":/mnt/us/dashboard/themes/$THEME_FILTER/$ORIENTATION_FILTER/
return
fi
ssh "$KINDLE_TARGET" "mkdir -p /mnt/us/dashboard/themes/$THEME_FILTER"
rsync -av --no-o --no-g \
"$ROOT_DIR/calendar/dist/themes/$THEME_FILTER/" \
"$KINDLE_TARGET":/mnt/us/dashboard/themes/$THEME_FILTER/
}
update_default_clock_region_env() {
if [ -z "$CLOCK_REGION_JSON" ] || [ ! -f "$CLOCK_REGION_JSON" ]; then
echo "Skipped CLOCK_REGION_* env update: no single background region selected"
return
fi
clock_x=$(clock_region_value x)
clock_y=$(clock_region_value y)
clock_width=$(clock_region_value width)
clock_height=$(clock_region_value height)
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\""
}
sync_dashboard_runtime
sync_theme_bundle
sync_kual_extension
update_default_clock_region_env
if [ -n "$THEME_FILTER" ]; then
if [ -n "$ORIENTATION_FILTER" ]; then
echo "Layered clock runtime synced to $KINDLE_TARGET (theme=$THEME_FILTER orientation=$ORIENTATION_FILTER)"
else
echo "Layered clock runtime synced to $KINDLE_TARGET (theme=$THEME_FILTER)"
fi
else
echo "Layered clock runtime synced to $KINDLE_TARGET"
fi