#!/usr/bin/sh

## Internal variables
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.
if systemd-detect-virt --quiet --vm; then
    # https://github.com/swaywm/sway/issues/6581
    export WLR_NO_HARDWARE_CURSORS=1
    # Fallback to the pixman renderer as it has a higher chance to work
    # and will be faster than llvmpipe anyways.
    # See https://gitlab.freedesktop.org/wlroots/wlroots/-/issues/2871
    export WLR_RENDERER=pixman
fi

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

## Load 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

# 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 -- /usr/bin/sway $_SWAY_EXTRA_ARGS "$@"
