2
\$\begingroup\$

I wrote this bash script to try to make sure save games don't get mixed between different steam users using the same Ubuntu account.

It uses inotify to try to figure out when a user changes and a soft link to point to the correct folder.

#!/bin/bash
link_name=~/.factorio
check_username=$(awk -F'"' '/AutoLoginUser/{print $4}' ~/.steam/registry.vdf)

function check_link_username {
    local link_from=$1
    local username=$2
    if [[ -L ${link_from} ]]
    then
        if [[ "${link_from}" -ef "${link_from}_${username}" ]]
        then
            return 0
        fi
        if [[ -d ${link_from} ]]
        then
            mkdir -p "${link_from}_${username}"
        elif [[ ! -f  "${link_from}_${username}" ]]
        then
            cp "${link_from}" "${link_from}_${username}"
        fi  
        rm ${link_from}
    else
        echo mv "${link_from}" "${link_from}_${username}"
        mv "${link_from}" "${link_from}_${username}"
    fi
    ln -s "${link_from}_${username}" "${link_from}"
}

check_link_username "$link_name" "$check_username"

while inotifywait -qqe delete_self -e modify ~/.steam/registry.vdf || true
do
  if newcheck=$(awk -F'"' '/AutoLoginUser/{print $4}' ~/.steam/registry.vdf)
  then
      if [[ ! "${newcheck}" = "${check_username}" ]]
      then
        check_username="${newcheck}"
        echo new user:${check_username}
        check_link_username "$link_name" "$check_username"
      fi
  fi
done

When trying to figure out which inotify events to watch for I monitored the parent folder:

