#!/usr/bin/env bash
#
#  ██████╗ ███████╗██████╗ ██╗    ██╗███╗   ███╗██████╗  ██████╗
#  ██╔══██╗██╔════╝██╔══██╗██║    ██║████╗ ████║██╔══██╗██╔════╝
#  ██████╔╝███████╗██████╔╝██║ █╗ ██║██╔████╔██║██████╔╝██║     
#  ██╔══██╗╚════██║██╔═══╝ ██║███╗██║██║╚██╔╝██║██╔══██╗██║     
#  ██████╔╝███████║██║     ╚███╔███╔╝██║ ╚═╝ ██║██║  ██║╚██████╗
#  ╚═════╝ ╚══════╝╚═╝      ╚══╝╚══╝ ╚═╝     ╚═╝╚═╝  ╚═╝ ╚═════╝

#  ┬┌┬┐┌─┐┌─┐┬─┐┌┬┐┌─┐┌┐┌┌┬┐  ┬  ┬┌─┐┬─┐┬┌─┐┌┐ ┬  ┌─┐┌─┐
#  ││││├─┘│ │├┬┘ │ ├─┤│││ │   └┐┌┘├─┤├┬┘│├─┤├┴┐│  ├┤ └─┐
#  ┴┴ ┴┴  └─┘┴└─ ┴ ┴ ┴┘└┘ ┴    └┘ ┴ ┴┴└─┴┴ ┴└─┘┴─┘└─┘└─┘


PATH="$HOME/.config/bspwm/scripts:$PATH"
rice_dir="$HOME/.config/bspwm/karla"

## Fix java applications
export _JAVA_AWT_WM_NONREPARENTING=1

#  ╦ ╦╔═╗╦═╗╦╔═╔═╗╔═╗╔═╗╔═╗╔═╗╔═╗
#  ║║║║ ║╠╦╝╠╩╗╚═╗╠═╝╠═╣║  ║╣ ╚═╗
#  ╚╩╝╚═╝╩╚═╩ ╩╚═╝╩  ╩ ╩╚═╝╚═╝╚═╝

	# Default 1 monitor with 6 workspaces
	for monitor in "$(xrandr -q | grep -w 'connected' | cut -d' ' -f1)"; do
		bspc monitor "$monitor" -d '1' '2' '3' '4' '5' '6'
		#bspc monitor "$monitor" -d '' '' '' '' '' ''
	done

	## For two or three monitors configuration see https://github.com/gh0stzk/dotfiles/wiki/Two-or-more-monitors-setup


#  ┌┐ ┌─┐┌─┐┬ ┬┌┬┐  ┌─┐┌─┐┌┐┌┌─┐┬┌─┐
#  ├┴┐└─┐├─┘││││││  │  │ ││││├┤ ││ ┬
#  └─┘└─┘┴  └┴┘┴ ┴  └─┘└─┘┘└┘└  ┴└─┘

bspc config external_rules_command $HOME/.config/bspwm/scripts/ExternalRules

bspc config window_gap			  6
bspc config split_ratio                   0.51
bspc config single_monocle                true
bspc config borderless_monocle            false
bspc config gapless_monocle               false

bspc config focus_follows_pointer         true
bspc config pointer_follows_focus         false
bspc config pointer_motion_interval       5
bspc config pointer_modifier              mod4 
bspc config pointer_action1               move 
bspc config pointer_action2               resize_side 
bspc config pointer_action3               resize_corner

#bspc wm --adopt-orphans
bspc rule -a scratch sticky=on state=floating focus=on

#  ╔═╗╦ ╦╔╦╗╔═╗╔═╗╔╦╗╔═╗╦═╗╔╦╗
#  ╠═╣║ ║ ║ ║ ║╚═╗ ║ ╠═╣╠╦╝ ║ 
#  ╩ ╩╚═╝ ╩ ╚═╝╚═╝ ╩ ╩ ╩╩╚═ ╩

# Set system vars for polybar
. SetSysVars

# Terminate already running polybar,picom, sxhkd and dunst instances
processes=("eww" "picom" "polybar" "sxhkd" "dunst")

for process in "${processes[@]}"; do
  if pidof -q "$process"; then
    pkill -9 -x "$process" > /dev/null; sleep 0.1
  fi
done

# Load colors, dunst, bars and/or eww widgets
. ${rice_dir}/Theme.sh

# Launch picom // If you have old hardware or encounter problems 
# uncomment the picom "--legacy-backends" line and comment the current one.
pidof -q picom || { picom --config "$HOME"/.config/bspwm/picom.conf & }
#pidof -q picom || { picom --legacy-backends --config "$HOME"/.config/bspwm/picom.conf & }

# Launch dunst notification daemon
dunst -config "$HOME"/.config/bspwm/dunstrc &

# Set random wallpaper for actual rice
feh -z --no-fehbg --bg-fill ${rice_dir}/walls

# Launch eww daemon
pidof -q eww || { eww -c "$HOME"/.config/bspwm/eww daemon & }

# Launch stalonetray
stalonetray -c "$HOME"/.config/bspwm/stalonetrayrc & sleep 0.1 && xdo hide -N stalonetray && touch "/tmp/syshide.lock"

# Reload sxhkd daemon
sxhkd -c "$HOME"/.config/bspwm/sxhkdrc &

# Launch polkit
pidof -q polkit-gnome-authentication-agent-1 || { /usr/lib/polkit-gnome/polkit-gnome-authentication-agent-1 & }

# Fix cursor
xsetroot -cursor_name left_ptr
