From ca02357b38f85612a2d8721bd5c3e66d0b8f6062 Mon Sep 17 00:00:00 2001 From: wytch Date: Sat, 28 Feb 2026 00:40:54 -0600 Subject: [PATCH] Hopes and dreams --- savesync.conf => old/savesync.conf | 0 savesync[rungame,endgame](sync).ash | 36 +++++++++++++------ ...nent).ash => slogger[start](permanent).ash | 0 3 files changed, 25 insertions(+), 11 deletions(-) rename savesync.conf => old/savesync.conf (100%) rename sslogger[start](permanent).ash => slogger[start](permanent).ash (100%) diff --git a/savesync.conf b/old/savesync.conf similarity index 100% rename from savesync.conf rename to old/savesync.conf diff --git a/savesync[rungame,endgame](sync).ash b/savesync[rungame,endgame](sync).ash index fb059c9..6225ac5 100644 --- a/savesync[rungame,endgame](sync).ash +++ b/savesync[rungame,endgame](sync).ash @@ -2,43 +2,58 @@ #shellcheck shell=dash # -PAYLOAD=$(cat /tmp/es_state.inf) +event_data=$(cat /tmp/es_state.inf) + REMOTE_BASE="saves:gamepi-tv" PIPE="/tmp/savesync_pipe" +#DEBUG=1 log() { # $1 = level # $2 = message - printf "LOG:[%s] %s" "$1" "$2" >"$PIPE" + printf "LOG:[%s] %s\n" "$1" "$2" >"$PIPE" } -log "INFO" "Event received: $PAYLOAD" -# Process the multi-line event data -# We use echo and a pipe to feed the while loop -echo "$PAYLOAD" | tr -d '\r' | while IFS="=" read -r key val; do +log "INFO" "Starting based on event" + +## Iterate over the data +echo "$event_data" | while IFS='=' read -r key val; do + # Split each pair into key and value using '=' as the delimiter + + # set variables and run functions based on the pairs case "$key" in - "SystemId") sid="$val" ;; + "SystemId") + sid="$val" + log "DEBUG" "Setting sid=$sid" + ;; "GamePath") gp="$val" sp=$(echo "$val" | sed 's|roms|saves|; s|\.[^.]*$|.srm|') bp=$(echo "$sp" | sed 's|saves|archives|') + log "DEBUG" "Setting values {gp=$gp, sp=$sp, bp=$bp}" + ;; + "Action") + act="$val" + log "DEBUG" "Setting act=$act" ;; - "Action") act="$val" ;; "State") st="$val" + log "DEBUG" "Setting st=$st" + log "DEBUG" "Starting logic tree" if [ "$st" = "playing" ] && [ "$act" = "rungame" ]; then log "INFO" "Sync START: $(basename "$gp")" - remote_f="$REMOTE_BASE/$sid/$(basename "$sp")" loc_sz=$(stat -c %s "$sp" 2>/dev/null || echo 0) + log "DEBUG" "local_size = $loc_sz" rem_sz=$(rclone lsjson "$remote_f" 2>/dev/null | grep -o '"Size":[0-9]*' | cut -d: -f2) : "${rem_sz:=0}" + log "DEBUG" "remote_size = $rem_sz" if [ "$loc_sz" -lt "$rem_sz" ]; then log "WARN" "Cloud save larger ($rem_sz). Restoring..." mkdir -p "$(dirname "$bp")" - log "DEBUG" "rclone copyto $remote_f $sp --backup-dir $(dirname $bp)" + log "DEBUG" "rclone copyto $remote_f $sp --backup-dir $(dirname "$bp")" rclone copyto "$remote_f" "$sp" --backup-dir "$(dirname "$bp")" else log "INFO" "Local save current. Updating..." @@ -51,7 +66,6 @@ echo "$PAYLOAD" | tr -d '\r' | while IFS="=" read -r key val; do log "DEBUG" "rclone update $sp $REMOTE_BASE/$sid/" rclone update "$sp" "$REMOTE_BASE/$sid/" log "INFO" "Final Sync Done." - #mosquitto_pub -t "$RESPONSE_TOPIC" -m "SaveSync=0" 2>/dev/null fi ;; esac diff --git a/sslogger[start](permanent).ash b/slogger[start](permanent).ash similarity index 100% rename from sslogger[start](permanent).ash rename to slogger[start](permanent).ash