#!/bin/sh -e

# Force the seat backend (seatd/logind) here if necessary
#export LIBSEAT_BACKEND=logind

# GTK
export GDK_BACKEND=wayland # May cause problems with some xorg applications
export CLUTTER_BACKEND=gdk # Until https://gitlab.gnome.org/GNOME/clutter/-/merge_requests/5 is merged

# QT
export QT_QPA_PLATFORM=wayland-egl
#export QT_WAYLAND_FORCE_DPI=physical
export QT_WAYLAND_DISABLE_WINDOWDECORATION=1

# EFL
export ELM_DISPLAY=wl
export ECORE_EVAS_ENGINE=wayland_egl
export ELM_ENGINE=wayland_egl
export ELM_ACCEL=opengl
#export ELM_SCALE=1

# SDL
export SDL_VIDEODRIVER=wayland # Not working for most games

# Java GUI sucks
export _JAVA_AWT_WM_NONREPARENTING=1

# Firefox, Thunderbird
export MOZ_ENABLE_WAYLAND=1
export MOZ_DBUS_REMOTE=1

# Misfit
export NO_AT_BRIDGE=1 # https://bbs.archlinux.org/viewtopic.php?id=189975
export WINIT_UNIX_BACKEND=wayland

# Compositor
export XDG_SESSION_TYPE=wayland
export XDG_SESSION_DESKTOP="$1"
export XDG_CURRENT_DESKTOP="$1"
export XDG_CURRENT_SESSION="$1"

# Set keyboard layouts.
# NOTE: Let fcitx5 do the thing when available.
if command -v fcitx5 >/dev/null; then
	export XKB_DEFAULT_LAYOUT="us"
	export GTK_IM_MODULE=fcitx
	export QT_IM_MODULE=fcitx
	export XMODIFIERS=@im=fcitx
else
	export XKB_DEFAULT_LAYOUT="us,ru"
	export XKB_DEFAULT_OPTIONS="grp:rctrl_rshift_toggle"
fi

case "$1" in
	# Symlink the correct Waybar config to appropriated location
	sway|river)
		ln -sf ~/.config/services/waybar.service ~/.config/services/enabled/waybar.service
		for item in config style.css; do
			ln -sf "$HOME/.config/waybar/$1/$item" "$HOME/.config/waybar/$item"
		done ;;
	*) rm -f ~/.config/services/enabled/waybar.service ;;
esac

# catch crashes with core dump
ulimit -c unlimited

#$HOME/.config/emacs/bin/doom env
exec /usr/bin/dbus-run-session "$@" > "${XDG_RUNTIME_DIR}/$1-$(date "+%Y-%m-%d").log" 2>&1
