#!/usr/bin/env bash
################################################################################
# Create wzt-info.yml yml file for WineZGUI Backup file, which will be used by
# winezgui-restore-wzt file, to get "Size" and "Game name" from this file
# Steps:
# 1. Set Debug Prompt, If prefix is default it is "I: Backup: ..."
# 2. Check if Prefix Directory is writable or not
# 3. Insert Data into wzt-info.yml
# 4. Check if the file exists after it is created
################################################################################
winezgui-create-wzt-info()
{
  dbug "I: Script: Launched ${FUNCNAME[0]}"
  HEADER="$(basename ${0}): ${FUNCNAME[0]}"
    
  SOURCE "script-check-variables-loaded-or-not"
  script-check-variables-loaded-or-not

  # 1. Need full path to directory to create file in it
  if [ -z "${1}" ]; then
       echo "Must provide Prefix or Template Directory to process"
       return 1
  else
       DIRECTORY="${1}"
       DIRNAME=$(basename "${1}")
  fi
  
  WZT_FILE_NAME="wzt-info.yml"
  WZT_FILE="${DIRECTORY}/${WZT_FILE_NAME}"

  # 2. Check if Prefix Directory is writable or not
  dbug "I: ${HEADER}: Creating ${WZT_FILE_NAME} at ${DIRNAME}"
  dbug "I: ${HEADER}: is ${DIRNAME} writable?"
  if [ -w "${DIRECTORY}" ]; then
       dbug "I: ${HEADER}: Yes!"
  else
       dbug "I: ${HEADER}: No!"
       dbug "I: ${HEADER}: Aborting!"
       return 1
  fi

  # 3. Insert Data into wzt-info.yml
  DATE=$(date +%F_%H%M)
  #CREATED_WINE_VERSION=$(grep -i "Wine:" ${INFOFILE}|cut -f2 -d ":")

  echo "wzt-info.yml:"
  echo "- - - - - - - - - - - - - - - - - - - - - - - - - - -"
  echo "Name:${PROGNAME}"                |tee    "${WZT_FILE}"
  echo "Exe:${EXE_NAME}"                 |tee -a "${WZT_FILE}"
  echo "Prefix:${DIRNAME}"               |tee -a "${WZT_FILE}"
  echo "Created:${DATE}"                 |tee -a "${WZT_FILE}"
  echo "Install:${INSTALL_TYPE}"         |tee -a "${WZT_FILE}"
  echo "Wine:${WINEVER}"                 |tee -a "${WZT_FILE}"
  echo "Runner:${RUNNER_NAME}"           |tee -a "${WZT_FILE}"
  echo "Arch:${WINEARCH}"                |tee -a "${WZT_FILE}"
  echo "Size:${TOTAL_SIZE_IN_HF}"        |tee -a "${WZT_FILE}"
  echo "WineZGUI:${APPVERSION}"          |tee -a "${WZT_FILE}"
  echo " "
  
  # 3.1 Include flatpak id in wzt-info.yml
  if [ "${INSTALL_TYPE}" = "flatpak" ]; then
       echo "Flatpak:${FLATPAK_NAME}"    |tee -a "${WZT_FILE}"
  fi

  # 4. Check if the file exists after it is created
  if [ -f "${WZT_FILE}" ]; then
       dbug "I: ${HEADER}: Created ${WZT_FILE}"
  fi
  
  EDIT_WZT=$(grep "edit_wzt:" "${SETTINGS_FILE}")
  # 7.5.5 Ask whether to edit wzt-info
  #${ZENITY_CMD} --question --no-wrap --title="Edit wzt-info.yml?" \
  #        --text "${WZT_FILE} is created, would you like edit and add other info into it?"
  # ANSWER=$?
   # 1.1 If user selects no or cancels to locate exe file
   #if [ ${ANSWER} -eq 1 ]; then
   # if any value is given for edit wzt then run
   if [ ! -z "${EDIT_WZT}" ]; then
        xdg-open "${WZT_FILE}"
        ${ZENITY_CMD} --info --title="Editing WZT-Info.yml" --text="Click ok after editing the wzt-file, to continue creating bundle!"
   fi
}
