#!/usr/bin/env bash

## Copyright (C) 2020-2023 Aditya Shakya <adi1090x@gmail.com>
##
## BSPWM config file for debiancraft

## General ---------------------------------------------------#

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

## Export bspwm/bin dir to PATH
export PATH="${PATH}:$BSPDIR/scripts"

## Run java applications without issues
export _JAVA_AWT_WM_NONREPARENTING=1
#wmname LG3D

## Current theme ---------------------------------------------#

## Bspwm colors
BSPWM_FBC='#81A1C1'
BSPWM_NBC='#2E3440'
BSPWM_ABC='#B48EAD'
BSPWM_PFC='#A3BE8C'

## Bspwm appearance
BSPWM_BORDER='1'
BSPWM_GAP='10'
BSPWM_SRATIO='0.50'

## Configurations --------------------------------------------#

## Manager Workspaces
workspaces() {
	name=1
	for monitor in `bspc query -M`; do
		#bspc monitor ${monitor} -n "$name" -d '1' '2' '3' '4' '5' '6' '7' '8'
		#bspc monitor ${monitor} -n "$name" -d 'A' 'B' 'C' 'D' 'E' 'F' 'G' 'H'
		#bspc monitor ${monitor} -n "$name" -d 'I' 'II' 'III' 'IV' 'V' 'VI' 'VII' 'VIII'
		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

## Window rules ----------------------------------------------#

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

## 1 > terminal (always open terminal on workspace-1)
bspc rule -a Alacritty desktop='^1' follow=on focus=on
bspc rule -a Xfce4-terminal desktop='^1' follow=on focus=on

## 2 > web (always open web browser on workspace-2)
bspc rule -a firefox desktop='^2' follow=on focus=on
bspc rule -a chromium desktop='^2' follow=on focus=on

## 3 > files (always open file manager on workspace-3)
declare -a files=(Pcmanfm Thunar qBittorrent)
for i in ${files[@]}; do
   bspc rule -a $i desktop='^3' follow=on focus=on; done

## 4 > code (always open editors on workspace-4)
declare -a code=(Geany code-oss)
for i in ${code[@]}; do
   bspc rule -a $i desktop='^4' follow=on focus=on; done

## 5 > office and docs (always open office/doc apps on workspace-5)
declare -a office=(Gucharmap Atril Evince \
libreoffice-writer libreoffice-calc libreoffice-impress \
libreoffice-startcenter libreoffice Soffice *:libreofficedev *:soffice)
for i in ${office[@]}; do
   bspc rule -a $i desktop='^5' follow=on focus=on; done

## 6 > communication (always open communication apps on workspace-6)
declare -a comm=(Thunderbird TelegramDesktop Hexchat)
for i in ${comm[@]}; do
   bspc rule -a $i desktop='^6' follow=on focus=on; done

## 7 > media (always open media apps on workspace-7)
declare -a media=(Audacity Music MPlayer Lxmusic Inkscape Gimp-2.10 obs)
for i in ${media[@]}; do
   bspc rule -a $i desktop='^7' state=floating follow=on focus=on; done

## 8 > system (always open system apps on workspace-8)
bspc rule -a 'VirtualBox Manager' desktop='^8' follow=on focus=on
bspc rule -a GParted desktop='^8' follow=on focus=on
declare -a settings=(Lxappearance Lxtask Lxrandr Arandr \
System-config-printer.py Pavucontrol Exo-helper-1 \
Xfce4-power-manager-settings)
for i in ${settings[@]}; do
   bspc rule -a $i desktop='^8' state=floating follow=on focus=on; done

## Always Floating Apps
declare -a floating=(alacritty-float Pcmanfm Thunar Onboard Yad 'Firefox:Places' \
Viewnior feh Nm-connection-editor)
for i in ${floating[@]}; do
   bspc rule -a $i state=floating follow=on focus=on; done

bspc rule -a Conky state=floating manage=off
bspc rule -a stalonetray state=floating manage=off

## Autostart -------------------------------------------------#

# Kill if already running
killall -9 xsettingsd sxhkd dunst ksuperkey xfce4-power-manager bspc

# Lauch xsettingsd daemon
xsettingsd --config="$BSPDIR"/xsettingsd &

# polkit agent
if [[ ! `pidof xfce-polkit` ]]; then
	/usr/lib/xfce-polkit/xfce-polkit &
fi

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

# Enable Super Keys For Menu
ksuperkey -e 'Super_L=Alt_L|F1' &
ksuperkey -e 'Super_R=Alt_L|F1' &

# Enable power management
xfce4-power-manager &

# Fix cursor
xsetroot -cursor_name left_ptr

# Restore wallpaper
bash ~/.fehbg

# Start mpd
exec mpd &

# Start bspwm scripts
bspbar
bspcomp
bspdunst
bspfloat &
