#! /bin/bash

# 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"
sh -c 'function shenv() { export "$1=$2"; }; source ~/.env;'

# Workspaces Configuration
bspc monitor DisplayPort-1 -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 "#727d53"
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 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
dbus-update-activation-environment --systemd XDG_CURRENT_DESKTOP
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 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 &
