# units (enabled via systemd --user enable)
# --no-reload: `systemctl enable` forces a full daemon-reload (~370ms) even when
# the symlink already exists. Without it these ~18 calls serialize into a ~6s
# daemon-reload storm at every sway startup. --now still starts each unit.

set $initialize_foot_server '[ -x "$(command -v foot)" ] && (systemctl --user enable --now --no-reload foot-server.socket; systemctl --user enable --now --no-reload foot-server)'
set $initialize_swayr_daemon '[ -x "$(command -v swayrd)" ] && systemctl --user enable --now --no-reload swayrd'
set $initialize_waybar '[ -x "$(command -v waybar)" ] && systemctl --user enable --now --no-reload waybar'
set $initialize_workspace_icons '[ -x "$(command -v sworkstyle)" ] && systemctl --user enable --now --no-reload sworkstyle'
set $initialize_poweralert_daemon '[ -x "$(command -v poweralertd)" ] && systemctl --user enable --now --no-reload poweralertd'
set $initialize_idlehack_daemon '[ -x "$(command -v idlehack)" ] && systemctl --user enable --now --no-reload idlehack'
set $initialize_idle_daemon '[ -x "$(command -v swayidle-conf)" ] && systemctl --user enable --now --no-reload swayidle'
# the sed migrates users whose saved profile still carries pcmanfm-qt's own
# Terminal=xterm default; it runs before the daemon so the stale value isn't
# reloaded and written straight back out on quit. Only that value is rewritten.
set $initialize_mounting_daemon 'sed -i "s|^Terminal=xterm$|Terminal=xdg-terminal-exec|" "$HOME/.config/pcmanfm-qt/default/settings.conf" 2>/dev/null; [ -x "$(command -v pcmanfm-qt)" ] || [ -x "$(command -v pcmanfm)" ] && systemctl --user enable --now --no-reload pcmanfm'
set $initialize_way_displays '[ -x "$(command -v way-displays)" ] && systemctl --user enable --now --no-reload way-displays'
set $initialize_flashfocus '[ -x "$(command -v flashfocus)" ] && systemctl --user enable --now --no-reload flashfocus'
set $initialize_mako '[ -x "$(command -v mako)" ] && systemctl --user set-environment MAKO_FONT="$term-font" MAKO_TEXT_COLOR="$text-color" MAKO_BORDER_COLOR="$accent-color" MAKO_BG_COLOR="$background-color" && systemctl --user reload-or-restart mako'
set $initialize_wlsunset '[ -x "$(command -v wlsunset)" ] && systemctl --user enable --now --no-reload wlsunset'
set $initialize_autotiling '[ -x "$(command -v autotiling)" ] || [ -x "$(command -v autotiling-rs)" ] && systemctl --user enable --now --no-reload autotiling'
set $initialize_cliphist_store '[ -x "$(command -v cliphist)" ] && systemctl --user enable --now --no-reload cliphist-store'
set $initialize_cliphist_watch '[ -x "$(command -v cliphist)" ] && systemctl --user enable --now --no-reload cliphist-watch'
set $initialize_polkit_agent '[ -x /usr/lib/polkit-gnome/polkit-gnome-authentication-agent-1 ] && systemctl --user enable --now --no-reload polkit-gnome'
set $initialize_clip_persist '[ -x "$(command -v wl-clip-persist)" ] && systemctl --user enable --now --no-reload wl-clip-persist'
set $initialize_calendar_daemon '[ -x "$(command -v calcurse)" ] && systemctl --user enable --now --no-reload calcurse-daemon'
set $initialize_watch_playerctl '[ -x "$(command -v playerctl)" ] && systemctl --user enable --now --no-reload watch-playerctl'

# autostarts

set $initialize_dex '[ -x "$(command -v dex)" ] && gdbus wait --session org.kde.StatusNotifierWatcher && dex -a -e SWAY'
set $help_menu '[ -x "$(command -v nwg-wrapper)" ] && [ -f $HOME/.config/nwg-wrapper/help.sh ] && /usr/share/sway/scripts/help.sh'
set $initialize_xdg_dirs '[ -x "$(command -v xdg-user-dirs-update)" ] && exec xdg-user-dirs-update'

## apply the keyboard layout from localectl if no keyboard layout has been set via config.d
set $auto_xdg_keyboard 'grep -q xkb_layout ~/.config/sway/config.d/*.conf || /usr/share/sway/scripts/keyboard.sh'
set $update_rofi_theme 'mkdir -p $HOME/.config/rofi/ && echo $rofi_theme > $HOME/.config/rofi/Manjaro.rasi'
set $create_rofi_config 'mkdir -p $HOME/.config/rofi/ && cp -n /usr/share/sway/templates/rofi/config.rasi $HOME/.config/rofi/config.rasi'
set $enable_noisetorch '[ -x "$(command -v noisetorch)" ] && noisetorch -u && noisetorch -i'
set $disable_nm_applet_autostart '[ -x "$(command -v nm-applet)" ] && [ ! -f $HOME/.config/autostart/nm-applet.desktop ] && cp /etc/xdg/autostart/nm-applet.desktop $HOME/.config/autostart/nm-applet.desktop && echo "Hidden=true" >> $HOME/.config/autostart/nm-applet.desktop'
set $apply_background swaymsg 'output * bg $background fill'

# daemons

set $nm_applet '[ -x "$(command -v nm-applet)" ] && pkill -U $USER -x nm-applet && nm-applet'
