What do you think about this script to clean the RetroArch configuration?

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
Kyowash

What do you think about this script to clean the RetroArch configuration?

Post by Kyowash »

Sometimes I mess the RetroArch configuration up and I have to configure everything again because I don't like the default configuration. As I was tired of doing the same every time I changed something accidentally, I made this script to restore my preferred configuration. The preferred configuration files are in the $HOME/.cleanconfig/retroarch directory.

However, there may be screenshots in the old configuration folder, so it first checks if there are any screenshots in the $HOME/.config/retroarch/screenshots directory. If there are, it moves them to a temporary directory, replace the configuration, and restore the screenshots.

This is the script I did:

Code: Select all

#!/bin/sh
set -o errexit
set -o nounset

if [ -d "$HOME/.cleanconfig/retroarch" ]
    then
        screenshots=0
        if [ -d "$HOME/.config/retroarch" ]
            then
                if [ -d "$HOME/.config/retroarch/screenshots" ]
                    then
                        if [ -n "$(ls -A "$HOME/.config/retroarch/screenshots")" ]
                            then
                                screenshots=1
                                readonly TemporaryDirectory="$HOME/tempdir-$(date "+%Y-%m-%dT%T")"
                                mkdir -- "$TemporaryDirectory"
                                mv -- "$HOME/.config/retroarch/screenshots"/* "$TemporaryDirectory"
                        fi
                fi
                rm -r -- "$HOME/.config/retroarch"
        fi
        cp -R -- "$HOME/.cleanconfig/retroarch" "$HOME/.config"
        if [ $screenshots -eq 1 ]
            then
                mv -- "$TemporaryDirectory"/* "$HOME/.config/retroarch/screenshots"
                rm -r -- "$TemporaryDirectory"
        fi
else
    printf "The %s/.cleanconfig/retroarch directory doesn't exist.\n" "$HOME" 1>&2
    exit 1
fi
I wanted to make the script as portable as possible, that's why for example I decided to use the current date for the name of the temporary directory instead of using utilities like mktemp.

Would you change/add anything?

Thanks in advance.
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
rene
Level 20
Level 20
Posts: 12212
Joined: Sun Mar 27, 2016 6:58 pm

Re: What do you think about this script to clean the RetroArch configuration?

Post by rene »

rsync -avn --exclude screenshots --delete ~/.cleanconfig/retroarch ~/.config/?

Without -n once satisfied, of course. And possibly without --exclude screenshots --delete if no undesired additional files show up in ~/.config/retroarch in the first place. And without -v if you don't care for verbosity. And without -a if you feel no need to preserve e.g. time stamps. And... ah, no, that's it.
rene
Level 20
Level 20
Posts: 12212
Joined: Sun Mar 27, 2016 6:58 pm

Re: What do you think about this script to clean the RetroArch configuration?

Post by rene »

It seems I may have murdered this thread a little, so, err, other than that...

I would personally suggest moving the screenshots directory rather than its content.That is, I would say e.g.

Code: Select all

mv "$HOME/.config/retroarch/screenshots" "$HOME/tempdir-$(date +%Y-%m-%dT%T)"
and the other way around for the restore.
Locked

Return to “Scripts & Bash”