#!/bin/bash
#
# bspwmrc
# bspwm configuration file
#

## Start a hotkey daemon
pgrep sxhkd -u "$UID" || sxhkd &

## Monitor allocation
# Honestly pretty complex
setmon() {
	desktops_total=10
	desktops_primary=8
	desktops_secondary=$(( desktops_total - desktops_primary ))

	monitor_primary="$(xrandr -q | awk '/primary/{print $1}')"
	monitors_secondary=($(xrandr -q | grep ' connected' | grep -v 'primary' | awk '{print $1}'))
	# Ensure we have enough for every monitor
	if (( desktops_secondary < ${#monitors_secondary[@]} )); then
		echo "Adding more desktops to match connected monitors"
		desktops_secondary=${#monitors_secondary[@]}
		desktops_total=$(( desktops_secondary + desktops_primary ))
	fi
	if (( ${#monitors_secondary[@]} == 0 )); then
		echo "Only one monitor allocated"
		layout=$(seq --separator=" " 1 $desktops_total)
		echo "Setting layout $layout"
		bspc monitor $monitor_primary -d $layout
	else
		echo "${#monitors_secondary[@]} monitors connected"
		deskpermon=$(( desktops_secondary / ${#monitors_secondary[@]} ))
		layout=$(seq --separator=" " 1 $desktops_primary)
		echo "Setting primary layout $layout"
		bspc monitor $monitor_primary -d $layout

		for m in $(seq 0 $(( ${#monitors_secondary[@]} - 1 ))); do
			layout=$(seq --separator=" " \
				$(( 1 + m + desktops_primary )) \
				${#monitors_secondary[@]} \
				"$desktops_total" \
				)
			echo "Setting layout for ${monitors_secondary[$m]} to $layout"
			bspc monitor ${monitors_secondary[$m]} -d $layout
		done
	fi
}
setmon

## Behavior
# Tiling behavior
bspc config split_ratio			0.50
bspc config remove_disabled_monitors	false
bspc config remove_unplugged_monitors	true

# Pointer behavior
bspc config click_to_focus		any
bspc config focus_follows_pointer	true
bspc config pointer_follows_monitor	true
bspc config pointer_modifier		mod1
bspc config pointer_action1		move #LMB
bspc config pointer_action2		resize_corner #RMB

# ewmh behavior
bspc config ignore_ewmh_focus		true # Fuck you, Discord

## Looks
# Padding
bspc config border_width		6
bspc config window_gap			8
bspc config single_monocle		false
bspc config borderless_monocle		false
bspc config gapless_monocle		false
# Colors
case $THEME in
	light)
		bspc config normal_border_color			"#f2e5bc" #Unfocused
		bspc config active_border_color			"#8ec07c" #Focused, but current monitor isn't
		bspc config focused_border_color		"#83a598" #Focused completely
		bspc config presel_feedback_color		"#ebdbb2" #???
		;;
	*)
		bspc config normal_border_color			"#32302f" #Unfocused
		bspc config active_border_color			"#8ec07c" #Focused, but current monitor isn't
		bspc config focused_border_color		"#83a598" #Focused completely
		bspc config presel_feedback_color		"#ebdbb2" #???
		;;
esac

## Rules
bspc rule -r '*'
case $HOSTNAME in
	dsk-ryzen-0*)
		# Big boy gets pseudotiled Firefox
		bspc rule -a Firefox state=pseudo_tiled rectangle=1920x1080+0+0
		;;
	*)
		;;
esac
bspc rule -a Steam desktop=7
bspc rule -a KeePassXC desktop=8
bspc rule -a slack desktop=9
bspc rule -a Riot desktop=9
bspc rule -a Element desktop=9
bspc rule -a discord desktop=9
bspc rule -a spotify desktop=10
bspc rule -a cantata desktop=10
bspc rule -a 'Minecraft 1.7.10' state=fullscreen
bspc rule -a 'NieR:Automata' state=fullscreen
bspc rule -a modloader64-gui state=floating border=off
# Plasma rules
bspc rule -a plasmashell state=floating sticky=on border=off
bspc rule -a krunner state=floating layer=above sticky=on border=off
# Widgets
bspc rule -a Conky state=floating layer=below sticky=on
bspc rule -a pavucontrol-qt state=floating layer=above sticky=on
