#!/bin/bash

# bspwmrc --- Initialisation script for BSPWM.
#
# Copyright (c) 2019-2025  Protesilaos Stavrou <info@protesilaos.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
#
## Commentary:
#
# This file is part of my custom desktop session:
# <https://github.com/protesilaos/dotfiles>.

### Essentials

# NOTE 2024-12-06: It seems my ~/.profile is not being read.  I need
# these here.

# Set PATH so it includes user's private executables.
if [ -d "$HOME"/.local/bin ]; then
    PATH=$PATH:"$HOME"/.local/bin
fi

if [ -d "$HOME"/Builds/bin ]
then
    PATH=$PATH:"$HOME"/Builds/bin
fi

# Auto unlocks the GPG and SSH agents.
eval $(/usr/bin/gnome-keyring-daemon --start --components=pkcs11,secrets,ssh)
# export SSH_AUTH_SOCK

# TODO 2023-10-08: Why does this work on my laptop, but not the above?
# Is the desktop affected?
export SSH_AUTH_SOCK="/run/user/$(id -u)/gnupg/S.gpg-agent.ssh"


# Shorten a couple of common commands that are used herein.
_bc() {
	bspc config "$@"
}

_check() {
	command -v "$1" > /dev/null
}

# # Determine XrandR settings.  Whether an external display is connected
# # to the laptop or not.
# _check bspwm_conf_xrandr.sh && bspwm_conf_xrandr.sh

# Define desktop[s] [per monitor].  Contingent on the above.  Also note
# that this leverages my `bspwm_dynamic_desktops'.
_check bspwm_conf_desktops.sh && bspwm_conf_desktops.sh

#### Window management
_bc automatic_scheme spiral
_bc initial_polarity second_child
_bc split_ratio 0.33
_bc border_width 3
_bc window_gap 5
_bc single_monocle true
_bc borderless_monocle true
_bc gapless_monocle false
_bc focus_follows_pointer true
_bc pointer_follows_monitor true
_bc pointer_modifier mod4
_bc pointer_action1 move
_bc pointer_action2 resize_side
_bc ignore_ewmh_focus false

_check bspwm_external_rules.sh && \
    _bc external_rules_command \
        "$(command -v bspwm_external_rules.sh)"

#### Colours
_check bspwm_conf_colors.sh && bspwm_conf_colors.sh

#### Autostart

# Common settings used by my BSPWM and Herbstluftwm setups
# (collectively referred to as "Xorg Tiling Window Managers").  I
# configure those window managers to be similar.
_check xtwm_common.sh && xtwm_common.sh

# Add padding equal to the panel height to fix an issue where there is
# an overlap between panel and windows.
_panel_height() {
    awk -F '=' '/height/ { print $2; exit; }' \
        "$HOME"/.config/polybar/config.ini
}

bspc config bottom_padding "$(_panel_height)"
