#!/usr/bin/env bash
# SOURCE "winezgui-shortcuts-restore"
# REVIEW
winezgui-shortcuts-restore()
{
  
  SOURCE "script-check-variables-loaded-or-not"
  script-check-variables-loaded-or-not
  unset COMMAND RealName RealFile count TEXT search_list SELECTED_OPTION
  unset SHOW_OPTION NAMES FILES SELECTED_OPTION SELECTED_SHORTCUT
  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 "*.desktop.bak" \
                                       -type f -print| tr '\n' '|')
  exe_found=$(echo ${search_list})

  if [ -z "${exe_found}" ] ; then
       ${ZENITY_CMD} --error --title "${APP_WITH_VER} - Prefix!" \
              --text "No Shortcuts found!"
       WineZGUI_Window
  fi
  # if exe_found has value, then only change exe
  for i in $(echo ${search_list}|tr '|' '\n'|sort)
  do
      RealName=$(basename "$i")
      RealFile=$(realpath -m "$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 count
  COMMAND+="${ZENITY_CMD} --title=\"${APP_WITH_VER}\" --text \"Shortcuts\" "
  COMMAND+="--radiolist --list --width=460 --height=${HEIGHT} "
  COMMAND+="--hide-header --column \" \" --column \"Action\" ${SHOW_OPTION}"
  SELECTED_OPTION=$(eval ${COMMAND})

  unset count
  # Delete
  # 4. Find which file the user selected and set the selected exe into variable
  for NAME in "${NAMES[@]}"; do
      if [ "${NAME}" = "${SELECTED_OPTION}" ]; then
           echo "Selected $NAME"
           echo "File is ${NAME}"
           export SELECTED_SHORTCUT="${FILES[count]}"
           break
      fi
      count=$(expr ${count} + 1)
  done
  # FIXME shortcut
  BASENAME=$(basename "${SELECTED_SHORTCUT}")

  if [ -L "${APPLICATIONSDIR}/${SHORTCUT_PREFIX}_${BASENAME}" ]; then
       unset TEXT
       TEXT+="${BASENAME} desktop file exists, overwrite?"
       TEXT+="\nUse <b>Launch...</b> or <b>Script...</b> later."
       dbug "I: ${HEADER}: ${APPLICATIONSDIR}/${SHORTCUT_PREFIX}_${BASENAME} file Already exists"
       ${ZENITY_CMD} --question --text  "${TEXT}" \
              --title "Desktop file already exists"
       ANSWER=$?
       if [ -z "${ANSWER}" ] || [ "$ANSWER" = "1" ]; then
            echo "Cancelled overwriting ${BASENAME}.desktop"
            return 1
       fi
  fi

  cp "${SELECTED_SHORTCUT}" "${SELECTED_SHORTCUT%.bak}"
  echo "ln -sfv ${SELECTED_SHORTCUT%.bak} ${APPLICATIONSDIR}/${SHORTCUT_PREFIX}_${BASENAME%.bak}"
  # If NO_SHORTCUTS= NOTHING/EMPTY then create shortcuts
  echo "xoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxo"
  if [ -z "${NO_SHORTCUTS}" ]; then
       ln -sfv "${SELECTED_SHORTCUT%.bak}" "${APPLICATIONSDIR}/${SHORTCUT_PREFIX}_${BASENAME%.bak}"
       dbug "I: Script: Shortcut is created at ${APPLICATIONSDIR}/${SHORTCUT_PREFIX}_${BASENAME%.bak}"
  else 
       dbug "I: Script: WARNING: Shortcut NOT CREATED at ${APPLICATIONSDIR}/${SHORTCUT_PREFIX}_${BASENAME%.bak}"
  fi
  unset TEXT
  # Clean up broken shortcuts and update shortcuts menu
  SOURCE "winezgui-remove-broken-links"
  winezgui-remove-broken-links
}
