#!/usr/bin/env bash
# script-backup-bundle
# Create Bundle from existing wine prefix
# Steps:
# 1. Exclude Directories for bundling
# 2. Calculate Backup Size based on Gamedir whether it is in Prefix or not
# 3. Prepare Prefix Name
# 4. Ask file name for the prefix / bundle
# 5. Ask user to select backup target/destination directory
# 6. Ask for Compression Levels
# 7. Create wzt = tar.zst file
# 8. Show info about created wzt file

script-backup-bundle()
{
  dbug "I: Script: Launched ${FUNCNAME[0]}"
    
  SOURCE "script-check-variables-loaded-or-not"
  script-check-variables-loaded-or-not
  HEADER="Script: ${FUNCNAME[0]}"
  dbug "I: ${HEADER}: Checking if ${EXE_NAME} is inside a standard directory!"
  
  GAME_DIR_NAME=$(realpath -m "${EXE_PATH}")

  if [ ! -d "${GAME_DIR_NAME}" ] || [ ! -f "${EXE_FILE}" ]; then 
       ${ZENITY_CMD} \
       --error --title="Game directory not found" \
       --text="${GAME_DIR_NAME}\ndirectory moved? partition not mounted?"
       return 1
  fi

  # 1. Exclude Directories for bundling
  # Do not allow game bundle creation if the user has .exe inside
  # standard directories like ~/Documents ~/Downloads etc. to avoid huge file
  unset DO_NOT_BUNDLE_FROM
  # 1.1 List of standard Linux directories to avoid
  # 1.1.1 Get root dirs using find to handle of different OS
  ROOT_DIRS="$(find / -maxdepth 1 -type d)"
  
  # 1.1.2 Get Mounted Partitions using df command
  # Use df command instead of mount
  MOUNTED_PARTITIONS="$(df |grep "/"|tr -s " "|cut -f6 -d " ")"
  # 1.1.3 Add Root dirs and mounted partitions to Do_not_bundle variable
  DO_NOT_BUNDLE_FROM+="${ROOT_DIRS} "
  DO_NOT_BUNDLE_FROM+="${MOUNTED_PARTITIONS} "
  # 1.1.4 Manually include these known directories
  DO_NOT_BUNDLE_FROM+="/    /boot /dev  /etc  /home /media "
  DO_NOT_BUNDLE_FROM+="/mnt /opt  /proc /root /run "
  DO_NOT_BUNDLE_FROM+="/srv /sys  /tmp  /usr  /var "
  DO_NOT_BUNDLE_FROM+="${HOME} "
  DO_NOT_BUNDLE_FROM+="${HOME}/Desktop    ${HOME}/Documents "
  DO_NOT_BUNDLE_FROM+="${HOME}/Downloads  ${HOME}/Music "
  DO_NOT_BUNDLE_FROM+="${HOME}/Pictures   ${HOME}/Public "
  DO_NOT_BUNDLE_FROM+="${HOME}/Templates  ${HOME}/Videos "

   ### Debug Check variables which are used in this file up to this point
   SOURCE "check-all-variables"
   echo "-------------------------------${FUNCNAME[0]}------------------------------------------"
   check-all-variables "HEADER GAME_DIR_NAME EXE_NAME EXE_PATH EXE_FILE ROOT_DIRS MOUNTED_PARTITIONS DO_NOT_BUNDLE_FROM"


  # 1.2 If game.exe file is inside standard (DO_NOT_BUNDLE_FROM) directories
  # Tell user to copy to a separate gamedir then try to create bundle
  if [[ "${DO_NOT_BUNDLE_FROM}" == *"${EXE_PATH}"* ]]; then
       MSG1="Will not create bundle with ${EXE_PATH} as Game directory"
       MSG2="Copy/Move files to a different directory to create bundle!"

       dbug "I: Script: ${FUNCNAME[0]}: ${EXE_PATH}"
       echo "E: Script: ${FUNCNAME[0]}: ${MSG1}"
       echo "E: Script: ${FUNCNAME[0]}: ${MSG2}"
       echo "E:${FUNCNAME[0]}: Cancelling Backup!"
       ${ZENITY_CMD} --error --no-wrap --text "${MSG1}\n${MSG2}" \
             --title "Cannot Bundle ${EXE_PATH}"
       unset DO_NOT_BUNDLE_FROM
       Script_Window
  fi

  unset DO_NOT_BUNDLE_FROM


  # Bundle Runner into the wzt file as tar file
  # if the runner is inside WINEZGUIDIR/RUNNERS
  if [[ "${RUNNER}" == *"${RUNNERS_DIR}"* ]]; then
       PREFIX_RUNNER="${PREFIXDIR}/Runner"
       mkdir -p "${PREFIX_RUNNER}" && dbug "---- Created ${PREFIX_RUNNER}"
       cp -rfp  "${RUNNERS_DIR}/${RUNNER_NAME}" "${PREFIX_RUNNER}" && dbug "copied runner" && \
       sed "s|${RUNNERS_DIR}|${PREFIX_RUNNER}|g" -i "${INFOFILE}"
  fi
  
  #Debug 
  echo "
  ---- script-bckup-bundle ----
  RUNNER=$RUNNER
  RUNNERS_DIR=$RUNNERS_DIR
  PREFIXDIR=$PREFIXDIR
  PREFIX_RUNNER=$PREFIX_RUNNER
  RUNNER_NAME=$RUNNER_NAME
  INFOFILE=$INFOFILE
  "
  #Remove drive_c/user/Temp/ all files, proton has steamuser, while wine uses $user
  find "${PREFIXDIR}/drive_c/users/steamuser/Temp" -type f -delete 
  find "${PREFIXDIR}/drive_c/users/${USER}/Temp"   -type f -delete
   

  # 2. Calculate Backup Size based on Gamedir whether it is in Prefix or not
  # If gamedir is inside prefixdir, total backup size will be prefix size
  if [[ "${EXE_FILE}" == *"${PREFIXDIR}"* ]] ; then
       dbug "I: Script: ${FUNCNAME[0]}: Game Directory inside Prefix!"
       # Do not include GAMEDIR separately when it is already inside prefix
       TOTAL_SIZE=$(expr ${PREFIX_SIZE_IN_BYTES})
       TOTAL_SIZE_IN_HF=$(echo ${TOTAL_SIZE}|numfmt --to iec)
       # Show only Prefix size
       unset TEXT
       # for zenity prompt
       TEXT+="${PREFIX_SIZE_IN_HF} ${PREFIXDIR}"
       
       ### Debug Check variables which are used in this file up to this point
       SOURCE "check-all-variables"
       echo "-------------------------------${FUNCNAME[0]}------------------------------------------"
       check-all-variables "EXE_FILE PREFIXDIR TOTAL_SIZE PREFIX_SIZE_IN_BYTES TOTAL_SIZE_IN_HF TEXT"
             
  else
       # If gamedir is outside prefixdir, total size = prefix size + gamedir
       GAMEDIR_DISK_USAGE_IN_HF=$(du -sh "${EXE_PATH}"|cut -f1)
         #Check whether du gave a value or not
       if [ -z "${GAMEDIR_DISK_USAGE_IN_HF}" ]; then
           echo "-------------------- Error --------------------------"
           echo "Error: ${FUNCNAME[0]}: GAMEDIR_DISK_USAGE_IN_HF not assigned"
           echo "-------------------- Error --------------------------"
       fi
       GAMEDIR_DU_IN_BYTES=$(echo ${GAMEDIR_DISK_USAGE_IN_HF} \
                            |cut -f1 -d " " |numfmt --from iec)
       # Calculate total backup size = prefix + gamedir
       TOTAL_SIZE=$(expr ${PREFIX_SIZE_IN_BYTES} + $GAMEDIR_DU_IN_BYTES)
       TOTAL_SIZE_IN_HF=$(echo ${TOTAL_SIZE}|numfmt --to iec)   
       # Show Prefix + Gamedir size
       unset TEXT
       # for zenity prompt
       TEXT+="${GAMEDIR_DISK_USAGE_IN_HF} ${EXE_PATH}\n"
       TEXT+="${PREFIX_SIZE_IN_HF} ${PREFIXDIR}"

       ### Debug Check variables which are used in this file up to this point
       SOURCE "check-all-variables"
       echo "-------------------------------${FUNCNAME[0]}------------------------------------------"
       check-all-variables "GAMEDIR_DISK_USAGE_IN_HF EXE_PATH GAMEDIR_DU_IN_BYTES TOTAL_SIZE PREFIX_SIZE_IN_BYTES \
                            GAMEDIR_DU_IN_BYTES GAMEDIR_DU_IN_BYTES TOTAL_SIZE TEXT"
  fi


  dbug "I: Script: ${FUNCNAME[0]}: Prefix  Size:${PREFIX_SIZE_IN_HF}"
  dbug "I: Script: ${FUNCNAME[0]}: Gamedir Size:${GAMEDIR_DISK_USAGE_IN_HF}"
  dbug "I: Script: ${FUNCNAME[0]}: Bundle  Size:${TOTAL_SIZE_IN_HF}"


  # 3. Prepare Prefix Name
  # 3.1 Format filename for flatpak or system install type
  if [ "${INSTALL_TYPE}" = "flatpak" ]; then
       # If "flatpak-" is not part of flatpak name
       # like WineZGUI flatpak name: io.github.fastrizwaan.WineZGUI, add it
       if [[ "$SHORT_FLATPAK_NAME" != *"flatpak-"* ]]; then
            SHORT_FLATPAK_NAME="flatpak-${SHORT_FLATPAK_NAME}"
       fi
       unset FILENAME
       FILENAME+="${PROGNAME}-${APP_WITH_VER}"
       FILENAME+="-Game-Bundle"
       # Do not use flatpak name in wzt file
       #FILENAME+="${SHORT_FLATPAK_NAME}"
       FILENAME+="-${DATE}"
       FILENAME+=".${BKP_EXT}"
       # Game-WineZGUI-0.87-Game-Bundle-wine-7.0-20220904_2359.wzt
 
       ### Debug Check variables which are used in this file up to this point
       SOURCE "check-all-variables"
       echo "-------------------------------${FUNCNAME[0]}------------------------------------------"
       check-all-variables "SHORT_FLATPAK_NAME FILENAME PROGNAME APP_WITH_VER DATE BKP_EXT"
  else # for system add wine-version
       unset FILENAME
       FILENAME+="${PROGNAME}-${APP_WITH_VER}"
       FILENAME+="-Game-Bundle-"
       FILENAME+="${WINEVER}"
       FILENAME+="-${DATE}"
       FILENAME+=".${BKP_EXT}"
       # Game-WineZGUI-0.87-Game-Bundle-wine-7.0-20220904_2359.wzt
 
       ### Debug Check variables which are used in this file up to this point
       SOURCE "check-all-variables"
       echo "-------------------------------${FUNCNAME[0]}------------------------------------------"
       check-all-variables "PROGNAME APP_WITH_VER WINEVER DATE BKP_EXT FILENAME DATADIR"
  fi
  # remove spaces->_ from FILENAME
  FILENAME=$(echo ${FILENAME}|tr " " "_")
  dbug "I: Script: ${FUNCNAME[0]}: Filename Suggestion: ${FILENAME}"

  # For Zenity prompt
  unset TITLE
  TITLE="Creating bundle for ${PREFIXNAME}"
  
  # Zenity 4 looks good
  # 4. Ask file name for the prefix / bundle
  dbug "I: Script: ${FUNCNAME[0]}: Asking filename"
  BACKUP_FILE=$(${ZENITY_CMD}          \
            --title="${TITLE}"         \
            --width=700 --height=30    \
            --text="${TEXT}"           \
            --entry-text="${FILENAME}" \
            --entry)


  # Step 4.1: if backup file is empty / cancel clicked
  if [ -z "${BACKUP_FILE}" ]; then
       dbug "I: Script: ${FUNCNAME[0]}: Cancelled!"
       unset BACKUP_FILE
       unset FILENAME
       Script_Window
  fi

  # Check if user entered symbols
  SOURCE "winezgui-check-entry-text"
  winezgui-check-entry-text "${BACKUP_FILE}"
  
  ENTRY_CHECK=$?
  if [ ${ENTRY_CHECK} -eq 1 ]; then
       dbug "I: Script: ${FUNCNAME[0]}: Symbols used in filename, cancelling!"
       return 1
  fi
  
  # 5. Ask user to select backup target/destination directory
  dbug "I: Script: ${FUNCNAME[0]}: Select backup directory"
  unset TITLE
  TITLE="Choose Backup Directory for ${BACKUP_FILE}!"

  TARGET_DIR=$(${ZENITY_CMD}  --file-selection --title="${TITLE}" --directory)
  if [ -z "${TARGET_DIR}" ]; then
      dbug "I: Script: ${FUNCNAME[0]}: Select backup directory: Cancelled!"
      Script_Window
  fi

  # 5.1: Check if the target directory is writable or not?
  if [ ! -w "${TARGET_DIR}" ]; then
       echo "E: Script: ${FUNCNAME[0]}: ${TARGET_DIR} not writable"
       ${ZENITY_CMD} --error --no-wrap --title="Backup Error!" \
                     --text "${TARGET_DIR} not writable\nAborting Backup!"
       Script_Window
  fi

  # Step 5.2 Required extension is .${BKP_EXT} i.e., .wzt, if not found add it
  dbug "I: Script: ${FUNCNAME[0]}: Given ${BACKUP_FILE}"

  if [[ "${BACKUP_FILE}" != *".${BKP_EXT}" ]]; then
       BACKUP_FILE="$(echo ${BACKUP_FILE}.${BKP_EXT})"
       dbug "I: Script: ${FUNCNAME[0]}: added suffix ${BKP_EXT}"
  fi

  dbug "I: Script: ${FUNCNAME[0]}: Using ${BACKUP_FILE}"

  # Step 5.3: Check for available disk space in target directory before backing up
  # to make sure that prefix size < target dir for backup to start

  # Step 5.4: Get available target dir partition's available space in
  # Human format (df -h)
  dbug "I: Script: ${FUNCNAME[0]}: Determining available space"
  AVAILABLE_SPACE=$(df -h "${TARGET_DIR}"|tr -s " "|tail -n1|cut -f4 -d " ")
  
  #Check whether df gave a value or not
  if [ -z "${AVAILABLE_SPACE}" ]; then
       echo "-------------------- Error --------------------------"
       echo "Error: ${FUNCNAME[0]}: AVAILABLE_SPACE not assigned"
       echo "-------------------- Error --------------------------"
  fi

  dbug "I: Script: ${FUNCNAME[0]}: Available space is:" \
       "${AVAILABLE_SPACE}"

  # Step 5.5: Convert the above $AVAILABLE_SPACE into bytes
  # for comparison
  AVAILABLE_SPACE_IN_BYTES=$(echo ${AVAILABLE_SPACE}|numfmt --from iec)

  dbug "I: ${HEADER}: Available space in Human: ${AVAILABLE_SPACE}"
  dbug "I: ${HEADER}: Available space in bytes: ${AVAILABLE_SPACE}"
 
  
  # Step 5.6: If Target directory does not have sufficient space, abort backup
  dbug "I: Script: ${FUNCNAME[0]}: Checking if size < target dir space"
  if [ ${AVAILABLE_SPACE_IN_BYTES} -gt ${TOTAL_SIZE} ];then
       dbug "I: ${HEADER}: ${TARGET_DIR} has enough available disk space"
  else
       echo "E: ${HEADER}: ${TARGET_DIR} does not have free disk space!!!"
       unset MSG
       unset TITLE
       TITLE="Not Enough Disk Space!"
       MSG+="${TARGET_DIR} has only ${AVAILABLE_SPACE}"
       MSG+=" "
       MSG+="free disk space left!\n"
       MSG+="Prefix backup requires ${TOTAL_SIZE_IN_HF} free disk space."
       # Warn user
       ${ZENITY_CMD} --warning --no-wrap --title "${TITLE}" --text "${MSG}"
       echo "E: Script: ${FUNCNAME[0]}: Not Enough Disk Space, Cancelled!"
       Script_Window
  fi

  # 6. Ask for Compression Levels
  unset LEVEL
  LEVEL=$(${ZENITY_CMD} --scale --value 10 --min-value 1 --max-value 19 \
      --text "${BACKUP_FILE}" --title "Select Compression Level...")
  # Exit if Cancel is clicked
  if [ -z "${LEVEL}" ]; then
       dbug "I: Script: ${FUNCNAME[0]}: Cancel Selected!"
       Script_Window
  fi
  dbug "I: Script ${FUNCNAME[0]}: Compression Level ${LEVEL} Selected!"


  # 7. Create wzt = tar.zst file
  # 7.1 Change directory to PREFIXES_DIR
  cd "${PREFIXES_DIR}"
  dbug "I: Script: ${FUNCNAME[0]}: Processing ${EXE_NAME}"

  # 7.0
  # If gamedir is outside prefixdir, bundle the gamedir
  if [[ "${EXE_FILE}" != *"${PREFIXDIR}"* ]] ; then
       # Do not include GAMEDIR separately when it is already inside prefix
       # tar would need these 2 variables to bundle gamedir into tar archive

      TAR_GAME_DIR_NAME=$(basename "${EXE_PATH}")
      TAR_GAME_DIR_PATH=$(dirname "${EXE_PATH}")
      dbug "I: Script: ${FUNCNAME[0]}: Gamedir at: ${TAR_GAME_DIR_PATH}"
      dbug "I: Script: ${FUNCNAME[0]}: Game Name : ${TAR_GAME_DIR_NAME}"

      # 7.2 Change EXE_FILE and EXE_PATH before bundling
      # Putting Game directory inside drive_c/GAMEDIR
      GAME_BUNDLED_DIR="drive_c/GAMEDIR/${TAR_GAME_DIR_NAME}"
      GAME_EXE_PATH="${PREFIXDIR}/${GAME_BUNDLED_DIR}"
      GAME_EXE_FILE="${GAME_EXE_PATH}/${EXE_NAME}"

      dbug "I: Script: ${FUNCNAME[0]}: Exe Path: ${GAME_EXE_PATH}"
      dbug "I: Script: ${FUNCNAME[0]}: Exe File: ${GAME_EXE_FILE}"

      # 7.3 Change EXE path and name in the script and txt file
      sed "s|${EXE_PATH}|${GAME_EXE_PATH}|g; t;" -i ${PREFIXDIR}/*.txt       \
                                                 -i ${PREFIXDIR}/*.sh        \
                                                 -i ${PREFIXDIR}/*.yml &&    \
      dbug "I: Script: Backup Bundle: Exe Path Changed To: ${GAME_EXE_PATH}"

      sed "s|${EXE_FILE}|${GAME_EXE_FILE}|g; t;" -i ${PREFIXDIR}/*.txt       \
                                                 -i ${PREFIXDIR}/*.sh        \
                                                 -i ${PREFIXDIR}/*.yml &&    \
      dbug "I: Script: ${FUNCNAME[0]}: Exe File Changed To: ${GAME_EXE_FILE}"

     
      # 7.4 Preprocess
      dbug "I: Script: ${FUNCNAME[0]}: Preparing files"
      cd "${PREFIXDIR}"
      
      #remove identifiable values
      SOURCE "winezgui-identity-remove"
      winezgui-identity-remove "${PREFIXDIR}"
      
      # Remove Identities from files
      # Find other files which has username in it.
      SOURCE "winezgui-identity-remove-from-drive-c-files"
      winezgui-identity-remove-from-drive-c-files "${PREFIXDIR}"

      # 7.5 Create wzt-info.yml required for restoring wzt file.
      SOURCE "winezgui-create-wzt-info"
      winezgui-create-wzt-info "${PREFIXDIR}"
      
      # 7.6 Create Bundle
      dbug "I: Script: ${FUNCNAME[0]}: Game Name: ${TAR_GAME_DIR_NAME}"
      dbug "I: Script: ${FUNCNAME[0]}: Creating ${BACKUP_FILE} file..."
       
      dbug "I: Script: ${FUNCNAME[0]}: testing for Runner use by the prefix"


      # Find Local Tar and ZSTD command
      if [ $(which tar) ] && [ $(which zstd) ]; then
           TAR_CMD="$(which tar)"
      fi
      
      # Report user the time it took to bundle
      TIMER_START=${SECONDS}
      # add wzt-info.yml 1st then the prefix directory to the wzt file
      (${TAR_CMD} -I "zstd -${LEVEL} -T0" -cf  "${TARGET_DIR}/${BACKUP_FILE}" \
                  --exclude="registry_backup*.gz"                             \
                  -C "${PREFIXDIR}"  "${WZT_FILE_NAME}"                       \
                  -C "${PREFIXES_DIR}" "${PREFIXNAME}"                        \
                  -C "${TAR_GAME_DIR_PATH}" "./${TAR_GAME_DIR_NAME}"          \
                  --transform                                                 \
        "s|^\./${TAR_GAME_DIR_NAME}|${PREFIXNAME}/${GAME_BUNDLED_DIR}|g" &)|  \
      ${ZENITY_CMD}  --progress --pulsate --auto-close                        \
              --title="Creating ${PREFIXNAME} Game Bundle...!"                \
              --width=500 --text="${BACKUP_FILE}..."
      TOOK=$(( SECONDS - ${TIMER_START} ))
      TIME_TAKEN=$(date -d@${TOOK} -u +%H:%M:%S)
      dbug "I: Script: ${FUNCNAME[0]}: Time Taken: ${TIME_TAKEN}"

  else
       # If gamedir is inside prefixdir, do not bundle gamedir
       # 7.4 Preprocess
       dbug "I: Script: ${FUNCNAME[0]}: Preparing files"
       cd "${PREFIXDIR}"
       SOURCE "winezgui-identity-remove"
       winezgui-identity-remove "${PREFIXDIR}"
       
       # Remove Identities from files
       # Find other files which has username in it.
       SOURCE "winezgui-identity-remove-from-drive-c-files"
       winezgui-identity-remove-from-drive-c-files "${PREFIXDIR}"
       
       # 7.5 Create wzt-info.yml for restore prompt requied for restoring wzt file.
       SOURCE "winezgui-create-wzt-info"
       winezgui-create-wzt-info "${PREFIXDIR}"
       
       # 7.6 Create Bundle
       dbug "I: Script: ${FUNCNAME[0]}: Game Name: ${TAR_GAME_DIR_NAME}"
       dbug "I: Script: ${FUNCNAME[0]}: Creating ${BACKUP_FILE} file..."
 
       # add wzt-info.yml 1st then the prefix directory to the wzt file
       # just like prefix
       # Report user the time it took to bundle
       TIMER_START=${SECONDS}
       (${TAR_CMD} -I "zstd -${LEVEL} -T0" -cf "${TARGET_DIR}/${BACKUP_FILE}" \
                   --exclude="registry_backup*.gz" \
                   -C "${PREFIXDIR}"  "${WZT_FILE_NAME}"      \
                   -C "${PREFIXES_DIR}" "${PREFIXNAME}" &) |    \
             ${ZENITY_CMD}  --progress --pulsate --auto-close \
             --title="Creating ${PREFIXNAME} Game Bundle...!" \
             --width=500 --text="${BACKUP_FILE}..."
       TOOK=$(( SECONDS - ${TIMER_START} ))
       dbug "I: Script: ${FUNCNAME[0]}: Time Taken: $(date -d@${TOOK} -u +%H:%M:%S)"
  fi

  # 7.7 If user clicks cancel, kill tar commands
  # Handle Backup interruption
  # zenity progress won't kill the process, so we do it
  # find the pid of tar which is working with ${BACKUP_FILE}, else it will kill
  # other wzt file operation like extraction and backup
  PID_OF_TAR=$(ps -aux|grep tar|grep -i "${BACKUP_FILE}"           \
              |sed "/.*grep -i.*/d"|sed "s/  */ /g" |cut -f2 -d " ")


  # If there is a PID_OF_TAR then we need to kill the tar command
  if ! [ -z "$PID_OF_TAR" ]; then
        kill -9 ${PID_OF_TAR} \
        && dbug "I: Script: ${FUNCNAME[0]}: Cancelled! Killed tar!" && \
        rm -f "${TARGET_DIR}/${BACKUP_FILE}" && \
        dbug "I: Script: ${FUNCNAME[0]}: Removed partial ${BACKUP_FILE}"

        # Report Size also
        BACKUP_SIZE=$(du -sh "${TARGET_DIR}/${BACKUP_FILE}"|cut -f1)
        # Check du value assignment
        if [ -z "${BACKUP_SIZE}" ]; then
            echo "-------------------- Error --------------------------"
            echo "Error: ${FUNCNAME[0]}: BACKUP_SIZE not assigned"
            echo "-------------------- Error --------------------------"
        fi
        # Backup Sucess Message
        MSG=("${PREFIXNAME} backup cancelled!")

  else
        dbug "I: Script: ${FUNCNAME[0]}: ${BACKUP_FILE} created, success!"
        # Report Size also
        BACKUP_SIZE=$(du -sh "${TARGET_DIR}/${BACKUP_FILE}"|cut -f1)
        # Check du value assignment
        if [ -z "${BACKUP_SIZE}" ]; then
            echo "-------------------- Error --------------------------"
            echo "Error: ${FUNCNAME[0]}: BACKUP_SIZE not assigned"
            echo "-------------------- Error --------------------------"
        fi
        # Backup Sucess Message
        MSG=("<b>Prefix: </b> ${PREFIXNAME}\n")
        MSG+="<b>Filename:</b> ${BACKUP_FILE}"
        MSG+="\n<b>Location:</b> ${TARGET_DIR}\n"
        MSG+="<b>Backup Size:</b> ${BACKUP_SIZE}"

  fi

  # 7.7 Restore changed values after backup
  dbug "I: Script: ${FUNCNAME[0]}: Restoring Registry and Info files"

  SOURCE "winezgui-identity-restore"
  winezgui-identity-restore "${PREFIXDIR}"

  # Find other files which has username in it.
  SOURCE "winezgui-identity-restore-from-drive-c-files"
  winezgui-identity-restore-from-drive-c-files "${PREFIXDIR}"

  # 7.8 Revert Change of EXE_FILE and EXE_PATH by Create Game Bundle Option
  # Change EXE path and name in the script and txt file for the bundle change
  # revert EXE_FILE and EXE_PATH for the gamedir which is outside prefixdir
  if [[ "${EXE_FILE}" != *"${PREFIXDIR}"* ]] ; then
      sed "s|${GAME_EXE_PATH}|${EXE_PATH}|g; t;"  \
            -i ${PREFIXDIR}/*.txt       \
            -i ${PREFIXDIR}/*.sh        \
            -i ${PREFIXDIR}/*.yml && \
          dbug "I: Script: ${FUNCNAME[0]}: Reverted Exe Path to: ${EXE_PATH}"

      sed "s|${GAME_EXE_FILE}|${EXE_FILE}|g; t;"  \
            -i ${PREFIXDIR}/*.txt       \
            -i ${PREFIXDIR}/*.sh        \
            -i ${PREFIXDIR}/*.yml && \
          dbug "I: Script: ${FUNCNAME[0]}: Reverted Exe File to: ${EXE_FILE}"
  fi

  dbug "I: Script: ${FUNCNAME[0]}: Created ${BACKUP_FILE} at ${TARGET_DIR}" \
  && ${ZENITY_CMD} --no-wrap --info --title="Game Bundle!" --text="${MSG[@]}"
  unset TEXT TITLE
}
