#!/bin/sh

# In case of using seatd (need the user to be in 'seat' group)
# (gnupg and podman hardcode /run/user/$UID for some functions)
# A better solution is using pam_rundir module
if [ -z "${XDG_RUNTIME_DIR}" ]
then
    userid=$(id -u ${USER})
    export XDG_RUNTIME_DIR="$(mktemp -d /tmp/${userid}-runtime-XXXXXX)"
    [ ! -d "${XDG_RUNTIME_DIR}" ] && mkdir -p "${XDG_RUNTIME_DIR}"
    chmod 0700 "${XDG_RUNTIME_DIR}"
fi

# Valid values: seatd, logind
#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
#export DBUS_SESSION_BUS_ADDRESS
#export DBUS_SESSION_BUS_PID

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

# Set keyboard layout
export XKB_DEFAULT_LAYOUT="us,ru"
export XKB_DEFAULT_OPTIONS="grp:rctrl_rshift_toggle"

# catch crashes with core dump
ulimit -c unlimited

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