#!/bin/sh # RomM Companion Pak for NextUI (TrimUI Brick / Smart Pro tg5040 & Smart Pro S tg5050) PAK_DIR="$(cd "$(dirname "$0")" && pwd)" cd "$PAK_DIR" || exit 1 BIN="$PAK_DIR/bin/romm-nextui" TMP_DIR="/tmp/romm-nextui" mkdir -p "$TMP_DIR" LOG_DIR="${LOGS_PATH:-$TMP_DIR}" mkdir -p "$LOG_DIR" LOG_FILE="$LOG_DIR/romm-companion.log" { echo "=== RomM Companion launched at $(date) ===" echo "PLATFORM=${PLATFORM:-unset} SDCARD_PATH=${SDCARD_PATH:-unset} PAK_DIR=$PAK_DIR" } >> "$LOG_FILE" # Resolve platform-specific NextUI binaries (tg5050 for Smart Pro S, tg5040 for Brick / Smart Pro) PLAT="${PLATFORM:-}" if [ -z "$PLAT" ]; then case "$PAK_DIR" in *tg5050*) PLAT="tg5050" ;; *tg5040*) PLAT="tg5040" ;; *) PLAT="tg5050" ;; esac fi find_ui_tool() { TOOL="$1" if [ -x "$PAK_DIR/bin/$PLAT/$TOOL" ]; then echo "$PAK_DIR/bin/$PLAT/$TOOL" elif [ -x "$PAK_DIR/bin/$TOOL" ]; then echo "$PAK_DIR/bin/$TOOL" elif command -v "$TOOL" >/dev/null 2>&1; then command -v "$TOOL" else echo "" fi } MINUI_LIST="$(find_ui_tool minui-list)" MINUI_PRESENTER="$(find_ui_tool minui-presenter)" echo "Resolved PLAT=$PLAT MINUI_LIST=$MINUI_LIST MINUI_PRESENTER=$MINUI_PRESENTER" >> "$LOG_FILE" show_message() { MSG="$1" BTN="${2:-OK}" if [ -n "$MINUI_PRESENTER" ]; then "$MINUI_PRESENTER" \ --message "$MSG" \ --confirm-show \ --confirm-button A \ --confirm-text "$BTN" \ --timeout 0 >> "$LOG_FILE" 2>&1 else echo "$MSG" >> "$LOG_FILE" fi } BUSY_PID="" start_busy() { stop_busy if [ -n "$MINUI_PRESENTER" ]; then "$MINUI_PRESENTER" --message "$1" --timeout -1 >> "$LOG_FILE" 2>&1 & BUSY_PID=$! fi } stop_busy() { if [ -n "$BUSY_PID" ]; then kill "$BUSY_PID" 2>/dev/null wait "$BUSY_PID" 2>/dev/null BUSY_PID="" fi } trap stop_busy EXIT INT TERM # Runs minui-list using --write-location (reliable across all NextUI platforms) pick_from_list() { LIST_FILE="$1" TITLE="$2" OUT_FILE="$TMP_DIR/selection.txt" rm -f "$OUT_FILE" "$MINUI_LIST" \ --file "$LIST_FILE" \ --format json \ --title "$TITLE" \ --write-location "$OUT_FILE" >> "$LOG_FILE" 2>&1 if [ -s "$OUT_FILE" ]; then cat "$OUT_FILE" fi } if [ ! -x "$BIN" ]; then show_message "romm-nextui binary missing!\nRun build-pak.sh to bundle the aarch64 binary." "Exit" exit 1 fi if [ -z "$MINUI_LIST" ] || [ -z "$MINUI_PRESENTER" ]; then echo "ERROR: minui-list or minui-presenter missing for $PLAT" >> "$LOG_FILE" exit 1 fi run_sync() { start_busy "Syncing saves & states with RomM 5.1.0..." OUT="$("$BIN" sync 2>&1)" stop_busy echo "$OUT" >> "$LOG_FILE" show_message "$OUT" "Done" } run_web_setup() { IP="$("$BIN" ip 2>/dev/null)" [ -z "$IP" ] && IP="127.0.0.1" PORT="8765" "$BIN" web-setup --port "$PORT" >> "$LOG_FILE" 2>&1 & WEB_PID=$! show_message "Web Setup Running!\n\nOpen on your phone or PC:\nhttp://${IP}:${PORT}\n\nPress A when finished." "Stop Server" kill "$WEB_PID" 2>/dev/null wait "$WEB_PID" 2>/dev/null } # Extracts a numeric field from the matching JSON item in a minui-list file by exact "name" lookup_field_by_name() { JSON_FILE="$1" TARGET_NAME="$2" FIELD="$3" # Each item is on one compact line or object; match the object containing "name":"TARGET_NAME" awk -v name="$TARGET_NAME" -v field="$FIELD" ' BEGIN { RS="}" } index($0, "\"name\":\"" name "\"") > 0 { if (match($0, "\"" field "\":[[:space:]]*\"?[^,\"]+\"?")) { val = substr($0, RSTART, RLENGTH) sub("^\"[^\"]+\":[[:space:]]*\"?", "", val) sub("\"$", "", val) print val exit } } ' "$JSON_FILE" } run_download_roms() { start_busy "Loading platforms from RomM..." "$BIN" platforms-menu > "$TMP_DIR/platforms.json" 2>> "$LOG_FILE" RC=$? stop_busy if [ $RC -ne 0 ] || ! grep -q "platform_id" "$TMP_DIR/platforms.json"; then show_message "Could not load platforms.\nCheck Wi-Fi and connect via Web Setup first." "OK" return fi PLAT_NAME="$(pick_from_list "$TMP_DIR/platforms.json" "RomM Platforms")" [ -z "$PLAT_NAME" ] && return PLAT_ID="$(lookup_field_by_name "$TMP_DIR/platforms.json" "$PLAT_NAME" "platform_id")" [ -z "$PLAT_ID" ] && return start_busy "Loading ROMs..." "$BIN" roms-menu --platform-id "$PLAT_ID" > "$TMP_DIR/roms.json" 2>> "$LOG_FILE" stop_busy if ! grep -q "rom_id" "$TMP_DIR/roms.json"; then show_message "No ROMs found for this platform." "OK" return fi ROM_NAME="$(pick_from_list "$TMP_DIR/roms.json" "Download ROM")" [ -z "$ROM_NAME" ] && return ROM_ID="$(lookup_field_by_name "$TMP_DIR/roms.json" "$ROM_NAME" "rom_id")" [ -z "$ROM_ID" ] && return start_busy "Downloading ROM to SD card..." OUT="$("$BIN" download-rom --rom-id "$ROM_ID" 2>&1)" stop_busy echo "$OUT" >> "$LOG_FILE" show_message "$OUT" "OK" } run_game_saves() { start_busy "Loading platforms from RomM..." "$BIN" platforms-menu > "$TMP_DIR/platforms.json" 2>> "$LOG_FILE" RC=$? stop_busy if [ $RC -ne 0 ] || ! grep -q "platform_id" "$TMP_DIR/platforms.json"; then show_message "Could not load platforms.\nCheck Wi-Fi and connect via Web Setup first." "OK" return fi PLAT_NAME="$(pick_from_list "$TMP_DIR/platforms.json" "Select Platform")" [ -z "$PLAT_NAME" ] && return PLAT_ID="$(lookup_field_by_name "$TMP_DIR/platforms.json" "$PLAT_NAME" "platform_id")" [ -z "$PLAT_ID" ] && return start_busy "Loading games..." "$BIN" roms-menu --platform-id "$PLAT_ID" > "$TMP_DIR/roms.json" 2>> "$LOG_FILE" stop_busy ROM_NAME="$(pick_from_list "$TMP_DIR/roms.json" "Select Game")" [ -z "$ROM_NAME" ] && return ROM_ID="$(lookup_field_by_name "$TMP_DIR/roms.json" "$ROM_NAME" "rom_id")" [ -z "$ROM_ID" ] && return start_busy "Loading saves & states..." "$BIN" rom-saves-menu --rom-id "$ROM_ID" > "$TMP_DIR/rom_saves.json" 2>> "$LOG_FILE" stop_busy SAVE_NAME="$(pick_from_list "$TMP_DIR/rom_saves.json" "Game Saves & States")" [ -z "$SAVE_NAME" ] && return KIND="$(lookup_field_by_name "$TMP_DIR/rom_saves.json" "$SAVE_NAME" "kind")" ITEM_ID="$(lookup_field_by_name "$TMP_DIR/rom_saves.json" "$SAVE_NAME" "id")" case "$KIND" in pull_save) start_busy "Pulling save to SD card..." OUT="$("$BIN" pull-save --save-id "$ITEM_ID" --rom-id "$ROM_ID" 2>&1)" stop_busy ;; pull_state) start_busy "Pulling state to SD card..." OUT="$("$BIN" pull-state --state-id "$ITEM_ID" --rom-id "$ROM_ID" 2>&1)" stop_busy ;; push_local) start_busy "Pushing local saves to RomM..." OUT="$("$BIN" push-rom-saves --rom-id "$ROM_ID" 2>&1)" stop_busy ;; *) return ;; esac echo "$OUT" >> "$LOG_FILE" show_message "$OUT" "OK" } run_conflicts() { "$BIN" conflicts-menu > "$TMP_DIR/conflicts.json" 2>> "$LOG_FILE" if ! grep -q "conflict_id" "$TMP_DIR/conflicts.json"; then show_message "No pending save conflicts!" "OK" return fi C_NAME="$(pick_from_list "$TMP_DIR/conflicts.json" "Save Conflicts")" [ -z "$C_NAME" ] && return CID="$(lookup_field_by_name "$TMP_DIR/conflicts.json" "$C_NAME" "conflict_id")" [ -z "$CID" ] && return cat > "$TMP_DIR/resolve.json" <<'EOF' {"items":[{"name":"Keep Handheld Save (Upload)"},{"name":"Keep RomM Server Save (Backup & Download)"}]} EOF CHOICE="$(pick_from_list "$TMP_DIR/resolve.json" "Resolve Conflict")" case "$CHOICE" in "Keep Handheld Save (Upload)") RES="keep_local" ;; "Keep RomM Server Save (Backup & Download)") RES="keep_server" ;; *) return ;; esac start_busy "Resolving conflict..." OUT="$("$BIN" resolve-conflict --id "$CID" --resolution "$RES" 2>&1)" stop_busy show_message "$OUT" "OK" } # Main NextUI D-pad Menu Loop cat > "$TMP_DIR/menu.json" <<'EOF' { "items": [ {"name": "Sync Saves & States Now"}, {"name": "Download ROMs from RomM"}, {"name": "Pull / Push Game Save"}, {"name": "Web Setup (Phone / PC)"}, {"name": "Resolve Save Conflicts"}, {"name": "Connection & SD Status"}, {"name": "Exit"} ] } EOF while true; do SELECTED="$(pick_from_list "$TMP_DIR/menu.json" "RomM Companion")" case "$SELECTED" in "Sync Saves & States Now") run_sync ;; "Download ROMs from RomM") run_download_roms ;; "Pull / Push Game Save") run_game_saves ;; "Web Setup (Phone / PC)") run_web_setup ;; "Resolve Save Conflicts") run_conflicts ;; "Connection & SD Status") OUT="$("$BIN" status --text 2>&1)" show_message "$OUT" "Back" ;; "Exit"|"") break ;; esac done stop_busy exit 0