#!/usr/bin/env bash

## Bspwm config directory
BSPDIR="$HOME/.config/bspwm"

## Bspwm colors
BSPWM_FBC='#fff'
BSPWM_NBC='#1E1E2E'
BSPWM_ABC='#fff'
BSPWM_PFC='#A6E3A1'

## Bspwm appearance
BSPWM_BORDER='0'
BSPWM_GAP='14'
BSPWM_SRATIO='0.50'

## Manager Workspaces
workspaces() {
    name=1
    for monitor in `bspc query -M`; do
        bspc monitor ${monitor} -n "$name" -d '' '' '' '' '' '' '' ''
        let name++
    done
}
workspaces

## Apply bspwm configurations
bspc config border_width "$BSPWM_BORDER"
bspc config window_gap "$BSPWM_GAP"
bspc config split_ratio "$BSPWM_SRATIO"

bspc config focused_border_color "$BSPWM_FBC"
bspc config normal_border_color "$BSPWM_NBC"
bspc config active_border_color "$BSPWM_ABC"
bspc config presel_feedback_color "$BSPWM_PFC"

bspc config borderless_monocle true
bspc config gapless_monocle true
bspc config paddingless_monocle true
bspc config single_monocle false
bspc config focus_follows_pointer true
bspc config presel_feedback true

## Manage all the unmanaged windows remaining from a previous session.
bspc wm --adopt-orphans

# remove all rules first
bspc rule -r *:*

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

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

# Lauch keybindings daemon
sxhkd -c "$BSPDIR"/sxhkdrc &

# Set Wallpaper
feh --no-fehbg --bg-fill "$BSPDIR"/wallpapers/cat_leaves.png &

# Exec Eww bar
# eww -c "$HOME"/.config/eww/dashboard open dashboard &

# Exec Polybar
polybar -q astro -c $HOME/.config/bspwm/polybar/config.ini &

# Exec Picom
picom -b --animations --animation-window-mass 0.5 --animation-for-open-window zoom --animation-stiffness 350 --corner-radius 12 &

# Exec Dunst
dunst -config $HOME/.config/bspwm/dunstrc &