#!/bin/sh
set -e

# determine where to place the log file
logfile="$HOME/.dkrc.log"
: > "$logfile"

border_width=5
border_outer_width=3

{
	# 9 workspaces per monitor
	dkcmd set numws=9

	# default workspace '_' values used when allocating new workspaces
	# can be applied to all existing workspaces when passed 'apply' after ws=_
	dkcmd set ws=_ apply layout=tile master=1 stack=4 gap=6 msplit=0.55 ssplit=0.5

	# focus windows when receiving activation and enable focus-follows-mouse
	dkcmd set focus_open=true focus_urgent=true focus_mouse=true

	# place clients at the tail and ignore size hints on tiled windows
	dkcmd set tile_tohead=0 tile_hints=false

	# minimum width/height for resizing, and minimum allowed on-screen when moving
	dkcmd set win_minwh=50 win_minxy=10

	# disable gaps and borders in single window layouts
	dkcmd set smart_gap=true smart_border=true

	# define mouse mod and move/resize buttons
	dkcmd set mouse mod=super move=button1 resize=button3

	# obey motif border hints on windows that draw their own (steam, easyeffects, etc.)
	dkcmd set obey_motif=true

	dkcmd set border width=$border_width outer_width=$border_outer_width \
		colour \
		focus='#fd0c0c' \
		unfocus='#222222' \
		urgent='#ee5555' \
		outer_focus='#222222' \
		outer_unfocus='#3030ef' \
		outer_urgent='#222222'

	dkcmd rule class="^(dooble|firefox|librewolf)$" ws=2
	dkcmd rule class="^discord$" ws=6
	dkcmd rule class="^spotify$" ws=8
	dkcmd rule class="^(mpv|nsxiv)$" float=true

	# define some terminals and allow them to be absorbed by spawned windows
	dkcmd rule class="^(st|lxterminal|st-256color|urxvt|kitty|alacritty|xterm|xterm-256color)$" terminal=true

	# set a window to never absorb other windows, like the xev event tester
	dkcmd rule title="^Event Tester$" no_absorb=true

	# focus window and workspace on opening
	# dkcmd rule class="^firefox$" ws=1 focus=true

	# apply current rule set to all existing windows (used mainly for WM restart)
	dkcmd rule apply '*'
} >> "$logfile" 2>&1

# inform of any errors in a notification
if grep -q 'error:' "$logfile"; then
	hash notify-send && notify-send -t 0 -u critical "dkrc has errors" \
		"$(awk '/error:/ {sub(/^error: /, ""); gsub(/</, "\<"); print}' "$logfile")"
	exit 1
fi

env | while read -r pair; do
	turnstile-update-runit-env "$(cut -d= -f1 <<< "$pair")"
done

