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

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

  # Check if --talk-name=org.freedesktop.Flatpak is allowed
  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

         
  if   [ ${INSTALL_TYPE} = "flatpak" ] && \
       [ $(flatpak-spawn --host which gnome-terminal 2>/dev/null) ]; then
         dbug "I: Script: Open Shell: Flatpak script, gnome-terminal running from sandbox"
         cd "${PREFIXDIR}"
         flatpak-spawn --host gnome-terminal --wait -- \
         flatpak --command=bash run "${FLATPAK_NAME}" --norc -c \
         "export PS1='[\u@\h:\w]\$ '; \
         export WINEPREFIX=\"${PREFIXDIR}\"; \
         export WINEARCH=\"${WINEARCH}\"; \
         export WINEZGUIDIR=\"${WINEZGUIDIR}\"; \
         export TEMPLATE=\"${TEMPLATE}\";\
         export PATH=\"${WINEZGUIDIR}:${RUNNER_PATH}:${PATH}\"; \
         cd \"$1\"; \
         shift; \
         exec bash --norc -i" \
         sh "${PREFIXDIR}"         
        
  elif [ ${INSTALL_TYPE} = "flatpak" ] && \
       [ $(flatpak-spawn --host which xfce4-terminal 2>/dev/null) ]; then
         dbug "I: Script: Open Shell: Flatpak script, xfce4-terminal running from sandbox"
         cd "${PREFIXDIR}"
         flatpak-spawn --host xfce4-terminal --disable-server -e \
         "flatpak run --command=bash "${FLATPAK_NAME}" --norc \
         -c \"export PS1='[\u@\h:\w]\$ '; \
         export WINEPREFIX=${PREFIXDIR}; \
         export WINEARCH=${WINEARCH}; \
         export WINEZGUIDIR=${WINEZGUIDIR}; \
         export TEMPLATE=${TEMPLATE};\
         export PATH=${WINEZGUIDIR}:${RUNNER_PATH}:${PATH}; \
         cd ${PREFIXDIR}; \
         exec bash --norc\""

  elif [ ${INSTALL_TYPE} = "flatpak" ] && \
       [ $(flatpak-spawn --host which konsole 2>/dev/null) ]; then
         dbug "I: Script: Open Shell: Flatpak script, konsole running from sandbox"
         cd "${PREFIXDIR}"
         flatpak-spawn --host konsole -e \
         "flatpak run --command=bash "${FLATPAK_NAME}" --norc \
         -c \"export PS1='[\u@\h:\w]\$ '; \
         export WINEPREFIX=${PREFIXDIR}; \
         export WINEARCH=${WINEARCH}; \
         export WINEZGUIDIR=${WINEZGUIDIR}; \
         export TEMPLATE=${TEMPLATE};\
         export PATH=${WINEZGUIDIR}:${RUNNER_PATH}:${PATH}; \
         cd ${PREFIXDIR}; \
         exec bash --norc\""
       # If flatpak install and script running from host
       # and gnome terminal is found

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

  elif [ ${INSTALL_TYPE} = "flatpak" ] && \
       [ $(which xfce4-terminal) ]; then
         export WINEPREFIX="${PREFIXDIR}";
         dbug "I: Script: Open Shell: Flatpak script, xfce4-terminal running from host"
         cd "${PREFIXDIR}"
         xfce4-terminal --disable-server -e "flatpak run --command=bash "${FLATPAK_NAME}" --norc \
         -c \"export PS1='[\u@\h:\w]\$ '; \
         export WINEPREFIX=${PREFIXDIR}; \
         export WINEARCH=${WINEARCH}; \
         export WINEZGUIDIR=${WINEZGUIDIR}; \
         export TEMPLATE=${TEMPLATE};\
         export PATH=${WINEZGUIDIR}:${RUNNER_PATH}:${PATH}; \
         cd ${PREFIXDIR}; \
         exec bash --norc\""

  elif [ ${INSTALL_TYPE} = "flatpak" ] && \
       [ $(which konsole) ]; then
         export WINEPREFIX="${PREFIXDIR}";
         dbug "I: Script: Open Shell: Flatpak script, konsole running from host"
         cd "${PREFIXDIR}"
         konsole -e "flatpak run --command=bash "${FLATPAK_NAME}" --norc \
         -c \"export PS1='[\u@\h:\w]\$ '; \
         export WINEPREFIX=${PREFIXDIR}; \
         export WINEARCH=${WINEARCH}; \
         export WINEZGUIDIR=${WINEZGUIDIR}; \
         export TEMPLATE=${TEMPLATE};\
         export PATH=${WINEZGUIDIR}:${RUNNER_PATH}:${PATH}; \
         cd ${PREFIXDIR}; \
         exec bash --norc\""
  
       # Else it is a host install and gnome-terminal is found
  
  elif [ ${INSTALL_TYPE} = "system" ] && \
       [ $(which gnome-terminal) ]; then
         dbug "I: Script: Open Shell: System script, gnome-terminal running from host"
         export WINEPREFIX="${PREFIXDIR}";
         cd "${PREFIXDIR}"
         gnome-terminal --wait -- bash -c \
         "export PS1='[\u@\h:\w]\$ '; \
         export WINEPREFIX=\"${PREFIXDIR}\"; \
         export WINEARCH=\"${WINEARCH}\"; \
         export WINEZGUIDIR=\"${WINEZGUIDIR}\"; \
         export TEMPLATE=\"${TEMPLATE}\";\
         export PATH=\"${WINEZGUIDIR}:${RUNNER_PATH}:${PATH}\"; \
         cd \"$1\"; \
         shift; \
         exec bash --norc -i" \
         sh "${PREFIXDIR}"      

         #"export PS1='[\u@\h:\w]\$ '; cd \"${WINEPREFIX}\"; exec bash -i"
         # gnome terminal is not found

  elif [ ${INSTALL_TYPE} = "system" ] && \
       [ $(which xfce4-terminal) ]; then
         dbug "I: Script: Open Shell: System script, xfce4-terminal running from host"
         cd "${PREFIXDIR}"
         export WINEPREFIX="${PREFIXDIR}";
         xfce4-terminal --disable-server -e \
        "bash -c \
       \"export PS1='[\u@\h:\w]\$ '; \
         export WINEPREFIX=${PREFIXDIR}; \
         export WINEARCH=${WINEARCH}; \
         export WINEZGUIDIR=${WINEZGUIDIR}; \
         export TEMPLATE=${TEMPLATE};\
         export PATH=${WINEZGUIDIR}:${RUNNER_PATH}:${PATH}; \
         cd ${PREFIXDIR}; exec bash --norc\""
  
  elif [ ${INSTALL_TYPE} = "system" ] && \
       [ $(which konsole) ]; then
         dbug "I: Script: Open Shell: System script, konsole running from host"
         cd "${PREFIXDIR}"
         export WINEPREFIX="${PREFIXDIR}";
         konsole -e \
        "bash -c \
       \"export PS1='[\u@\h:\w]\$ '; \
         export WINEPREFIX=${PREFIXDIR}; \
         export WINEARCH=${WINEARCH}; \
         export WINEZGUIDIR=${WINEZGUIDIR}; \
         export TEMPLATE=${TEMPLATE};\
         export PATH=${WINEZGUIDIR}:${RUNNER_PATH}:${PATH}; \
         cd ${PREFIXDIR}; exec bash --norc\""
  else
         dbug "I: Script: Open Shell: Gnome-terminal not found"
         # Looks good for 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
}