~$ inotifywait --monitor --timefmt '%T' --format '%T %e                  %f' ~/.steam/| grep registry.vdf
Setting up watches.
Watches established.
19:55:34 OPEN                  registry.vdf
19:55:34 ACCESS                  registry.vdf
19:55:34 CLOSE_NOWRITE,CLOSE                  registry.vdf
19:55:34 CREATE                  registry.vdf20210926195534
19:55:34 OPEN                  registry.vdf20210926195534
19:55:34 MODIFY                  registry.vdf20210926195534
19:55:34 OPEN                  registry.vdf20210926195534
19:55:34 ACCESS                  registry.vdf20210926195534
19:55:34 CLOSE_NOWRITE,CLOSE                  registry.vdf20210926195534
19:55:34 CLOSE_WRITE,CLOSE                  registry.vdf20210926195534
19:55:34 MOVED_FROM                  registry.vdf20210926195534
19:55:34 MOVED_TO                  registry.vdf20210926195534.tmp.swap
19:55:34 MOVED_FROM                  registry.vdf
19:55:34 MOVED_TO                  registry.vdf20210926195534
19:55:34 MOVED_FROM                  registry.vdf20210926195534.tmp.swap
19:55:34 MOVED_TO                  registry.vdf
19:55:34 DELETE                  registry.vdf20210926195534
19:55:34 CREATE                  registry.vdf20210926195534
19:55:34 OPEN                  registry.vdf20210926195534
19:55:34 MODIFY                  registry.vdf20210926195534
19:55:34 OPEN                  registry.vdf20210926195534
19:55:34 ACCESS                  registry.vdf20210926195534
19:55:34 CLOSE_NOWRITE,CLOSE                  registry.vdf20210926195534
19:55:34 OPEN                  registry.vdf
19:55:34 ACCESS                  registry.vdf
19:55:34 CLOSE_NOWRITE,CLOSE                  registry.vdf
19:55:34 OPEN                  registry.vdf20210926195534
19:55:34 ACCESS                  registry.vdf20210926195534
19:55:34 CLOSE_NOWRITE,CLOSE                  registry.vdf20210926195534
19:55:34 CLOSE_WRITE,CLOSE                  registry.vdf20210926195534
19:55:34 MOVED_FROM                  registry.vdf20210926195534
19:55:34 MOVED_TO                  registry.vdf20210926195534.tmp.swap
19:55:34 MOVED_FROM                  registry.vdf
19:55:34 MOVED_TO                  registry.vdf20210926195534
19:55:34 MOVED_FROM                  registry.vdf20210926195534.tmp.swap
19:55:34 MOVED_TO                  registry.vdf
19:55:34 DELETE                  registry.vdf20210926195534
19:55:34 CREATE                  registry.vdf20210926195534
19:55:34 OPEN                  registry.vdf20210926195534
19:55:34 MODIFY                  registry.vdf20210926195534
19:55:34 OPEN                  registry.vdf20210926195534
19:55:34 ACCESS                  registry.vdf20210926195534
19:55:34 CLOSE_NOWRITE,CLOSE                  registry.vdf20210926195534
19:55:34 OPEN                  registry.vdf
19:55:34 ACCESS                  registry.vdf
19:55:34 CLOSE_NOWRITE,CLOSE                  registry.vdf
19:55:34 OPEN                  registry.vdf20210926195534
19:55:34 ACCESS                  registry.vdf20210926195534
19:55:34 CLOSE_NOWRITE,CLOSE                  registry.vdf20210926195534
19:55:34 CLOSE_WRITE,CLOSE                  registry.vdf20210926195534
19:55:34 MOVED_FROM                  registry.vdf20210926195534
19:55:34 MOVED_TO                  registry.vdf20210926195534.tmp.swap
19:55:34 MOVED_FROM                  registry.vdf
19:55:34 MOVED_TO                  registry.vdf20210926195534
19:55:34 MOVED_FROM                  registry.vdf20210926195534.tmp.swap
19:55:34 MOVED_TO                  registry.vdf
19:55:34 DELETE                  registry.vdf20210926195534
19:55:34 OPEN                  registry.vdf
19:55:34 ACCESS                  registry.vdf
19:55:34 CLOSE_NOWRITE,CLOSE                  registry.vdf
19:55:34 CREATE                  registry.vdf20210926195534
19:55:34 OPEN                  registry.vdf20210926195534
19:55:34 MODIFY                  registry.vdf20210926195534
19:55:34 OPEN                  registry.vdf20210926195534
19:55:34 ACCESS                  registry.vdf20210926195534
19:55:34 CLOSE_NOWRITE,CLOSE                  registry.vdf20210926195534
19:55:34 CLOSE_WRITE,CLOSE                  registry.vdf20210926195534
19:55:34 MOVED_FROM                  registry.vdf20210926195534
19:55:34 MOVED_TO                  registry.vdf20210926195534.tmp.swap
19:55:34 MOVED_FROM                  registry.vdf
19:55:34 MOVED_TO                  registry.vdf20210926195534
19:55:34 MOVED_FROM                  registry.vdf20210926195534.tmp.swap
19:55:34 MOVED_TO                  registry.vdf
19:55:34 DELETE                  registry.vdf20210926195534
19:55:34 OPEN                  registry.vdf
19:55:34 ACCESS                  registry.vdf
19:55:34 CLOSE_NOWRITE,CLOSE                  registry.vdf
19:55:35 OPEN                  registry.vdf
19:55:35 ACCESS                  registry.vdf
19:55:35 CLOSE_NOWRITE,CLOSE                  registry.vdf
19:55:35 OPEN                  registry.vdf
19:55:35 ACCESS                  registry.vdf
19:55:35 CLOSE_NOWRITE,CLOSE                  registry.vdf
19:55:35 CREATE                  registry.vdf20210926195535
19:55:35 OPEN                  registry.vdf20210926195535
19:55:35 MODIFY                  registry.vdf20210926195535
19:55:35 OPEN                  registry.vdf20210926195535
19:55:35 ACCESS                  registry.vdf20210926195535
19:55:35 CLOSE_NOWRITE,CLOSE                  registry.vdf20210926195535
19:55:35 CLOSE_WRITE,CLOSE                  registry.vdf20210926195535
19:55:35 MOVED_FROM                  registry.vdf20210926195535
19:55:35 MOVED_TO                  registry.vdf20210926195535.tmp.swap
19:55:35 MOVED_FROM                  registry.vdf
19:55:35 MOVED_TO                  registry.vdf20210926195535
19:55:35 MOVED_FROM                  registry.vdf20210926195535.tmp.swap
19:55:35 MOVED_TO                  registry.vdf
19:55:35 DELETE                  registry.vdf20210926195535
19:55:35 OPEN                  registry.vdf
19:55:35 ACCESS                  registry.vdf
19:55:35 CLOSE_NOWRITE,CLOSE                  registry.vdf
19:55:35 OPEN                  registry.vdf
19:55:35 ACCESS                  registry.vdf
19:55:35 CLOSE_NOWRITE,CLOSE                  registry.vdf
19:55:36 OPEN                  registry.vdf
19:55:36 ACCESS                  registry.vdf
19:55:36 CLOSE_NOWRITE,CLOSE                  registry.vdf
19:55:36 CREATE                  registry.vdf20210926195536
19:55:36 OPEN                  registry.vdf20210926195536
19:55:36 MODIFY                  registry.vdf20210926195536
19:55:36 CLOSE_WRITE,CLOSE                  registry.vdf20210926195536
19:55:36 MOVED_FROM                  registry.vdf20210926195536
19:55:36 MOVED_TO                  registry.vdf20210926195536.tmp.swap
19:55:36 MOVED_FROM                  registry.vdf
19:55:36 MOVED_TO                  registry.vdf20210926195536
19:55:36 MOVED_FROM                  registry.vdf20210926195536.tmp.swap
19:55:36 MOVED_TO                  registry.vdf
19:55:36 DELETE                  registry.vdf20210926195536
19:55:36 OPEN                  registry.vdf
19:55:36 ACCESS                  registry.vdf
19:55:36 CLOSE_NOWRITE,CLOSE                  registry.vdf
19:55:36 OPEN                  registry.vdf
19:55:36 ACCESS                  registry.vdf
19:55:36 CLOSE_NOWRITE,CLOSE                  registry.vdf
19:55:36 CREATE                  registry.vdf20210926195536
19:55:36 OPEN                  registry.vdf20210926195536
19:55:36 MODIFY                  registry.vdf20210926195536
19:55:36 OPEN                  registry.vdf20210926195536
19:55:36 ACCESS                  registry.vdf20210926195536
19:55:36 CLOSE_NOWRITE,CLOSE                  registry.vdf20210926195536
19:55:36 CLOSE_WRITE,CLOSE                  registry.vdf20210926195536
19:55:36 MOVED_FROM                  registry.vdf20210926195536
19:55:36 MOVED_TO                  registry.vdf20210926195536.tmp.swap
19:55:36 MOVED_FROM                  registry.vdf
19:55:36 MOVED_TO                  registry.vdf20210926195536
19:55:36 MOVED_FROM                  registry.vdf20210926195536.tmp.swap
19:55:36 MOVED_TO                  registry.vdf
19:55:36 DELETE                  registry.vdf20210926195536
19:55:36 OPEN                  registry.vdf
19:55:36 ACCESS                  registry.vdf
19:55:36 CLOSE_NOWRITE,CLOSE                  registry.vdf
19:55:36 CREATE                  registry.vdf20210926195536
19:55:36 OPEN                  registry.vdf20210926195536
19:55:36 MODIFY                  registry.vdf20210926195536
19:55:36 OPEN                  registry.vdf20210926195536
19:55:36 ACCESS                  registry.vdf20210926195536
19:55:36 CLOSE_NOWRITE,CLOSE                  registry.vdf20210926195536
19:55:36 CLOSE_WRITE,CLOSE                  registry.vdf20210926195536
19:55:36 MOVED_FROM                  registry.vdf20210926195536
19:55:36 MOVED_TO                  registry.vdf20210926195536.tmp.swap
19:55:36 MOVED_FROM                  registry.vdf
19:55:36 MOVED_TO                  registry.vdf20210926195536
19:55:36 OPEN                  registry.vdf20210926195536
19:55:36 ACCESS                  registry.vdf20210926195536
19:55:36 CLOSE_NOWRITE,CLOSE                  registry.vdf20210926195536
19:55:36 MOVED_FROM                  registry.vdf20210926195536.tmp.swap
19:55:36 MOVED_TO                  registry.vdf
19:55:36 DELETE                  registry.vdf20210926195536
19:55:36 OPEN                  registry.vdf
19:55:36 ACCESS                  registry.vdf
19:55:36 CLOSE_NOWRITE,CLOSE                  registry.vdf

I was hoping to just be able to monitor the registry file, but since it gets deleted and replaced, I took the tack of just repeatedly trying to monitor it. Unfortunately, the watch sometimes fails (I'm presuming due to trying to start watching in the middle of a swap) That's why I've added the || true to the while loop.

I'm a bit concerned about the reliability of this, and was hoping a code review might come up with some ideas.

\$\endgroup\$

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.