#!/bin/ash # shellcheck shell=dash # 1. Load Configs - Use absolute paths . /recalbox/share/system/configs/savesync/savesync.conf # 2. Setup Variables EVENT_FILE="/tmp/es_state.inf" RESPONSE_TOPIC="recalbox/savesync/response" TIMEOUT_SEC=10 log() { # Ensure port is 1883 mosquitto_pub -h 127.0.0.1 -p 1883 -q 0 -t "$LOG_TOPIC" -m "SaveLog=[$(date '+%H:%M:%S')] [$1] $2" 2>/dev/null } # 3. Check Payload [ ! -s "$EVENT_FILE" ] && exit 0 PAYLOAD=$(cat "$EVENT_FILE") # 4. Check Connection quickly (1 second max) #nc -z -w 1 "$RCLONE_ENDPOINT" "$RCLONE_PORT" || { log "ERROR" "Offline"; exit 1; } log "DEBUG" "Checking connection to: ${RCLONE_ENDPOINT}:${RCLONE_PORT}" nc -zv -w 1 "$RCLONE_ENDPOINT" "$RCLONE_PORT" 2>&1 | log "DEBUG" "$(cat)" # 5. Publish Request if mosquitto_pub -h 127.0.0.1 -p 1883 -t "$TOPIC" -m "$PAYLOAD"; then log "INFO" "Event published. Waiting for Daemon..." else log "ERROR" "MQTT Broker unreachable." exit 1 fi # 6. The Wait (With Timeout) # We listen for the daemon to say "SaveContinue=0" ( sleep "$TIMEOUT_SEC" mosquitto_pub -h 127.0.0.1 -t "$RESPONSE_TOPIC" -m "SaveSync=timeout" ) & TIMER_PID=$! # Wait for exactly 1 message RESPONSE=$(mosquitto_sub -h 127.0.0.1 -p 1883 -t "$RESPONSE_TOPIC" -C 1) kill "$TIMER_PID" 2>/dev/null case "$RESPONSE" in *"timeout"*) log "ERROR" "Daemon timed out. Starting game with local save." ;; *) log "INFO" "Daemon finished. Proceeding." ;; esac exit 0