#!/usr/bin/env bash
#  using flatpak install and script is running from flatpak container
# Check if it is a flatpak based script
# Check if it is running from inside flatpak container
# and gnome terminal is found on host
winezgui-open-shell()
{
  dbug "I: Script: Launched ${FUNCNAME[0]}"
  
  SOURCE "script-check-variables-loaded-or-not"
  script-check-variables-loaded-or-not

  if [ ${INSTALL_TYPE} = "flatpak" ] && [ $(which flatpak-spawn) ]; then
       unset TEXT
       TEXT+="To allow this feature to work.\n"
       TEXT+="<tt><b>flatpak override --user --talk-name=org.freedesktop.Flatpak ${FLATPAK_NAME}</b></tt>\n\n"
       TEXT+="To remove the above permission, run:\n"
       TEXT+="<tt>flatpak override --reset ${FLATPAK_NAME}</tt>"
       flatpak-spawn --host pwd 1>/dev/null 2>/dev/null || \
       (echo "E: $(basename ${0}): needs to access system to run gnome-terminal via flatpak-spawn";\
        ${ZENITY_CMD} --error --title="${APP_WITH_VER}: No System Access!" --text="${TEXT}" )
       unset TEXT

  fi

#
# export WINEPREFIX=${DEFAULT}

# Let user run wine (runner) and latest winetricks
 RUNNER_PATH=$(dirname ${RUNNER})
 chmod +rx ${WINEZGUIDIR}/winetricks

  if   [ ${INSTALL_TYPE} = "flatpak" ] && \
       [ $(flatpak-spawn --host which gnome-terminal 2>/dev/null) ]; then
         dbug "I: Open Terminal: Flatpak script, gnome-terminal running from sandbox"
         echo "1111111111111111111111111111111111111"

         cd "${TEMPLATE}"
         flatpak-spawn --host gnome-terminal --wait -- \
         flatpak --command=bash run "${FLATPAK_NAME}" --norc -c \
        "export PS1='[\u@\h:\w]\$ '; \
         export WINEARCH=${WINEARCH}; \
         export WINEPREFIX=${WINEPREFIX}; \
         export WINEZGUIDIR=${WINEZGUIDIR}; \
         export TEMPLATE=${TEMPLATE};\
         export PATH=${WINEZGUIDIR}:${RUNNER_PATH}:${PATH}; \
         cd \"$1\"; shift; exec bash --norc -i" \
         sh "${TEMPLATE}"

  elif [ ${INSTALL_TYPE} = "flatpak" ] && \
       [ $(flatpak-spawn --host which xfce4-terminal 2>/dev/null) ]; then
         dbug "I: Open Terminal: Flatpak script, xfce4-terminal running from sandbox"
         echo "2222222222222222222222222222222222"

         cd "${TEMPLATE}"
         flatpak-spawn --host xfce4-terminal --disable-server -e \
         "flatpak run --command=bash "${FLATPAK_NAME}" --norc \
         -c \"export PS1='[\u@\h:\w]\$ '; \
         export WINEPREFIX=${WINEZGUIDIR}; \
         export WINEARCH=${WINEARCH}; \
         export WINEZGUIDIR=${WINEZGUIDIR}; \
         export TEMPLATE=${TEMPLATE};\
         export PATH=${WINEZGUIDIR}:${RUNNER_PATH}:${PATH}; \
         cd ${TEMPLATE}; \
         exec bash --norc\""


        #  flatpak-spawn --host xfce4-terminal --disable-server -e \
        #  "flatpak run --command=bash "${FLATPAK_NAME}" --norc \
        #  -c \"export PS1='[\u@\h:\w]\$ '; cd ${WINEZGUIDIR}; exec bash --norc\""

  elif   [ ${INSTALL_TYPE} = "flatpak" ] && \
       [ $(flatpak-spawn --host which konsole 2>/dev/null) ]; then
         dbug "I: Open Terminal: Flatpak script, konsole running from sandbox"
         echo "33333333333333333333333333333333333"

         cd "${TEMPLATE}"
         flatpak-spawn --host konsole  -e \
         flatpak --command=bash run "${FLATPAK_NAME}" --norc -c \
        "export PS1='[\u@\h:\w]\$ '; \
         export WINEARCH=${WINEARCH}; \
         export WINEPREFIX=${WINEPREFIX}; \
         export WINEZGUIDIR=${WINEZGUIDIR}; \
         export TEMPLATE=${TEMPLATE};\
         export PATH=${RUNNER_PATH}:${WINEZGUIDIR}:${PATH}; \
         cd \"${TEMPLATE}\"; exec bash --norc"

      #    flatpak --command=bash run "${FLATPAK_NAME}" --norc -c \
      #    "export PS1='[\u@\h:\w]\$ ';  cd \"$1\"; shift; exec bash --norc -i" \
      #    sh "${WINEZGUIDIR}"
      #  # If flatpak install and script running from host
       
       # and gnome terminal is found
  elif [ ${INSTALL_TYPE} = "flatpak" ] && \
       [ $(which gnome-terminal) ]; then
         dbug "I: Open Terminal: Flatpak script, gnome-terminal running from host"
         echo "44444444444444444444444444444444444"
         
         cd "${TEMPLATE}"
         gnome-terminal --wait -- \
         flatpak --command=bash run "${FLATPAK_NAME}" --norc -c \
        "export PS1='[\u@\h:\w]\$ '; \
         export WINEARCH=${WINEARCH}; \
         export WINEPREFIX=${WINEPREFIX}; \
         export WINEZGUIDIR=${WINEZGUIDIR}; \
         export TEMPLATE=${TEMPLATE};\
         export PATH=${RUNNER_PATH}:${WINEZGUIDIR}:${PATH}; \
         cd \"$1\"; shift; exec bash --norc -i" \
         sh "${WINEZGUIDIR}"
  
  elif [ ${INSTALL_TYPE} = "flatpak" ] && \
       [ $(which xfce4-terminal) ]; then
         dbug "I: Open Terminal: Flatpak script, xfce4-terminal running from host"
         echo "55555555555555555555555555555555555"

          cd "${TEMPLATE}"
         xfce4-terminal --disable-server -e "flatpak run --command=bash "${FLATPAK_NAME}" --norc \
         -c \"export PS1='[\u@\h:\w]\$ '; \
         export WINEPREFIX=${WINEPREFIX}; \
         export WINEARCH=${WINEARCH}; \
         export WINEZGUIDIR=${WINEZGUIDIR}; \
         export TEMPLATE=${TEMPLATE};\
         export PATH=${WINEZGUIDIR}:${RUNNER_PATH}:${PATH}; \
         cd ${TEMPLATE}; \
         exec bash --norc\""

  elif [ ${INSTALL_TYPE} = "flatpak" ] && \
       [ $(which konsole) ]; then
         dbug "I: Open Terminal: Flatpak script, konsole running from host"
         echo "66666666666666666666666666666666666"

         cd "${TEMPLATE}"
         konsole -e \
         flatpak --command=bash run "${FLATPAK_NAME}" --norc -c \
        "export PS1='[\u@\h:\w]\$ '; \
         export WINEARCH=${WINEARCH}; \
         export WINEPREFIX=${WINEPREFIX}; \
         export WINEZGUIDIR=${WINEZGUIDIR}; \
         export TEMPLATE=${TEMPLATE};\
         export PATH=${RUNNER_PATH}:${WINEZGUIDIR}:${PATH}; \
         cd \"${TEMPLATE}\"; exec bash --norc" 

        #  "flatpak run --command=bash "${FLATPAK_NAME}" --norc \
        #  -c \"export PS1='[\u@\h:\w]\$ '; cd ${WINEZGUIDIR}; exec bash --norc\""

       # Else it is a host install and gnome-terminal is found
  elif [ ${INSTALL_TYPE} = "system" ] && \
       [ $(which gnome-terminal) ]; then
         dbug "I: Open Terminal: System script, gnome-terminal running from host"
         echo "7777777777777777777777777777777777"

         cd "${TEMPLATE}"
         gnome-terminal --wait -- bash -c \
         "export PS1='[\u@\h:\w]\$ '; \
         export WINEARCH=${WINEARCH}; \
         export WINEPREFIX=${WINEPREFIX}; \
         export WINEZGUIDIR=${WINEZGUIDIR}; \
         export TEMPLATE=${TEMPLATE};\
         export PATH=${RUNNER_PATH}:${WINEZGUIDIR}:${PATH}; \
         cd \"$1\"; shift; exec bash --norc -i" \
         sh "${WINEZGUIDIR}"
         # gnome terminal is not found

  elif [ ${INSTALL_TYPE} = "system" ] && \
       [ $(which xfce4-terminal) ]; then
         dbug "I: Open Terminal: System script, xfce4-terminal running from host"
         echo "88888888888888888888888888888888888"

         cd "${TEMPLATE}"
         xfce4-terminal --disable-server -e \
         "bash -c \
         \"export PS1='[\u@\h:\w]\$ '; \
         export WINEARCH=${WINEARCH}; \
         export WINEPREFIX=${WINEPREFIX}; \
         export WINEZGUIDIR=${WINEZGUIDIR}; \
         export TEMPLATE=${TEMPLATE};\
         export PATH=${RUNNER_PATH}:${WINEZGUIDIR}:${PATH}; \
         cd \"${TEMPLATE}\"; exec bash --norc\""

        #  "bash -c \"export PS1='[\u@\h:\w]\$ '; cd ${WINEZGUIDIR}; exec bash --norc\""

  elif [ ${INSTALL_TYPE} = "system" ] && \
       [ $(which konsole) ]; then
         dbug "I: Open Terminal: System script, konsole running from host"
         echo "99999999999999999999999999999999999"

         cd "${TEMPLATE}"
         konsole  -e \
         bash -c \
         "export PS1='[\u@\h:\w]\$ '; \
         export WINEARCH=${WINEARCH}; \
         export WINEPREFIX=${WINEPREFIX}; \
         export WINEZGUIDIR=${WINEZGUIDIR}; \
         export TEMPLATE=${TEMPLATE};\
         export PATH=${RUNNER_PATH}:${WINEZGUIDIR}:${PATH}; \
         cd \"$1\"; shift; exec bash --norc -i" \
         sh "${WINEZGUIDIR}"
        #  "bash -c \"export PS1='[\u@\h:\w]\$ '; cd ${WINEZGUIDIR}; exec bash --norc\""
  else
         dbug "I: Open Terminal: Gnome-terminal not found"
         # Looks good with zenity 4
         ${ZENITY_CMD} --title "${SELECTION}" --width=500 --height=30 --info  \
                --text "This feature requires \"gnome-terminal\" or \"xfce4-terminal\" or \"konsole\""
  fi
  unset TEXT

}
