#!/usr/bin/env sh

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

# Check if system is running in virtual machine
case "$(systemd-detect-virt)" in
qemu)
  export WLR_RENDERER=pixman
  export WLR_NO_HARDWARE_CURSORS=1
  ;;
kvm)
  export WLR_NO_HARDWARE_CURSORS=1
  ;;
oracle)
  export WLR_NO_HARDWARE_CURSORS=1
  ;;
esac

# Apply Nvidia-specific variables
if [ -d /sys/module/nvidia ]; then
    export WLR_NO_HARDWARE_CURSORS=1
    export GBM_BACKEND=nvidia-drm
    export __GLX_VENDOR_LIBRARY_NAME=nvidia
    export LIBVA_DRIVER_NAME=nvidia
fi

## Load system environment customizations
if [ -d /etc/environment.d ]; then
    set -o allexport
    for f in /etc/environment.d/*.conf; do
        # shellcheck source=/dev/null
        . "$f"
    done
    set +o allexport
fi

## Load user environment customizations
if [ -d "${XDG_CONFIG_HOME:-$HOME/.config}/environment.d" ]; then
    set -o allexport
    for i in "${XDG_CONFIG_HOME:-$HOME/.config}/environment.d"/*.conf; do
        # shellcheck source=/dev/null
        . "$i"
    done
    set +o allexport
fi

# Run Sway under ssh-agent
run_sway() {
    exec systemd-cat -- /usr/bin/ssh-agent /usr/bin/sway $@
}

# Check if Nvidia driver installed, start Sway and send output to the journal
if [ -d /sys/module/nvidia ] && [ ! -d /sys/module/amdgpu ] && [ ! -d /sys/module/i915 ]; then
    run_sway --unsupported-gpu $@
else
    run_sway
fi
