#!/bin/bash

# Mostly untouched from https://github.com/xdbob/sway-services

# From: https://people.debian.org/~mpitt/systemd.conf-2016-graphical-session.pdf

# robustness: if the previous graphical session left some failed units,
# reset them so that they don't break this startup
for unit in $(systemctl --user --no-legend --state=failed list-units | cut -f1 -d' '); do
	partof="$(systemctl --user show -p PartOf --value "$unit")"
	for target in sway-session.target wayland-session.target graphical-session.target; do
		if [ "$partof" = "$target" ]; then
			systemctl --user reset-failed "$unit"
			break
		fi
	done
done

# Source /etc/profile (needed, contains important environment variables)
source /etc/profile

# Save environment before adding to systemd (to be removed after sway stops)
new_envs=$(systemctl --user show-environment | cut -d'=' -f 1 | sort | comm -13 - <(env | cut -d'=' -f 1 | sort))

# first import environment variables from the login manager
systemctl --user import-environment

# then start the service
systemctl --wait --user start sway.service

# Cleanup environment (see above)
systemctl --user unset-environment $new_envs
