#!/usr/bin/env bash set -euo pipefail HOST="" REMOTE_ROOT="/opt/lingniu-go-native" DAYS_BACK=1 WINDOW_DAYS=3 BASELINE_LOOKBACK_DAYS=7 PROTOCOLS="GB32960,JT808,YUTONG_MQTT" ON_CALENDAR="*-*-* 01:30:00" DRY_RUN="false" usage() { cat <<'USAGE' Usage: scripts/install-stats-backfill-timer.sh --host root@115.29.187.205 [options] Options: --host HOST SSH target for ECS installation. --remote-root DIR Remote install root. Defaults to /opt/lingniu-go-native. --days-back N Target end date relative to local day. Defaults to 1, meaning yesterday. --window-days N Number of days to recalculate ending at days-back. Defaults to 3. --baseline-lookback-days N Maximum days searched before the target window for a mileage baseline. Defaults to 7. --protocols LIST Comma-separated protocols. Defaults to GB32960,JT808,YUTONG_MQTT. --on-calendar SPEC systemd OnCalendar value. Defaults to "*-*-* 01:30:00". --dry-run Install timer in dry-run mode. -h, --help Show this help. USAGE } while [[ $# -gt 0 ]]; do case "$1" in --host) HOST="${2:-}" shift 2 ;; --remote-root) REMOTE_ROOT="${2:-}" shift 2 ;; --days-back) DAYS_BACK="${2:-}" shift 2 ;; --window-days) WINDOW_DAYS="${2:-}" shift 2 ;; --baseline-lookback-days) BASELINE_LOOKBACK_DAYS="${2:-}" shift 2 ;; --protocols) PROTOCOLS="${2:-}" shift 2 ;; --on-calendar) ON_CALENDAR="${2:-}" shift 2 ;; --dry-run) DRY_RUN="true" shift ;; -h|--help) usage exit 0 ;; *) echo "unknown argument: $1" >&2 usage >&2 exit 2 ;; esac done if [[ -z "$HOST" ]]; then echo "--host is required" >&2 usage >&2 exit 2 fi case "$DAYS_BACK" in ''|*[!0-9]*) echo "--days-back must be a non-negative integer" >&2 exit 2 ;; esac case "$WINDOW_DAYS" in ''|*[!0-9]*|0) echo "--window-days must be a positive integer" >&2 exit 2 ;; esac case "$BASELINE_LOOKBACK_DAYS" in ''|*[!0-9]*|0) echo "--baseline-lookback-days must be a positive integer" >&2 exit 2 ;; esac remote_cmd="" printf -v remote_cmd \ "REMOTE_ROOT=%q DAYS_BACK=%q WINDOW_DAYS=%q BASELINE_LOOKBACK_DAYS=%q PROTOCOLS=%q ON_CALENDAR=%q DRY_RUN=%q bash -s" \ "$REMOTE_ROOT" "$DAYS_BACK" "$WINDOW_DAYS" "$BASELINE_LOOKBACK_DAYS" "$PROTOCOLS" "$ON_CALENDAR" "$DRY_RUN" ssh "$HOST" "$remote_cmd" <<'REMOTE' set -euo pipefail install -d /etc/systemd/system cat >/etc/systemd/system/lingniu-go-stats-backfill.service </etc/systemd/system/lingniu-go-stats-backfill.timer </dev/null || true systemctl enable --now lingniu-go-stats-backfill.timer systemctl list-timers lingniu-go-stats-backfill.timer --no-pager REMOTE