#!/usr/bin/env bash
# winezgui-list-available-scripts
# Finds exe installed in drive_c by an installer
# and set the variable SELECTED_SCRIPT
winezgui-list-available-prefix-scripts()
{
  dbug "I: $(basename ${0}): Launched ${FUNCNAME[0]}"
  dbug "I: Script: Launched ${FUNCNAME[0]}"
    
  SOURCE "script-check-variables-loaded-or-not"
  script-check-variables-loaded-or-not

  unset COMMAND count SHOW_OPTION NAMES FILES RESULT search_list
  
  SAVEIFS=${IFS}
  IFS=$(echo -en "\n\b")

  count=0
  NAMES=()
  FILES=()
  # Find all exe files in drive_c Exclude windows directory,
  # and common exe files present in Program Files directory
  # Add those files to FILES array and filenames to NAMES array

  search_list=$(find ${PREFIXES_DIR}/* -maxdepth 1 -iname "*_other.sh" \
               -prune -o -iname "*.sh" -a -type f -print 2>/dev/null| tr '\n' '|')
  exe_found=$(echo ${search_list})

  if [ -z "${exe_found}" ] ; then
       ${ZENITY_CMD} --error --title "${APP_WITH_VER} - Prefix!" \
              --text "Prefixes directory empty.\nUse \"Open exe...\" to install!"
       WineZGUI_Window
  fi
  # if exe_found has value, then only change exe
  for i in $(echo ${search_list}|tr '|' '\n'|sort)
  do
      RealFile=$(realpath -m "${i}")
      RealName=$(basename "${i}")
      # If 1st value is empty, enable it
      if [ -z "${SHOW_OPTION}" ]; then
           SHOW_OPTION+="TRUE \"${RealName}\""
           SHOW_OPTION+=" "
           NAMES+=("${RealName}")
           FILES+=("${RealFile}")
      else
           SHOW_OPTION+="0 \"${RealName}\""
           SHOW_OPTION+=" "
           NAMES+=("${RealName}")
           FILES+=("${RealFile}")
      fi
      count=$(expr ${count} + 1)
  done
  IFS=${SAVEIFS}

 # Generate Height value
 SOURCE "winezgui-get-window-height"
 winezgui-get-window-height ${#FILES[@]}

  # 3. Show the found exe files using zenity
  unset COMMAND
  COMMAND+="${ZENITY_CMD} --title=\"${APP_WITH_VER} - Script!\" "
  COMMAND+="--text \"Select Prefix Script...\" "
  COMMAND+="--radiolist --list --width=460 --height=${HEIGHT} "
  COMMAND+="--hide-header --column \" \" --column \"Action\" ${SHOW_OPTION}"
  RESULT="$(eval ${COMMAND})"

   ANSWER=$?
   
   if [ ${ANSWER} -eq 1 ]; then
        dbug "I: ${HEADER}: Cancelled!"
        unset SEARCH_LIST TEMPLATES_FOUND SHOW_OPTION
        unset NAMES FILES COMMAND RESULT count i
        WineZGUI_Window
   fi
   
   if [ -z "${RESULT}" ]; then
        dbug "I: ${HEADER}: Cancelled!"
        unset SEARCH_LIST TEMPLATES_FOUND SHOW_OPTION
        unset NAMES FILES COMMAND RESULT count i
        WineZGUI_Window
   fi

  count=0

  # 4. Find which file the user selected and set the selected exe into variable
  for i in "${NAMES[@]}"; do
      if [ "$i" = "$RESULT" ]; then
           dbug "I: ${FUNCNAME[0]}: Selected $i"
           dbug "I: ${FUNCNAME[0]}: File is ${FILES[${count}]}"
           export SELECTED_SCRIPT="${FILES[${count}]}"

           break
      fi
      count=$(expr ${count} + 1)
  done
}
