#!/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-launch-scripts()
{ 
  dbug "I: $(basename ${0}): Launched ${FUNCNAME[0]}"
    
  SOURCE "script-check-variables-loaded-or-not"
  script-check-variables-loaded-or-not

  if [ -z "${1}" ]; then
       SEARCH_PATH="${PREFIXES_DIR}"
  else
        SEARCH_PATH="${1}"
  fi      
  unset COMMAND
  unset count
  unset SHOW_OPTION
  unset NAMES
  unset FILES
  unset RESULT
  unset 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 ${SEARCH_PATH}/* -maxdepth 1 -iname "*.sh" -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=$(grep "^export PROGNAME=" $i|cut -f2 -d "="|sed 's/ /\ /g')

      # If RealName is empty skip adding to list
      if [ -z "${RealName}" ]; then
           continue
      fi

      # 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} - Launch!\" "
  COMMAND+="--text \"Select Shortcut...\" "
  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 PREFIXES_FOUND SHOW_OPTION 
        unset NAMES FILES COMMAND RESULT count i
        WineZGUI_Window
   fi
   
   if [ -z "${RESULT}" ]; then
        dbug "I: ${HEADER}: Cancelled!"
        unset SEARCH_LIST PREFIXES_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
} 
