#!/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=1

## 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
###        ^
### Overcooked! 2           −  Team17     − https://pcgamingwiki.com/wiki/Overcooked!_2
### Trine Enchanted Edition − .frozenbyte − https://pcgamingwiki.com/wiki/Trine_Enchanted_Edition
### Trine 2 Complete Story  − .frozenbyte − https://pcgamingwiki.com/wiki/Trine_2
games_list="Team17%.frozenbyte"
## }}}

remote_dir="${HOME}/Nextcloud/games/linux.sgl.script"
home_remote_dir="${remote_dir}/home"

# }}}

# 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="${home_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="${home_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."
		return 0
	else
		[ "${debug}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG : Symlink game — ${_game_name} doesn't have remote data."
		return 1
	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
## }}}

# }}}

		[ "${debug}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG : Run save game script for Homedir."

# 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}" 2> /dev/null | sed 's/\(^.\).*/\1/')"

	game_name="$(basename "${game_path}")"
	game_dir="$(dirname "${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
		*)
			### Still try to symlink the savegame directory
			symlink_game_dir "${game_name}" "${game_dir}"
			### If the symlink failed
			if [ "${?}" != 0 ]; then
				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
			fi
			;;
	esac

done
# }}}

exit 0