#!/usr/bin/env bash
winezgui-template-create-new()
{
  HEADER="$(basename ${0}): ${FUNCNAME[0]}"
  dbug "I: $(basename ${0}): Launched ${FUNCNAME[0]}"
    
  SOURCE "script-check-variables-loaded-or-not"
  script-check-variables-loaded-or-not
  unset NEW_TEMPLATE

  # For Zenity 4
  ZENITY_VERSION=$(${ZENITY_CMD} --version|cut -f1 -d ".")
  if [ "${ZENITY_VERSION}" -eq 4 ]; then
       WINDOW_HEIGHT=340
       WINDOW_WIDTH=500
  else
       WINDOW_HEIGHT=240
       WINDOW_WIDTH=360
  fi
  
  NEW_TEMPLATE=$(${ZENITY_CMD}                                       \
               --title "${APP_WITH_VER} - Create Template"           \
               --list   --hide-header                                \
               --width=${WINDOW_WIDTH}                               \
               --height=${WINDOW_HEIGHT}                             \
               --radiolist --column " "                              \
               --column "Action"                                     \
                   TRUE "Empty win32 template..."                    \
                      0 "Empty win64 template..."                    \
               --text   "Template <b>${TEMPLATE_NAME}</b> in use, Create:" )

  # Exit if Cancel is clicked
  if [ -z "${NEW_TEMPLATE}" ]; then
       dbug "I: Templates: Cancel Selected!"
       return 1
  fi

  # If arch specified as win32 then use it else use win64
  if [ "${NEW_TEMPLATE}" = "Empty win32 template..." ]; then
       SAVED_WINEARCH="${WINEARCH}"
       export WINEARCH=win32 
       export NEW_WINEARCH=${WINEARCH}
  else
       SAVED_WINEARCH="${WINEARCH}"
       export WINEARCH=win64
       export NEW_WINEARCH=${WINEARCH}
  fi

  # Get name for the Prefix Template
  NEW_TEMPLATE=$(${ZENITY_CMD} --title "Create Prefix Template with ${WINEARCH} arch..." \
                             --text  "Current Arch:${SAVED_WINEARCH}\nName new template:" \
                             --width=500 --entry)

  if [ -z "${NEW_TEMPLATE}" ]; then #if no name is given use filename
       #${ZENITY_CMD} --info --title="Shortcut... " --text="no Change"
       # return to Game window
       dbug "I: ${HEADER}: Cancelled"
  fi

  # Check if user entered symbols
  SOURCE "winezgui-check-entry-text"
  winezgui-check-entry-text "${NEW_TEMPLATE}"
  
  ENTRY_CHECK=$?
  if [ ${ENTRY_CHECK} -eq 1 ]; then
       dbug "I: Script: ${FUNCNAME[0]}: Symbols used in filename, cancelling!"
       return 1
  fi
  
  # remove starting spaces and replace space with _
  NEW_TEMPLATE=$(echo "${NEW_TEMPLATE}"|sed "s/^ *//g"|sed "s/ /_/g")

  if [ -d "${TEMPLATES_DIR}/${NEW_TEMPLATE}" ]; then
       ${ZENITY_CMD} --error --title "${APP_WITH_VER}" \
              --text "Template Directory Already Exists!"
  fi

  # Create new Prefix Template
  SOURCE "winezgui-initialize-wineprefix"
  winezgui-initialize-wineprefix "${TEMPLATES_DIR}/${NEW_TEMPLATE}"
  export WINEARCH="$SAVED_WINEARCH"
  
  # Ask whether the user would like to change template
  unset TEXT
  TEXT+="New Template <b>${NEW_TEMPLATE}</b> is created...\n"
  TEXT+="Change Template...?"
  ${ZENITY_CMD} --question --title "${APP_WITH_VER} New Template!" --text "${TEXT}"
  ANSWER=$?
  if [ ${ANSWER} -eq 0 ]; then
       SOURCE "winezgui-template-change"
       winezgui-template-change
  fi
  dbug "I: ${HEADER}: Created Template ${NEW_TEMPLATE} with ${NEW_WINEARCH}"
  unset TEXT
  return 0
}
