#!/usr/bin/env bash

BORDER_WIDTH=5 # change in bspwmrc
export WINDOW_GAP=25
DESKTOPS=(dev web gfx env play chat misc mon out util)

BSPWM_MONITORS=$(bspc query -M | tac)
MONITOR_COUNT=$(echo "${BSPWM_MONITORS}" | wc -w | awk '{ printf $1 }')
NUM_DESKTOPS=${#DESKTOPS[@]}
PER_MONITOR=$((NUM_DESKTOPS / MONITOR_COUNT))
REVERSE_DESKTOP_ORDERING=0

# allow a per-device config to override options
if [ -f "$EDFP/bspwm" ]; then
	source "$EDFP/bspwm"
fi

bspc config normal_border_color      "$(xrdb -query | sed -ne 's/.*background:\s*//p')"
bspc config focused_border_color      "$(xrdb -query | sed -ne 's/.*color0\?4:\s*//p')"
bspc config active_border_color       "$(xrdb -query | sed -ne 's/.*color0\?4:\s*//p')"
bspc config presel_feedback_color     "$(xrdb -query | sed -ne 's/.*color0\?4:\s*//p')"
bspc config border_width              "$BORDER_WIDTH"
bspc config split_ratio               0.5
bspc config borderless_monocle        true
bspc config gapless_monocle           true
bspc config pointer_modifier			    "mod4"
bspc config remove_unplugged_monitors true
bspc config remove_disabled_monitors  true

bspc config window_gap "$WINDOW_GAP"

bspc rule -a "*" split_dir=right

i=1
for mon in ${BSPWM_MONITORS}; do
  max=$((i + PER_MONITOR - 1))
	screens=
	for j in $(seq $i $max); do
		ind=${DESKTOPS[$((j - 1))]}
		screens="${screens}${ind} "
	done
  bspc monitor "$mon" -d $screens
  i=$((max + 1))
done

if [[ $REVERSE_DESKTOP_ORDERING -eq 1 ]]; then
	prev_mon=
	for mon in ${BSPWM_MONITORS}; do
		if [[ ! -z $prev_mon ]]; then
			bspc monitor "$mon" --swap "$prev_mon"
		fi
		prev_mon="$mon"
	done
fi

# allow a per-device config to override options
if [ -f "$EDFP/bspwm-after" ]; then
	source "$EDFP/bspwm-after"
fi