#!/bin/bash

	# 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 <http://www.gnu.org/licenses/>.

# Description
# -----------
#
# Configure `bspwm` depending on the host computer. Part of my dotfiles.
# See https://gitlab.com/protesilaos/dotfiles
#
# This script implements some important settings, such as the number of
# workspaces.  The reason these are not defined in `bspwmrc` is to keep
# things clean and organised.  Also, this is better for testing
# purposes.  It has been proven more difficult to debug various
# scripting patterns when they are embedded directly in the `bspwmrc`
# file.
#
# The approach followed herein is largely simplistic because I know the
# setup in advance. I deploy my dotfiles on two separate machines.  On
# one I often run a dual monitor setup.  As such, there are some bespoke
# configs for each host.

# I define my host names as BASH variables in this file.  The format is
# `my_variable='my_value'`.  Then I source it to retrieve the value I
# need.  This makes it possible to run commands depending on the
# computer you are working on.
source "$HOME/.my_hostnames"

# On my laptop I often connect an external monitor.  This is a simple
# way to see if I have 2 monitors or not.  On the desktop, which is my
# secondary computer, I always have a single monitor.  This is a
# simplistic approach that works only because I know the setup[s] in
# advance.
monitor_count="$(bspc query -M | wc -l)"

# These should be applied if only one monitor is available.  This always
# applies to my desktop computer.  It also is true for my laptop on
# occasions where no external monitor is connected.
bspwm_generic_workspaces() {
	# A single desktop on the monitor.  We only define one, because of
	# another script I have that implements dynamic desktops.
	bspc monitor -d 1
}

# These apply to my laptop when an external monitor is connected.
bspwm_laptop_dual_monitor() {
	# Set the workspaces per monitor.  We only define one per monitor,
	# because of another script I have that implements dynamic
	# desktops.
	bspc monitor VGA1 -d 1
	bspc monitor LVDS1 -d 8

	# make sure borders are always on, otherwise it is impossible to
	# find the focused window on two monitors with monocles…
	bspc config borderless_monocle false

	# Add padding equal to panel height to fix issue where there is an
	# overlap between panel and windows in LVDS1.  The panel gets
	# covered by the windows.
	if pgrep -x melonpanel > /dev/null; then
		local panel_height
		panel_height="$(sed '/^melonpanel_height=/!d ; s,.*\([0-9]\{2\}\).*,\1,' $(command -v melonpanel 2> /dev/null))"
		bspc config top_padding "$panel_height"
	fi
}

# Run commands depending on the host computer.
case "$(hostname)" in
	"$my_laptop_hostname")
		echo "You are on $my_laptop_hostname"

		# run the script that adds the appropriate `xrandr` settings
		if [ "$(command -v own_script_laptop_dual_monitor 2> /dev/null)" ]; then
			own_script_laptop_dual_monitor
		fi

		# Is an external monitor connected to my laptop?
		if [ "$monitor_count" == 2 ]; then
			echo "Monitor count is equal to 2"
			echo "Defining per-monitor workspaces"
			bspwm_laptop_dual_monitor
		else
			bspwm_generic_workspaces
		fi
		;;
	*)
		echo "You are not on $my_laptop_hostname"
		echo "Defaulting to generic workspace configurations"
		bspwm_generic_workspaces
		;;
esac
