#!/bin/sh
# Simple two way game-save sync
# NOTE: This will ALLWAYS replace the backups with local folders,
# even if they are empty folders

command -v xdg-user-dir &> /dev/null &&
    DOCUMETS="$(xdg-user-dir DOCUMENTS)" ||
    DOCUMETS=~/Documents

SAVES_DIR="$DOCUMETS/GameSaves/"
test -d "$SAVES_DIR" || exit 1

test_rsync(){
    test -d "$1" && {
        mkdir -p $(dirname "$2")
        cp --recursive --no-target-directory "$1" "$2"
    }
}

game_save(){
    GAME_NAME="$1"; shift
    INSTALL_DIR="${1/"~"/$HOME}/"; shift

    BACKUP_DIR="$SAVES_DIR/$GAME_NAME/"

    test_rsync "$INSTALL_DIR" "$BACKUP_DIR" ||
    test_rsync "$BACKUP_DIR" "$INSTALL_DIR" ||
    echo "Nothing to do for $GAME_NAME"

    while test -n "$1"
    do
        find "$BACKUP_DIR" -name "$1" -delete
        shift
    done
}

game_save mindustry "~/.local/share/Mindustry"
game_save minecraft "~/.minecraft/saves"
game_save factorio  "~/.factorio" '_autosave*.zip' '*.sync-conflict-*'
game_save cuphead   "~/Games/cuphead/drive_c/users/lelgenio/Application Data/Cuphead"

