#!/usr/bin/env zsh

local targets=(river-session.target graphical-session.target)
for unit in $(systemctl --user --no-legend --state=failed --plain list-units | cut -f1 -d' '); do
    local partof="$(systemctl --user show -p PartOf --value "$unit")"
    if (( ${targets[(Ie)${partof}]} )); then
        systemctl --user reset-failed "$unit"
        break
    fi
done

source "${HOME}/.config/river/river-env.zsh"

local current_systemd_environ=(${${(f)"$(systemctl --user show-environment)"}/=*})
local current_shell_environ=(${${(f)"$(export)"}/=*})
local new_env=(${current_shell_environ:|current_systemd_environ})

systemctl --user import-environment ${current_shell_environ}
dbus-update-activation-environment ${current_shell_environ}

systemctl --wait --user start river.service

systemctl --user unset-environment ${new_env}
