From 2a63b0c096ffa03367e41f93e63e244014e224ba Mon Sep 17 00:00:00 2001 From: Gardouille Date: Sun, 13 Oct 2019 16:39:00 +0200 Subject: [PATCH] Prepare a script to manage extra save games in home aka save path outside of XDG or Steam. --- games/save.game.home | 124 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100755 games/save.game.home diff --git a/games/save.game.home b/games/save.game.home new file mode 100755 index 0000000..56234e5 --- /dev/null +++ b/games/save.game.home @@ -0,0 +1,124 @@ +#!/bin/sh + +# Purpose {{{ +## Try to centralize all game's save that doesn't respect XDG specifications +## or Steam directories in order to : +## easily backup all save +## easily restore it +## be able to access it from anywhere +## … all you can do with a Nextcloud (share, versionning,…) +## +## 1. Move save directories of a list of known games from homedir (or subdir) +## to a remote directory (Nextcloud, remote mount,…). +## Then create a symlink in home directory to the remote game directory. +## +## 2. If a directory doesn't exist, check if a remote one is available +## and symlink it. +## +## KISS : Only manage save directories from home directory (or subdir). +## For other paths (Steam,…) check other scripts. +# }}} + +# Vars {{{ +debug=0 + +## List of video games for homedir {{{ +### Give the relative path from homedir. +### Separate each path with "%" to be able to manage white space, eg : +### .mygame%.first-subtree/save of GAMEX +### +games_list="..." +## }}} + +remote_dir="${HOME}/Nextcloud/games/linux.sgl.script" + +# }}} + +# Functions {{{ +# Move one save game dir {{{ +move_game_dir() { + _game_name="${1}" + _game_dir="${2}" + _local_game_path="${HOME}/${_game_dir}/${_game_name}" + _remote_game_path="${remote_dir}/${_game_dir}/${_game_name}" + + ## If a remote directory doesn't already exists for this game + if [ ! -d "${_remote_game_path}" ]; then + ### Ensure to create tree directories on remote storage + mkdir -p -- "$(dirname "${_remote_game_path}")" + ### Move data to remote storage + mv -- "${_local_game_path}" "${_remote_game_path}" + [ "${debug}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG : Move game − The data of ${_game_name} − ${_local_game_path} moved to remote storage." + ### Then ask to create a symbolic link to local storage + symlink_game_dir "${_game_name}" "${_game_dir}" + else + printf '\e[1;35m%-6s\e[m\n' "Move game − ${_game_name} already have data on remote storage : ${_remote_game_path}. Abort to avoid to override data." + exit 5 + fi +} +# }}} +# Symlink one save game dir from remote to local {{{ +symlink_game_dir() { + _game_name="${1}" + _game_dir="${2}" + _local_game_path="${HOME}/${_game_dir}/${_game_name}" + _remote_game_path="${remote_dir}/${_game_dir}/${_game_name}" + + if [ -d "${_remote_game_path}" ]; then + ln -s -- "${_remote_game_path}" "${_local_game_path}" + [ "${debug}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG : Symlink game — Symlink remote data of ${_game_name} to local storage." + else + [ "${debug}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG : Symlink game — ${_game_name} doesn't have remote data." + fi +} +# }}} + +# }}} + +# Tests {{{ + +## Ensure remote dir exist {{{ +if [ ! -d "${remote_dir}" ]; then + printf '\e[1;35m%-6s\e[m\n' "The directory for save game doesn't exists : ${remote_dir}" + exit 1 +fi +## }}} + +# }}} + +# Manage save game from homedir {{{ +## Set "%" as field separator +IFS="%" +for game_path in ${games_list}; do + local_game_path="${HOME}/${game_path}" + local_game_path_type="$(ls -ld "${local_game_path}" | sed 's/\(^.\).*/\1/')" + + game_name="$(basename "${game_path}")" + game_dir="$(direname "${game_path}")" + + case ${local_game_path_type} in + ## Data is already a symlink + "symbolic"|"l") + link_name="$(file "${local_game_path}" | sed 's;.* symbolic link to \(.*\);\1;')" + [ "${debug}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG : Homedir for loop — The data of ${game_path} are already symlinked to ${link_name} . Skip." + ;; + ## Data is still a directory + "directory"|"d") + move_game_dir "${game_name}" "${game_dir}" + ;; + ## Data doesn't exist + "cannot") + [ "${debug}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG : Homedir for loop — The data of ${game_path} − ${local_game_path} doesn't exist. Skip." + symlink_game_dir "${game_name}" "${game_dir}" + ;; + ## Data can't be managed + *) + printf '\e[1;35m%-6s\e[m\n' "Data of ${game_path} (Homedir) − ${local_game_path} are not managed. Type: ${local_game_path_type}. Abort" + exit 3 + ;; + esac + +done +# }}} + +exit 0