#!/usr/bin/sh

unset WAYLAND_DISPLAY
unset DISPLAY

port=0
while [ -e "${XDG_RUNTIME_DIR}/wayland-${port}" ]; do
    let port+=1
done
wayland_display=wayland-${port}

export WAYLAND_DISPLAY=${wayland_display}

if command -v Xwayland > /dev/null
then
  export MIR_SERVER_ENABLE_X11=1
  MIR_SERVER_XWAYLAND_PATH=$(which Xwayland)
  export MIR_SERVER_XWAYLAND_PATH
fi

# If enabling Xwayland, set up tmpfile to identify X11 DISPLAY
if [ $MIR_SERVER_ENABLE_X11 -eq 1 ]; then
    x11_display_file=$(mktemp --tmpdir="${XDG_RUNTIME_DIR}")
fi

export MIRIWAY_CONFIG_DIR="budgie"

# freedesktop specifications mandate that the definition
# of XDG_SESSION_TYPE should be respected
XDG_SESSION_TYPE="wayland"
export XDG_SESSION_TYPE
# Make sure all toolkits use their Wayland backend
#
# For Gtk applications also the x11 backend as a safe fallback
if [ -z "${GDK_BACKEND}" ]; then
    GDK_BACKEND="wayland,x11"
    export GDK_BACKEND
fi
if [ -z "${QT_QPA_PLATFORM}" ]; then
    QT_QPA_PLATFORM="wayland"
    export QT_QPA_PLATFORM
fi

# allow compatible Qt6 apps to theme with gtk3
if [ -z "${QT_QPA_PLATFORMTHEME}" ]; then
    QT_QPA_PLATFORMTHEME="gtk3"
    export QT_QPA_PLATFORMTHEME
fi

# In many OS the wayland backend for clutter does not work, so
# use the safe "gdk" backend instead
if [ -z "${CLUTTER_BACKEND}" ]; then
    CLUTTER_BACKEND="gdk"
    export CLUTTER_BACKEND
fi
if [ -z "${SDL_VIDEODRIVER}" ]; then
    SDL_VIDEODRIVER="wayland"
    export SDL_VIDEODRIVER
fi

# Add a helper for electron based apps to think they are running under GNOME
export GNOME_DESKTOP_SESSION_ID=this-is-deprecated

# This is default for current firefox/thunderbird
MOZ_ENABLE_WAYLAND="1"
export MOZ_ENABLE_WAYLAND

if [ ! -e "${XDG_CONFIG_HOME}/budgie/miriway-shell.config" ]; then
    install -Dpm0644 "/usr/share/budgie-wayland-session/miriway-shell.config"  "${XDG_CONFIG_HOME}/budgie"
fi

miriway-shell --display-config=static="${XDG_CONFIG_HOME}/miriway-shell.display-config" --add-wayland-extensions=all --x11-displayfd 5 5>${x11_display_file} &
miriway_pid=$!

# Wait until the server starts
until [ -O "${XDG_RUNTIME_DIR}/${WAYLAND_DISPLAY}" ]
do
  if ! kill -0 ${miriway_pid} &> /dev/null
  then
    echo "ERROR: miriway-shell [pid=${miriway_pid}] is not running"
    exit 1
  fi
  inotifywait -qq --timeout 5 --event create "$(dirname "${XDG_RUNTIME_DIR}/${WAYLAND_DISPLAY}")"
done

# Grab the X11 DISPLAY and export it if needed
if [ $MIR_SERVER_ENABLE_X11 -eq 1 ]; then
  if inotifywait -qq --timeout 5 --event close_write "${x11_display_file}" && [ -s "${x11_display_file}" ]
  then
    # ${x11_display_file} contains the X11 display
    DISPLAY=:$(cat "${x11_display_file}")
    export DISPLAY
    rm "${x11_display_file}"
  else
    echo "ERROR: Failed to get X11 display from miriway-shell [pid=${miriway-pid}]"
    rm "${x11_display_file}"
    kill ${miriway_pid}
    exit 1
  fi
fi

# Import all variables into the environment
systemctl --user import-environment

# Start the portal service
/usr/libexec/xdg-desktop-portal --replace &
xdp_pid=$!
/usr/libexec/xdg-desktop-portal-wlr &
xdpwlr_pid=$!
/usr/libexec/xdg-desktop-portal-gtk &
xdpgtk_pid=$!

# Launch Budgie

budgie-desktop "$@"
budgie_desktop_exit_code=$?

# Cleanup when the session is ending

kill $xdpgtk_pid
kill $xdpwlr_pid
kill $xdp_pid

kill $miriway_pid

exit $budgie_desktop_exit_code
