#! /bin/bash

# Qt apps follow qt6ct, which reads ~/.config/qt6ct/colors/dc.conf
export QT_QPA_PLATFORMTHEME=qt6ct

# GTK4 ignores gtk-application-prefer-dark-theme and did not pick up the
# portal's prefer-dark either, which left labels dark-on-dark. Forcing the
# dark variant here covers GTK3 and GTK4 both; ~/.config/gtk-{3,4}.0/gtk.css
# recolours it on top.
export GTK_THEME=Adwaita:dark

# Start sxhkd if it's not already running
pgrep -x sxhkd >/dev/null || sxhkd -c ~/.config/bspwm/sxhkdrc &

# Start picom
picom --config $HOME/.config/bspwm/picom.conf &

# Set window manager name to LG3D to fix some Java applications display issues
wmname LG3D

# Vars
export XDG_CURRENT_DESKTOP="bspwm"
[ -f "$HOME/.env" ] && . "$HOME/.env"

# Workspaces Configuration
# Whatever the primary output is called on this machine.
MONITOR="$(xrandr --query | awk '/ connected primary/{print $1; exit}')"
[ -z "$MONITOR" ] && MONITOR="$(bspc query -M --names | head -n1)"
bspc monitor "$MONITOR" -d 1 2 3 4 5 6 7 8

# Enable window focus follow mouse pointer
bspc config focus_follows_pointer true

# Auto set monitor
monitor=$(xrandr --query | grep " connected" | awk '{ print $1 }' | head -n 1)
resolution=$(xrandr --query | grep -A1 "^$monitor" | grep -Eo '[0-9]+x[0-9]+' | sort -V | tail -n 1)
refresh_rate=$(xrandr --query | grep -A1 "^$monitor" | grep -Eo '[0-9]+\.[0-9]+' | sort -V | tail -n 1)

if [[ -n $monitor && -n $resolution && -n $refresh_rate ]]; then
  xrandr --output "$monitor" --mode "$resolution" --rate "$refresh_rate"
fi

# Set the border colors for focused and normal windows
bspc config focused_border_color "#92b9ea"
bspc config normal_border_color "#201b30"
bspc config active_border_color "#3d3752"
bspc config presel_feedback_color "#b483c0"
bspc config border_width 2
bspc config borderless_monocle true

# Set mouse cursor to left pointer
xsetroot -cursor_name left_ptr

# Configure gaps and window gap size
bspc config gapless_monocle false
bspc config window_gap 10

# Configure actions for moving and resizing floating windows with the mouse (mod4 = Super/Windows key)
bspc config pointer_modifier mod4
bspc config pointer_action1 move
bspc config pointer_action2 resize_side
bspc config pointer_action3 resize_corner

# Rules
bspc rule -a Control-panel.py state=floating layer=above center=on sticky=on border=off follow=on focus=on
bspc rule -a feh state=floating
bspc rule -a '*:sun-awt-X11-XWindowPeer' manage=off
bspc rule -a 'vlc' state=floating center=true
bspc rule -a 'Blueman-manager' state=floating center=true
bspc rule -a 'qt5ct' state=floating rectangle=900x600+0+0 center=true
bspc rule -a 'qt6ct' state=floating rectangle=900x600+0+0 center=true
bspc rule -a 'ark' state=floating center=true
bspc rule -a 'Xarchiver' state=floating center=true
bspc rule -a 'pavucontrol' state=floating center=true
bspc rule -a 'Yad' state=floating center=true
bspc rule -a 'Blueman-manager' state=floating center=true
bspc rule -a 'Gnome-calculator' state=floating rectangle=360x500+0+0 center=true
bspc rule -a 'loupe' state=floating rectangle=1200x800+0+0 center=true
bspc rule -a 'pavucontrol' state=floating rectangle=920x450+0+0 center=true
bspc rule -a 'gamescope' desktop=11
bspc rule -a 'legcord' desktop=10

# Resources and Utilities
# DISPLAY/XAUTHORITY must reach systemd-user + D-Bus or graphical portal backends
# (xdg-desktop-portal-gtk, used by browser file pickers) fail with "cannot open display".
systemctl --user import-environment DISPLAY XAUTHORITY XDG_CURRENT_DESKTOP
dbus-update-activation-environment --systemd DISPLAY XAUTHORITY XDG_CURRENT_DESKTOP
[ -f "$HOME/.Xresources" ] && xrdb merge "$HOME/.Xresources"
xsettingsd &
dunst &
flameshot &
sh $HOME/.config/polybar/launch.sh
sh $HOME/.config/scripts/polkitkdeauth.sh
sh $HOME/.config/scripts/set-wallpaper.sh

# Clipboard history daemon (super + v)
command -v greenclip >/dev/null 2>&1 && { pgrep -f 'greenclip daemon' >/dev/null || greenclip daemon & }

# Particle field on the wallpaper layer (run `particles` any time to toggle)
if command -v particles >/dev/null 2>&1; then
    particles --start &
elif [ -x "$HOME/.local/bin/particles" ]; then
    "$HOME/.local/bin/particles" --start &
fi


# Clipboard sync
if command -v clipnotify >/dev/null 2>&1 && command -v xclip >/dev/null 2>&1 && command -v cliphist >/dev/null 2>&1; then
  (
    while clipnotify; do
      xclip -o -selection c | cliphist store
    done
  ) &
fi

# Apps to open on startup
legcord &
xset s off &
xset s noblank &
xset dpms 0 0 0 &
