if[[ ! -L "$rdhome"&& ! -L "$rdhome/roms"]];then# Add a roms folder symlink back to ~/retrodeck if missing, to fix things like PS2 autosaves until user migrates whole ~retrodeck directory
versionwheresaveschanged="0.4.5b"# Hardcoded break point between unsorted and sorted saves
if[[$(sed -e "s/\.//g"<<<$hard_version) > $(sed -e "s/\.//g"<<<$versionwheresaveschanged)]]&&[[ ! $(sed -e "s/\.//g"<<<$version) > $(sed -e "s/\.//g"<<<$versionwheresaveschanged)]];then# Check if user is upgrading from the version where save organization was changed. Try not to reuse this, it things 0.4.5b is newer than 0.4.5
--text="You are updating to a version of RetroDECK where save file locations have changed!\n\nYour existing files will be backed up for safety and then sorted automatically.\n\nIf a file cannot be sorted automatically it will remain where it is for manual sorting.\n\nPLEASE BE PATIENT! This process can take several minutes if you have a large ROM library."
allgames=($(find "$roms_folder" -maxdepth 2 -mindepth 2 ! -name "systeminfo.txt" ! -name "systems.txt" ! -name "gc" ! -name "n3ds" ! -name "nds" ! -name "wii" ! -name "xbox" ! -name "*^*"| sed -e "s/ /\^/g"))# Build an array of all games and multi-disc-game-containing folders, adding whitespace placeholder
allsaves=($(find "$saves_folder" -mindepth 1 -maxdepth 1 -name "*.*" ! -name "gc" ! -name "n3ds" ! -name "nds" ! -name "wii" ! -name "xbox"| sed -e "s/ /\^/g"))# Build an array of all save files, ignoring standalone emulator sub-folders, adding whitespace placeholder
allstates=($(find "$states_folder" -mindepth 1 -maxdepth 1 -name "*.*" ! -name "gc" ! -name "n3ds" ! -name "nds" ! -name "wii" ! -name "xbox"| sed -e "s/ /\^/g"))# Build an array of all state files, ignoring standalone emulator sub-folders, adding whitespace placeholder
if[[ ! "${gamestoskip[*]}"=~ "${1}"]];then# If the current game name exists multiple times in array ie. /roms/snes/Mortal Kombat 3.zip and /roms/genesis/Mortal Kombat 3.zip, skip and alert user to sort manually
gamebasename=$(basename "$game"| sed -e 's/\..*//')# Extract pure file name ie. /roms/snes/game1.zip becomes game1
systemdir="$(basename "$(dirname "$1")")"# Extract parent directory identifying system ROM belongs to
matches=($(find "$roms_folder" -maxdepth 2 -mindepth 2 -name "$gamebasename"".*"| sed -e 's/ /^/g'| sed -e 's/\..*//'))# Search for multiple instances of pure game name, adding to skip list if found
if[[${#matches[@]} -gt 1]];then
echo"ERROR: Multiple ROMS found with name:"$gamebasename"Please sort saves and states for these ROMS manually" >> $migration_logfile
log w "Game with name \"$name\" already found. Skipping to next game..."$migration_logfile# Inform user of game being skipped due to duplicate ROM names
find "$roms_folder" -mindepth 2 -maxdepth 2 -name "*\^*" -exec echo"ERROR: Game named"{}"found, please move save manually"\; >> $migration_logfile# Warn user if any of their files have the whitespace replacement character used by the script
totalfiles=$totalsaves#set variables for save file migration
filesleft=$totalsaves
currentlybeingmoved="saves"
current_dest_folder=$saves_folder
for i in "${allsaves[@]}";do# For each save file, compare to every ROM file name looking for a match
found=
currentsave=($(basename "$i"| sed -e 's/\..*//'))# Extract pure file name ie. /saves/game1.sav becomes game1
for j in "${allgames[@]}";do
currentgame=($(basename "$j"| sed -e 's/\..*//'))# Extract pure file name ie. /roms/snes/game1.zip becomes game1
[[$currentgame==$currentsave]]&&{found=1; break;}# If names match move to next stage, otherwise skip
done
[[ -n $found]]&& movefile $j$i||echo"ERROR: No ROM match found for save file"$i| sed -e 's/\^/ /g' >> $migration_logfile# If a match is found, run movefile() otherwise warn user of stranded save file
done
totalfiles=$totalstates#set variables for state file migration
filesleft=$totalstates
currentlybeingmoved="states"
current_dest_folder=$states_folder
for i in "${allstates[@]}";do# For each state file, compare to every ROM file name looking for a match
found=
currentstate=($(basename "$i"| sed -e 's/\..*//'))# Extract pure file name ie. /states/game1.sav becomes game1
for j in "${allgames[@]}";do
currentgame=($(basename "$j"| sed -e 's/\..*//'))# Extract pure file name ie. /roms/snes/game1.zip becomes game1
[[$currentgame==$currentstate]]&&{found=1; break;}# If names match move to next stage, otherwise skip
done
[[ -n $found]]&& movefile $j$i||echo"ERROR: No ROM match found for state file"$i| sed -e 's/\^/ /g' >> $migration_logfile# If a match is found, run movefile() otherwise warn user of stranded state file
--text="The migration process has sorted all of your files automatically.\n\nEverything should be working normally, if you experience any issues please check the RetroDECK wiki or contact us directly on the Discord."
--text="The migration process was unable to sort $(cat $migration_logfile| grep "ERROR"| wc -l) files automatically.\n\nThese files will need to be moved manually to their new locations, find more detail on the RetroDECK wiki.\n\nA log of the files that need manual sorting can be found at $rdhome/manual_sort_needed.log"