summaryrefslogtreecommitdiff
path: root/noita-backup
blob: fa06d7c9e1825f18278f2c0969387806571e79dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/bin/sh
XDG_DATA_HOME="${XDG_DATA_HOME:-$HOME/.local/share}"
for datadir in "$XDG_DATA_HOME" /usr/local/share /usr/share; do
	NOITA_BACKUP_ICON="$datadir/icons/noita.png"
	if [ -e "$NOITA_BACKUP_ICON" ]; then
		break  # Contains value even if not found
	fi
done
COMPAT_DRIVE_C="$XDG_DATA_HOME/Steam/steamapps/compatdata/881100/pfx/drive_c"
NOITA_SAVE_DIR="$COMPAT_DRIVE_C/users/steamuser/AppData/LocalLow/Nolla_Games_Noita"
NOITA_BACKUP_DIR="${XDG_CACHE_HOME:-$HOME/.cache}/noita-backup"

notify() {
	notify-send -i "$NOITA_BACKUP_ICON" "Noita Backup" "$@"
}

stamp() {
	date +%Y-%m-%d-%H-%M-%S
}

backup() {
	id=$(notify -p "Creating a backup...")
	filename="save00.bak.$(stamp)"
	if mkdir -p "$NOITA_BACKUP_DIR" && cp -r "$NOITA_SAVE_DIR/save00" "$NOITA_BACKUP_DIR/$filename"; then
		notify -r $id "Saved as $filename"
	else
		notify -r $id "Could not backup the save"
	fi
}

restore() {
	latest="$(find "$NOITA_BACKUP_DIR" -name 'save00.bak.*' | sort -r | head -n1)"
	if [ -z "$latest" ]; then
		notify "No backups to restore from"
		return
	fi
	filename=$(basename "$latest")
	save="$NOITA_SAVE_DIR/save00"
	restored="$NOITA_BACKUP_DIR/save00.restored.$(stamp)"
	id=$(notify -p "Restoring $filename...")
	if mv "$save" "$restored" && cp -r "$latest" "$save"; then
		notify -r $id "Restored save from $filename"
	else
		notify -r $id "Could not restore save"
	fi
}

destroy() {
	rm -rf "$NOITA_BACKUP_DIR"
	notify "Removed all saves"
}

case "$0" in
	*backup-noita) backup;;
	*restore-noita) restore;;
	*destroy-noita) destroy;;
esac