#!/usr/bin/env bash
# winezgui-template-restore
winezgui-template-restore()
{
  
  SOURCE "script-check-variables-loaded-or-not"
  script-check-variables-loaded-or-not
  HEADER="$(basename ${0}): ${FUNCNAME[0]}"
  dbug "I: $(basename ${0}): Launched ${FUNCNAME[0]}"
 
  # Ask user to select the .wzt backup file to restore
  TAR_FILE="$(${ZENITY_CMD} --file-filter="*.zst *.gz *.bz2"      \
                     --file-selection                      \
                     --title="Select Prefix Template Backup File" )"
  TAR_FILE_NAME=$(basename "${TAR_FILE}")
  # If no Backup file is selected, or cancel is clicked
  if [ -z "${TAR_FILE}" ]; then
       dbug "I: Restore: Cancelled!"
       unset TAR_FILE  ; # Empty variable
       return 1
  fi # -z "${TAR_FILE}"
  
  cd "${TEMPLATES_DIR}"

  # 0. Check whether WINEZGUIDIR is writable before restoring
  if [ ! -w "${TEMPLATES_DIR}" ]; then
       dbug "I: ${HEADER}: ${TEMPLATES_DIR} is not writable. Aborting!"
       ${ZENITY_CMD} --error --title "${APP_WITH_VER}" \
              --text "${TEMPLATES_DIR} is not writable"
       return 1
  fi
  TAR_PREFIX=$(${TAR_CMD} -tf "${TAR_FILE}"|head -n2|grep "/"|cut -f1 -d "/")
  dbug "I: ${HEADER}: Prefix from ${TAR_FILE} is ${TAR_PREFIX}"

  (${TAR_CMD} -xvf "${TAR_FILE}" -C "${TEMPLATES_DIR}" --transform \
          "s|XOUSERXO|${USER}|g" &)|                        \
  (${ZENITY_CMD} --progress --width=500                            \
         --title="Restoring ${TAR_FILE_NAME}!"              \
         --text="Extracting ${TAR_FILE}..."                 \
         --pulsate --auto-close)

  ZENITY_STATUS=$?

  # 4.2.1 If user cancels extraction,  zenity will return 1
  if [ "${ZENITY_STATUS}" = "1" ]; then
       # If the user cancels extraction of wzt game bundle during extraction
       # kill the running tar command which has wzt in it
       unset PID_OF_TAR
       PID_OF_TAR=$(ps -aux|grep -i tar|sed "/.*grep -i.*/d" \
                           |sed "s/  */ /g" |cut -f2 -d " "  )

       if ! [ -z "$PID_OF_TAR" ]; then
            kill -9 ${PID_OF_TAR} \
            && dbug "I: ${HEADER}: Cancelled! Stopped extraction!"
            # Ask the user whether to delete partially extracted or let it be
            ${ZENITY_CMD} --question --title "Extraction Cancelled!" \
                   --text "Delete ${TAR_PREFIX} prefix?"
            ANSWER=$?
            if [ "${ANSWER}" = "0" ]; then
                 # User selected Delete
                 rm -rf "${TEMPLATES_DIR}/${TAR_PREFIX}" && \
                 dbug "I: ${HEADER}: Removed ${TEMPLATES_DIR}/${TAR_PREFIX}"
                 # Cleanup broken links in winezgui shortcuts directory
                 find ${APPLICATIONSDIR} -maxdepth 1 -iname "${SHORTCUT_PREFIX}*" -xtype l -delete 2>/dev/null && \
                 dbug "I: ${HEADER}: Removed broken WineZGUI .desktop links"
            else
                 # user wants to keep prefix
                 dbug "I: ${HEADER}: Keeping extracted ${WINEZGUIDIR}/${TAR_PREFIX}"
                 # Restore Sucess Message
            fi
            MSG=("${TAR_PREFIX} Restore Cancelled!")
            WineZGUI_Window
       fi # if ! [ -z "$PID_OF_TAR" ]; then
  else
       # PROCESS XO--XO variable inside prefixes and templates directory
       SOURCE "winezgui-identity-restore-all"
       winezgui-identity-restore-all

       # Inform on successful extraction
       dbug "I: ${HEADER}: Extracted ${TAR_FILE} Successfully!"
       ${ZENITY_CMD} --info --title="${APP_WITH_VER} - Restore Template!" \
       --text="Extracted ${TAR_FILE} Successfully!" 
  
  fi # [ "${ZENITY_STATUS}" = "1" ]; then
}

