2019-10-09 20:30:17 +02:00
#!/bin/sh
# Purpose {{{
## Try to centralize all game's save that respect XDG specifications 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 XDG's
## directories to a remote directory (Nextcloud, remote mount,…).
## Then create a symlink in XDG directories 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 Steam userdata. For other
## paths (Steam,…) check other scripts.
# }}}
# Vars {{{
debug=0
## XDG config {{{
XDG_CONFIG_HOME="${HOME}/.config"
xdg_config="$(printf "%s" "${XDG_CONFIG_HOME}" | sed -e "s;${HOME}/;;")"
2019-10-10 22:46:35 +02:00
xdg_config_games="..."
2019-10-09 20:30:17 +02:00
## }}}
## XDG data {{{
XDG_DATA_HOME="${HOME}/.local/share"
xdg_data="$(printf "%s" "${XDG_DATA_HOME}" | sed -e "s;${HOME}/;;")"
data_games="..."
## }}}
remote_dir="${HOME}/Nextcloud/games/linux.sgl.script"
# }}}
2019-10-09 20:37:26 +02:00
# 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
}
# }}}
# }}}
2019-10-09 20:30:17 +02:00
# 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
## }}}
## Ensure XDG directories exist {{{
for xdg_dir in "${xdg_config}" "${xdg_data}"; do
local_xdg_path="${HOME}/${xdg_dir}"
if [ ! -d "${local_xdg_path}" ]; then
printf '\e[1;35m%-6s\e[m\n' "The XDG directory − ${xdg_dir} doesn't exists yet… Should it must be create (for restoration,…) [Y/n] ?"
read -r create_local_xdg
if [ "${create_local_xdg}" = "" ] || [ "${create_local_xdg}" = "Y" ] || [ "${create_local_xdg}" = "y" ]; then
mkdir -p -- "${local_xdg_path}"
else
printf '\e[1;35m%-6s\e[m\n' "XDG directory (${xdg_dir}) doesn't exists, abort script."
exit 2
fi
fi
done
## }}}
# }}}
2019-10-10 22:46:35 +02:00
# Manage XDG config save game {{{
for game_name in ${xdg_config_games}; do
local_game_path="${HOME}/${xdg_config}/${game_name}"
local_game_path_type="$(ls -ld "${local_game_path}" | sed 's/\(^.\).*/\1/')"
case ${local_game_path_type} in
## Data is already a symlink
"symbolic")
link_name="$(file "${local_game_path}" | sed 's;.* symbolic link to \(.*\);\1;')"
[ "${debug}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG : XDG CONFIG for loop — The data of ${game_name} are already symlinked to ${link_name} . Skip."
;;
## Data is still a directory
"directory")
move_game_dir "${game_name}" "${xdg_config}"
;;
## Data doesn't exist
"cannot")
[ "${debug}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG : XDG CONFIG for loop — The data of ${game_name} − ${local_game_path} doesn't exist. Skip."
symlink_game_dir "${game_name}" "${xdg_config}"
;;
## Data can't be managed
*)
printf '\e[1;35m%-6s\e[m\n' "Data of ${game_name} (userdata) − ${local_game_path} are not managed. Type: ${local_game_path_type}. Abort"
exit 3
;;
esac
done
# }}}