#!/usr/bin/env bash
script-winetricks-cli()
{
  dbug "I: Script: Launched ${FUNCNAME[0]}"
  
  SOURCE "script-check-variables-loaded-or-not"
  script-check-variables-loaded-or-not
  
  # Create tmp directory for wget and winetricks
  if [ ! -d "${WINEZGUIDIR}/tmp" ]; then
       mkdir -p "${WINEZGUIDIR}/tmp"
  fi

  # Create Temp dir
  if [ ! -d "${PREFIXDIR}/drive_c/users/${USER}/Temp" ]; then
      mkdir -p "${PREFIXDIR}/drive_c/users/${USER}/Temp"
  fi

  # Ask user if he'd like to download latest winetricks, everytime
  if [ ! -f "${WINEZGUIDIR}/winetricks" ]; then
       SOURCE "winezgui-download-winetricks"
       winezgui-download-winetricks
  fi  
  
  # Show debug output for winetricks command in use
  dbug "I: Script: ${FUNCNAME[0]}: WTRICKS_CMD=$WTRICKS_CMD - CHECK"
  
  # Check if winetricks is available at ${WINEZGUIDIR}
  if [ -f "${WINEZGUIDIR}/winetricks" ]; then
       dbug "I: Script: Winetricks CLI: found winetricks at ${WINEZGUIDIR}!"
       WTRICKS_CMD="${WINEZGUIDIR}/winetricks"
       chmod 755 "${WTRICKS_CMD}"
  fi       

  VERB_LIST="mfc42 xact xact_x64 gmdls \
            vcrun2003 vcrun2005        \
            vcrun2008 vcrun2010        \
            vcrun2019 faudio           \
            quartz avifil32 corefonts"

# Old Games with MIDI support
# d3dx9 xact quartz gmdls avifil32 faudio d3dx9_43 dmusic32 vcrun2008 dirac \
# dmband dmcompos dmime dmloader dmscript dmstyle dmsynth dmusic dsound dswave \
# directmusic directplay amstream

  # Backup registry files before running winetricks
  DATE=$(date +%Y%m%d_%H_%M_%S)
  cd ${PREFIXDIR} && \
  tar -zcvf registry_backup_${DATE}.tar.gz *.reg;

  ${ZENITY_CMD} --info --title="Registry files Backed up!" \
  --text="If winetricks messes up the prefix, restore registry files:\ncd ${PREFIXDIR};\ntar -xvf registry_backup_${DATE}.tar.gz"
  
  MESSAGE="Change & Press Enter to install these using Winetricks:"
  TITLE_TEXT="Winetricks CLI - Paste your Dlls list"
  # Remove multiple spaces from variable
  VERB_LIST=$(echo ${VERB_LIST}|sed "s/  */ /g")
  
  # Looks good with zenity 4
  VERBS=$( ${ZENITY_CMD} --title "${TITLE_TEXT}"     \
                  --width=800 --height=30     \
                  --text  "${MESSAGE}"        \
                  --entry-text "${VERB_LIST}" \
                  --entry)

  if [ -z "${VERBS}" ]; then #if no dlls are given
       #go back to Main function
       dbug "I: Script: Winetricks CLI: no verbs are given!"
       return 1
  fi
  
  # Check if user entered symbols
  SOURCE "winezgui-check-entry-text"
  winezgui-check-entry-text "${VERBS}"
  
  ENTRY_CHECK=$?
  if [ ${ENTRY_CHECK} -eq 1 ]; then
       dbug "I: Script: ${FUNCNAME[0]}: Symbols used in filename, cancelling!"
       return 1
  fi
  
  VERBS=(${VERBS}) ; #convert string to array
  dbug "I: Script: Winetricks CLI: Selected verbs:"
  dbug "I: Script: ${VERBS[*]}"
  
  # Report user the time it took to install winetricks
  TIMER_START=${SECONDS}
  # use winetricks latest if found
  if [ -f "${WINEZGUIDIR}/winetricks" ]; then
       PATH=${PATH}:/app/bin
       export PATH
       WTRICKS_CMD=${WINEZGUIDIR}/winetricks
       export WTRICKS_CMD
  fi

  # Show debug output for winetricks command in use
  dbug "I: Script: ${FUNCNAME[0]}: WTRICKS_CMD=$WTRICKS_CMD - in use!"

  # set runner and winezguidir path for wine and winetricks
  RUNNER_PATH=$(dirname ${RUNNER})
  chmod +rx ${WINEZGUIDIR}/winetricks
  export PATH=${RUNNER_PATH}:${WINEZGUIDIR}:${PATH};
  
  # Start of Show Progress using zenity dialog
  ( for i in ${VERBS[*]}; do
  
  # Show Progress Percentage and Info
  echo "# Installing ${i}..."
  
  # Install winetricks verbs and put output to logs
  ${WTRICKS_CMD} --unattended ${i}
  done
  TOOK=$(( SECONDS - ${TIMER_START} ))
  echo "# Time Taken: ${TOOK} seconds!"
  ) | ${ZENITY_CMD} --width=500 \
            --title="Please Wait... This will take time!" \
            --progress --pulsate --auto-kill
  # Inform about time taken
  
  dbug "I: Script: Winetricks CLI: Exited!"
}
