#!/bin/bash

EA_PFX_ID=4172646814
PVZ_PFX_ID=3273286877
BACKUP_PATH=$HOME/GameBackups

echo "EA-App Prefix      : "$EA_PFX_ID
echo "PvZ Prefix         : "$PVZ_PFX_ID
echo "Backup Destination : "$BACKUP_PATH
echo
read -p "If you want to use these defaults, enter 'yes': " USER_CONFIRM 

if [[ ! "$USER_CONFIRM" =~ ^(yes|Yes|YES|y|ja|j|JA|Ja|ok|OK|okay|OKAY|Ok|Okay)$ ]]; then

  while true; do
    read -p "Please enter the Steam-Game ID of the EA-App: " EA_PFX_ID

    if [[ "$EA_PFX_ID" =~ ^[0-9]+$ ]]; then
      break
    else
      echo "Invalid input. Please enter a numeric Game ID."
    fi
  done
  
  while true; do
    read -p "Please enter the Steam-Game ID of the Plants vs. Zombies Game: " PVZ_PFX_ID

    if [[ "$PVZ_PFX_ID" =~ ^[0-9]+$ ]]; then
      break
    else
      echo "Invalid input. Please enter a numeric Game ID."
    fi
  done

  while true; do
    read -p "Please enter the BackupDestination " BACKUP_PATH
    read -p "If $BACKUP_PATH is correct, enter 'yes': " USER_CONFIRM
    if [[ "$USER_CONFIRM" =~ ^(yes|Yes|YES|y|ja|j|JA|Ja|ok|OK|okay|OKAY|Ok|Okay)$ ]]; then
      break
    fi
  done
fi

mkdir -p $BACKUP_PATH/ea
mkdir -p $BACKUP_PATH/pvz

if [ -d "$BACKUP_PATH" ]; then

  available_space=$(df -Pk "$BACKUP_PATH" | awk 'NR==2 {print $4}')
  required_space=$(du -sk "$HOME/.steam/steam/steamapps/compatdata/$EA_PFX_ID/pfx/drive_c/Program\ Files/EA\ Games/" | cut -f1)

  if [ "$available_space" -lt "$required_space" ]; then
    echo "Insufficient disk space. Please free up space or choose a different backup destination."
    exit
  fi

  echo "Backup of EA Game Library…";
  rsync -ahPz $HOME/.steam/steam/steamapps/compatdata/$EA_PFX_ID/pfx/drive_c/Program\ Files/EA\ Games/  $BACKUP_PATH/ea/lib


  available_space=$(df -Pk "$BACKUP_PATH" | awk 'NR==2 {print $4}')
  required_space=$(du -sk "$HOME/.steam/steam/steamapps/compatdata/$EA_PFX_ID/pfx/drive_c/users/steamuser/Documents/" | cut -f1)

  if [ "$available_space" -lt "$required_space" ]; then
    echo "Insufficient disk space. Please free up space or choose a different backup destination."
    exit
  fi

  echo "Backup of EA SaveGames…"
  rsync -ahPz $HOME/.steam/steam/steamapps/compatdata/$EA_PFX_ID/pfx/drive_c/users/steamuser/Documents/ $BACKUP_PATH/ea/docs

  available_space=$(df -Pk "$BACKUP_PATH" | awk 'NR==2 {print $4}')
  required_space=$(du -sk "$HOME/.steam/steam/steamapps/compatdata/$PVZ_PFX_ID/pfx/drive_c/ProgramData/PopCap\ Games/PlantsVsZombies/userdata/" | cut -f1)

  if [ "$available_space" -lt "$required_space" ]; then
    echo "Insufficient disk space. Please free up space or choose a different backup destination."
    exit
  fi

  echo "Backup of PvZ SaveGames"
  rsync -ahPz $HOME/.steam/steam/steamapps/compatdata/$PVZ_PFX_ID/pfx/drive_c/ProgramData/PopCap\ Games/PlantsVsZombies/userdata/ $BACKUP_PATH/pvz/userdata
  
else
  echo -e "\b [failed]\nAborting"
  exit
fi

sleep 1

echo "The main purpose of this script is to provide an easy way to circumvent the EA Apps stupid Updates because they break Proton support every time."
echo "How to circumvent:"
echo -e "\t1st: Back Up you Game Library and you savegames [done]"
echo -e "\t2nd: Start Steam in BPM and delete Proton files for the EA-App"
echo -e "\t3rd: Download the EA-App installer from the Internet, and reinstall the App"
echo -e "\t4th: Put your Game-Library and your savegames back into place"
echo
read -p "This Script helped you to complete Task 1. If you want to proceed, complete Tasks 2 and 3 and then come back. Do you want to complete Task 4 now? (if so, enter 'yes')" USER_CONFIRM

if [[ "$USER_CONFIRM" =~ ^(yes|Yes|YES|y|ja|j|JA|Ja|ok|OK|okay|OKAY|Ok|Okay)$ ]]; then
  echo "Recovering EA-Game Library and EA-Savegames (THIS WILL NOT RECOVER YOUR PVZ SAVEGAMES)…"

  rsync -ahPz $BACKUP_PATH/ea/lib/ $HOME/.steam/steam/steamapps/compatdata/$EA_PFX_ID/pfx/drive_c/Program\ Files/EA\ Games
  rsync -ahPz $BACKUP_PATH/ea/docs/ $HOME/.steam/steam/steamapps/compatdata/$EA_PFX_ID/pfx/drive_c/users/steamuser/Documents
fi
