Cleaned it up a bit, fixed shellcheck errors, formatting
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
#!/bin/ash
|
#!/bin/ash
|
||||||
#
|
#shellcheck shell=dash
|
||||||
PAYLOAD=$(cat /tmp/es_state.inf)
|
PAYLOAD=$(cat /tmp/es_state.inf)
|
||||||
echo "EVENT:$PAYLOAD" >/tmp/savesync_pipe
|
echo "EVENT:$PAYLOAD" >/tmp/savesync_pipe
|
||||||
|
|||||||
3
savesync.conf
Normal file
3
savesync.conf
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
#!/bin/ash
|
||||||
|
#shellcheck shell=dash
|
||||||
|
export REMOTE_BASE="saves:gamepi-tv"
|
||||||
@@ -1,41 +1,39 @@
|
|||||||
#!/bin/ash
|
#!/bin/ash
|
||||||
|
# shellcheck shell=dash
|
||||||
. /recalbox/share/system/configs/savesync/savesync.conf
|
. /recalbox/share/system/configs/savesync/savesync.conf
|
||||||
|
|
||||||
PIPE="/tmp/savesync_pipe"
|
PIPE="/tmp/savesync_pipe"
|
||||||
LOG_FILE="/recalbox/share/system/logs/savesync.log"
|
LOG_FILE="/recalbox/share/system/logs/savesync.log"
|
||||||
|
|
||||||
mkfifo "$PIPE"
|
# Setup the pipe if it doesn't exist
|
||||||
|
[ -p "$PIPE" ] || mkfifo "$PIPE"
|
||||||
chmod 666 "$PIPE"
|
chmod 666 "$PIPE"
|
||||||
|
|
||||||
# Function to write to disk
|
|
||||||
write_log() {
|
write_log() {
|
||||||
printf "[%s] %s\n" "$(date '+%Y-%m-%d %H:%M:%S')" "$1" >>"$LOG_FILE"
|
printf "[%s] %s\n" "$(date '+%Y-%m-%d %H:%M:%S')" "$1" >> "$LOG_FILE"
|
||||||
}
|
}
|
||||||
|
|
||||||
# The listener loop
|
write_log "--- Daemon Started ---"
|
||||||
|
|
||||||
while true; do
|
while true; do
|
||||||
if read -r line <"$PIPE"; then
|
# Read one line from the pipe
|
||||||
|
if read -r line < "$PIPE"; then
|
||||||
case "$line" in
|
case "$line" in
|
||||||
LOG:*)
|
LOG:*)
|
||||||
# Remove the "LOG:" prefix and write
|
|
||||||
write_log "${line#LOG:}"
|
write_log "${line#LOG:}"
|
||||||
;;
|
;;
|
||||||
EVENT:*)
|
EVENT:*)
|
||||||
# Handle Game Events (forked to background)
|
# Fork the event handling
|
||||||
(
|
(
|
||||||
|
|
||||||
PIPE="/tmp/savesync_pipe"
|
|
||||||
event_data="${line#EVENT:}"
|
event_data="${line#EVENT:}"
|
||||||
|
|
||||||
# Clean input
|
# Process the multi-line event data
|
||||||
$(echo "$event_data" | tr -d '\r')
|
# We use echo and a pipe to feed the while loop
|
||||||
|
echo "$event_data" | tr -d '\r' | while IFS="=" read -r key val; do
|
||||||
while IFS= read -r key val; do
|
|
||||||
case "$key" in
|
case "$key" in
|
||||||
"SystemId") sid="$val" ;;
|
"SystemId") sid="$val" ;;
|
||||||
"GamePath")
|
"GamePath")
|
||||||
gp="$val"
|
gp="$val"
|
||||||
# Fast path transformation using sed
|
|
||||||
sp=$(echo "$val" | sed 's|roms|saves|; s|\.[^.]*$|.srm|')
|
sp=$(echo "$val" | sed 's|roms|saves|; s|\.[^.]*$|.srm|')
|
||||||
bp=$(echo "$sp" | sed 's|saves|archives|')
|
bp=$(echo "$sp" | sed 's|saves|archives|')
|
||||||
;;
|
;;
|
||||||
@@ -43,54 +41,35 @@ while true; do
|
|||||||
"State")
|
"State")
|
||||||
st="$val"
|
st="$val"
|
||||||
|
|
||||||
# Check if we have the full "Start Game" context
|
|
||||||
if [ "$st" = "playing" ] && [ "$act" = "rungame" ]; then
|
if [ "$st" = "playing" ] && [ "$act" = "rungame" ]; then
|
||||||
# FORK to background, but keep current variables!
|
write_log "Sync START: $(basename "$gp")"
|
||||||
(
|
|
||||||
printf "%s %s\n" "LOG:" "Syncing START for $(basename "$gp")" >${PIPE}
|
|
||||||
|
|
||||||
remote_f="$REMOTE_BASE/$sid/$(basename "$sp")"
|
remote_f="$REMOTE_BASE/$sid/$(basename "$sp")"
|
||||||
loc_sz=$(stat -c %s "$sp" 2>/dev/null || echo 0)
|
loc_sz=$(stat -c %s "$sp" 2>/dev/null || echo 0)
|
||||||
|
|
||||||
# Fetch remote size
|
|
||||||
rem_sz=$(rclone lsjson "$remote_f" 2>/dev/null | grep -o '"Size":[0-9]*' | cut -d: -f2)
|
rem_sz=$(rclone lsjson "$remote_f" 2>/dev/null | grep -o '"Size":[0-9]*' | cut -d: -f2)
|
||||||
: "${rem_sz:=0}"
|
: "${rem_sz:=0}"
|
||||||
|
|
||||||
if [ "$loc_sz" -lt "$rem_sz" ]; then
|
if [ "$loc_sz" -lt "$rem_sz" ]; then
|
||||||
printf "%s %s\n" "LOG:" "Cloud save larger. Restoring..." >${PIPE}
|
write_log "Cloud save larger ($rem_sz). Restoring..."
|
||||||
mkdir -p "$(dirname "$bp")"
|
mkdir -p "$(dirname "$bp")"
|
||||||
rclone copyto "$remote_f" "$sp" --backup-dir "$(dirname "$bp")"
|
rclone copyto "$remote_f" "$sp" --backup-dir "$(dirname "$bp")"
|
||||||
else
|
else
|
||||||
printf "%s %s\n" "LOG:" "Local save current. Updating..."> >${PIPE}
|
write_log "Local save current. Updating..."
|
||||||
rclone update "$remote_f" "$sp"
|
rclone update "$remote_f" "$sp"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# SIGNAL SUCCESS (Key must match Publisher's expectation)
|
# Signal back to the publisher via MQTT or a second Response Pipe
|
||||||
printf "%s %s\n" "LOG:" "Save synced successfully" >${PIPE}
|
#mosquitto_pub -t "$RESPONSE_TOPIC" -m "SaveSync=0" 2>/dev/null
|
||||||
) &
|
|
||||||
|
|
||||||
elif [ "$st" = "endgame" ]; then
|
elif [ "$st" = "endgame" ]; then
|
||||||
# Syncing on end - we don't necessarily need to block here
|
write_log "Sync END: $(basename "$sp")"
|
||||||
(
|
|
||||||
printf "%s %s\n" "LOG:" "Syncing END for $(basename "$sp")" >${PIPE}
|
|
||||||
rclone update "$sp" "$REMOTE_BASE/$sid/"
|
rclone update "$sp" "$REMOTE_BASE/$sid/"
|
||||||
printf "%s %s\n" "LOG:" "Final Sync Done." >${PIPE}
|
write_log "Final Sync Done."
|
||||||
|
#mosquitto_pub -t "$RESPONSE_TOPIC" -m "SaveSync=0" 2>/dev/null
|
||||||
) &
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Clear variables for next burst of events
|
|
||||||
sid=""
|
|
||||||
gp=""
|
|
||||||
sp=""
|
|
||||||
bp=""
|
|
||||||
act=""
|
|
||||||
st=""
|
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
done<$(printf "%s\n" "$event_data" | tr -d '\r')
|
done
|
||||||
|
|
||||||
printf '%s\n' "LOG:Sync complete for $sp" >"$PIPE"
|
|
||||||
) &
|
) &
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
|
|||||||
Reference in New Issue
Block a user