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.
##
2019-10-13 16:38:47 +02:00
## KISS : Only manage save directories from XDG. For other paths (Steam, home,…)
## check other scripts.
2019-10-09 20:30:17 +02:00
# }}}
# Vars {{{
2019-10-23 11:04:02 +02:00
debug=1
2019-10-09 20:30:17 +02:00
## XDG config {{{
XDG_CONFIG_HOME="${HOME}/.config"
xdg_config="$(printf "%s" "${XDG_CONFIG_HOME}" | sed -e "s;${HOME}/;;")"
2019-10-11 15:04:08 +02:00
### List of video games for XDG CONFIG {{{
2019-10-11 14:50:09 +02:00
2020-11-16 13:06:09 +01:00
### Broforce − unity3d − https://pcgamingwiki.com/wiki/Broforce
### Butcher − unity3d − https://pcgamingwiki.com/wiki/Butcher
### Darkwood − unity3d − https://pcgamingwiki.com/wiki/Darkwood
### Deadbolt − deadbolt_game − https://www.pcgamingwiki.com/wiki/Deadbolt
### Enter the Gungeon − unity3d − https://www.pcgamingwiki.com/wiki/Enter_the_Gungeon
### Fez − FEZ − https://www.pcgamingwiki.com/wiki/Fez
### Full Metal Furies − Cellar Door Games − https://pcgamingwiki.com/wiki/Full_Metal_Furies
### Galak-Z The Void − unity3d − https://www.pcgamingwiki.com/wiki/Galak-Z:_The_Dimensional
### Hyper Light Drifter − HyperLightDrifter − https://www.pcgamingwiki.com/wiki/Hyper_Light_Drifter
### Jump Gunners − unity3d − https://pcgamingwiki.com/wiki/Jump_Gunners
### Never Alone − unity3d − https://pcgamingwiki.com/wiki/Never_Alone
### Never Alone (bis) − unity3d − https://pcgamingwiki.com/wiki/Never_Alone
### Nuclear Throne − nuclearthrone − https://pcgamingwiki.com/wiki/Nuclear_Throne
### Overcooked! 2 − unity3d − https://pcgamingwiki.com/wiki/Overcooked!_2
### Risk of Rain − Risk_of_Rain − https://pcgamingwiki.com/wiki/Risk_of_Rain
### Streets of Rogue − unity3d − https://www.pcgamingwiki.com/wiki/Streets_of_Rogue
### The Dishwasher: Vampire Smile − VampireSmile − https://pcgamingwiki.com/wiki/The_Dishwasher:_Vampire_Smile
### Tricky Towers − unity3d − https://pcgamingwiki.com/wiki/Tricky_Towers
### Ultimate Chicken Horse − unity3d − https://www.pcgamingwiki.com/wiki/Ultimate_Chicken_Horse
2020-08-07 12:22:12 +02:00
xdg_config_games="unity3d%Cellar Door Games%deadbolt_game%FEZ%HyperLightDrifter%nuclearthrone%Risk_of_Rain%VampireSmile"
2019-10-11 14:50:09 +02:00
### }}}
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}/;;")"
2019-10-11 15:04:08 +02:00
### List of video games for XDG DATA {{{
2020-11-16 12:51:24 +01:00
### Alien: Isolation − feral-interactive − https://www.pcgamingwiki.com/wiki/Alien:_Isolation
2020-07-02 15:57:37 +02:00
### Borderlands: The Pre-Sequel − aspyr-media − https://www.pcgamingwiki.com/wiki/Borderlands:_The_Pre-Sequel
### Borderlands 2 − aspyr-media − https://pcgamingwiki.com/wiki/Borderlands_2
### Capsized − Capsized − https://pcgamingwiki.com/wiki/Capsized
### Dust: An Elysian Tail − DustAET − https://www.pcgamingwiki.com/wiki/Dust:_An_Elysian_Tail
### Full Metal Furies − Cellar Door Games − https://pcgamingwiki.com/wiki/Full_Metal_Furies
### HotlineMiami − HotlineMiami − https://pcgamingwiki.com/wiki/Hotline_Miami
### Mercenary Kings − Tribute Games − https://pcgamingwiki.com/wiki/Mercenary_Kings
2020-11-16 13:01:30 +01:00
### Move or Die − love − https://www.pcgamingwiki.com/wiki/Move_or_Die#Configuration_file.28s.29_location
2020-11-16 12:54:46 +01:00
### N++ − Metanet − https://www.pcgamingwiki.com/wiki/N%2B%2B
2020-07-02 15:57:37 +02:00
### PixelJunk Shooter − PJShooter − https://pcgamingwiki.com/wiki/PixelJunk_Shooter
### Saints Row: The Third − vpltd − https://pcgamingwiki.com/wiki/Saints_Row:_The_Third
### Saints Row IV − vpltd − https://pcgamingwiki.com/wiki/Saints_Row_IV
2019-10-11 15:04:08 +02:00
### The Dishwasher: Vampire Smile − VampireSmile − https://pcgamingwiki.com/wiki/The_Dishwasher:_Vampire_Smile
2020-07-02 15:57:37 +02:00
### The Swapper − Facepalm Games − https://pcgamingwiki.com/wiki/The_Swapper
### TowerFall Ascension − TowerFall − https://pcgamingwiki.com/wiki/TowerFall_Ascension
2020-11-16 13:01:30 +01:00
xdg_data_games="feral-interactive%aspyr-media%Capsized%DustAET%Cellar Door Games%HotlineMiami%Tribute Games%love%Metanet%PJShooter%vpltd%VampireSmile%Facepalm Games%TowerFall"
2019-10-11 15:04:08 +02:00
### }}}
2019-10-09 20:30:17 +02:00
## }}}
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."
2019-10-26 18:19:58 +02:00
return 0
2019-10-09 20:37:26 +02:00
else
[ "${debug}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG : Symlink game — ${_game_name} doesn't have remote data."
2019-10-26 18:19:58 +02:00
return 1
2019-10-09 20:37:26 +02:00
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
2019-10-13 20:32:20 +02:00
[ "${debug}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG : Run save game script for XDG."
2019-10-10 22:46:35 +02:00
# Manage XDG config save game {{{
2019-10-11 17:39:36 +02:00
## Set "%" as field separator
IFS="%"
2019-10-10 22:46:35 +02:00
for game_name in ${xdg_config_games}; do
local_game_path="${HOME}/${xdg_config}/${game_name}"
2019-10-26 18:19:58 +02:00
local_game_path_type="$(ls -ld "${local_game_path}" 2> /dev/null | sed 's/\(^.\).*/\1/')"
2019-10-10 22:46:35 +02:00
case ${local_game_path_type} in
## Data is already a symlink
2019-10-11 17:40:05 +02:00
"symbolic"|"l")
2019-10-10 22:46:35 +02:00
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
2019-10-11 17:40:05 +02:00
"directory"|"d")
2019-10-10 22:46:35 +02:00
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
*)
2019-10-26 18:19:58 +02:00
### Still try to symlink the savegame directory
symlink_game_dir "${game_name}" "${xdg_config}"
### If the symlink failed
if [ "${?}" != 0 ]; then
printf '\e[1;35m%-6s\e[m\n' "Data of ${game_name} (XDG CONFIG) − ${local_game_path} are not managed. Type: ${local_game_path_type}. Abort"
exit 3
fi
2019-10-10 22:46:35 +02:00
;;
esac
done
# }}}
2019-10-11 17:57:59 +02:00
# Manage XDG data save game {{{
## Set "%" as field separator
IFS="%"
for game_name in ${xdg_data_games}; do
local_game_path="${HOME}/${xdg_data}/${game_name}"
2019-10-26 18:19:58 +02:00
local_game_path_type="$(ls -ld "${local_game_path}" 2> /dev/null | sed 's/\(^.\).*/\1/')"
2019-10-11 17:57:59 +02:00
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 : XDG DATA for loop — The data of ${game_name} are already symlinked to ${link_name} . Skip."
;;
## Data is still a directory
"directory"|"d")
move_game_dir "${game_name}" "${xdg_data}"
;;
## Data doesn't exist
"cannot")
[ "${debug}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG : XDG DATA for loop — The data of ${game_name} − ${local_game_path} doesn't exist. Skip."
symlink_game_dir "${game_name}" "${xdg_data}"
;;
## Data can't be managed
*)
2019-10-26 18:19:58 +02:00
### Still try to symlink the savegame directory
symlink_game_dir "${game_name}" "${xdg_data}"
### If the symlink failed
if [ "${?}" != 0 ]; then
printf '\e[1;35m%-6s\e[m\n' "Data of ${game_name} (XDG DATA) − ${local_game_path} are not managed. Type: ${local_game_path_type}. Abort"
exit 3
fi
2019-10-11 17:57:59 +02:00
;;
esac
done
# }}}
exit 0