#!/usr/bin/sh

## Internal variables
readonly _SWAY_COMMAND="/usr/bin/sway"
SWAY_EXTRA_ARGS=""

## General exports
export XDG_CURRENT_DESKTOP=sway
export XDG_SESSION_DESKTOP=sway
export XDG_SESSION_TYPE=wayland

## Hardware compatibility
# We can't be sure that the virtual GPU is compatible with Sway.
# We should be attempting to detect an EGL driver instead, but that appears
# to be a bit more complicated.
case $(systemd-detect-virt --vm) in
    "none"|"")
        ;;
    "kvm")
        # There's two drivers we can get here, depending on the 3D acceleration
        # flag state: either virtio_gpu/virgl or kms_swrast/llvmpipe.
        #
        # The former one causes graphical glitches in OpenGL apps when using
        # 'pixman' renderer. The latter will crash 'gles2' renderer outright.
        # Neither of those support 'vulkan'.
        #
        # The choice is obvious, at least until we learn to detect the driver
        # instead of abusing the virtualization technology identifier.
        #
        # See also: https://gitlab.freedesktop.org/wlroots/wlroots/-/issues/2871
        export WLR_RENDERER=pixman
        # 'pixman' on virtio_gpu with recent kernels is glitchy. Appears that
        # it only affects atomic KMS, and legacy interface works.
        export WLR_DRM_NO_ATOMIC=1
        # WLR_NO_HARDWARE_CURSORS=1 is not needed with legacy DRM interface
        ;;
    *)
        # https://github.com/swaywm/sway/issues/6581
        export WLR_NO_HARDWARE_CURSORS=1
        ;;
esac

## Apply `environment.d(5)` customizations
# This can be used to share the custom environment configs with systemd --user.
# Importing `systemd --user show-environment` here may have unexpected
# consequences, such as getting a leftover `WAYLAND_DISPLAY` or `DISPLAY`
# and breaking Sway startup. Thus, the direct call to a systemd generator.
set -o allexport
eval "$(/usr/lib/systemd/user-environment-generators/30-systemd-environment-d-generator)"
set +o allexport

## Load Sway-specific system environment customizations
if [ -f /etc/sway/environment ]; then
    set -o allexport
    # shellcheck source=/dev/null
    . /etc/sway/environment
    set +o allexport
fi

## Load Sway-specific user environment customizations
if [ -f "${XDG_CONFIG_HOME:-$HOME/.config}/sway/environment" ]; then
    set -o allexport
    # shellcheck source=/dev/null
    . "${XDG_CONFIG_HOME:-$HOME/.config}/sway/environment"
    set +o allexport
fi

## Unexport internal variables
# export -n is not POSIX :(
_SWAY_EXTRA_ARGS="$SWAY_EXTRA_ARGS"
unset SWAY_EXTRA_ARGS

## Log all exported WLR_ variables
if _WLR_VARS=$(env | grep '^WLR_'); then
    printf 'environment variables for wlroots: %s' "$_WLR_VARS" |
        tr '\n' ' ' |
        systemd-cat -p notice -t "${_SWAY_COMMAND##*/}"
fi

# Start sway with extra arguments and send output to the journal
# shellcheck disable=SC2086 # quoted expansion of EXTRA_ARGS can produce empty field
exec systemd-cat -- $_SWAY_COMMAND $_SWAY_EXTRA_ARGS "$@"
