#!/bin/bash

# get data.win checksum
if [ -f "gamedata/data.win" ]; then
	game_chksm=$(md5sum gamedata/"data.win" | awk '{print $1}')
else
	echo "WARNING: Game data not found; check the instructions."
	return 1
fi

itch_check=false
itch_patch=""
patch_id=""

case $game_chksm in
	29f820538024539f18171fb447034fe7) echo "Steam Version 1.1.1 Found. Patching data.win."; patch_id="vs.xdelta";;
	1a666b533539af4cebb7c12311bd9a56) echo "Itch Version 1.1.1 Found. Patching data.win."; itch_check=true; itch_patch="vs-itch-to-steam.xdelta"; patch_id="vs.xdelta";;
	*)
        echo "WARNING: Game checksum not found; check the instructions and your game version. data.win md5 ""$game_chksm"
        return 1
        ;;
esac

if [ "$itch_check" = "false" ]; then
	$ESUDO $controlfolder/xdelta3 -f -d -s gamedata/"data.win" gamedata/$patch_id gamedata/"vs-patched.win"
else
	mv gamedata/"data.win" gamedata/"data_itch.win"
	$ESUDO $controlfolder/xdelta3 -f -d -s gamedata/"data_itch.win" gamedata/$itch_patch gamedata/"data.win"
	$ESUDO $controlfolder/xdelta3 -f -d -s gamedata/"data.win" gamedata/$patch_id gamedata/"vs-patched.win"
	$ESUDO rm gamedata/"data_itch.win"
fi

if [ ! -f "gamedata/vs-patched.win" ]; then
	echo "WARNING: Patching unsuccessful; please check the instructions and try again. data.win md5 ""$game_chksm"
	return 1
fi

return 0