#!/bin/bash

# bspwm focus mode --- removes gaps, padding, panel from the viewport.
#
# This script is meant to be used in my custom desktop session:
# https://gitlab.com/protesilaos/dotfiles.
#
# Debian Buster dependencies:
#	sudo apt install bspwm lemonbar

# Copyright (c) 2019 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 <http://www.gnu.org/licenses/>.

# This assumes that we are already running the lemonbar script.
[ -z "$(pgrep -x melonpanel > /dev/null)" ] || { echo "No running melonpanel"; exit 1; }

panel_height="$(sed '/^melonpanel_height=/!d ; s,.*\([0-9]\{2\}\).*,\1,' "$(command -v melonpanel 2> /dev/null)")"

# The top padding this is not defined in bspwmrc but in another script
# that sets the value on a per-host-computer basis.  We just get the
# value from the running program.
top_padding="$(bspc config top_padding)"

# Read the default setting from the bspwm config file.
_get_value() {
	local bspwmrc
	bspwmrc="$HOME/.config/bspwm/bspwmrc"

	sed "/$1/!d ; s,.* $1 \([a-z0-9]*\).*,\1," "$bspwmrc"
}

## Use the function that gets the desired value from bspwmrc.
window_gap="$(_get_value window_gap)"
gapless_monocle="$(_get_value gapless_monocle)"
paddingless_monocle="$(_get_value paddingless_monocle)"
border_width="$(_get_value border_width)"

# Calculate the actual gap from the top of the viewport to the upper
# part of the nodes.
effective_padding=$(("$top_padding" + "$window_gap"))

# If we are not in "focus mode"...
if [ "$effective_padding" -ge "$panel_height" ] ; then
	top_padding=0
	window_gap=0
	gapless_monocle=true
	paddingless_monocle=true
	border_width=4

	# No need to keep the panel
	pkill -xo melonpanel && pkill -x lemonbar
else
	# Unset everything
	unset top_padding
	unset window_gap
	unset gapless_monocle
	unset paddingless_monocle
	unset border_width
	panel_status=on
fi

# Create overlapping borders in focus mode.
if [ -n "$border_width" ]; then
	bspc config window_gap -"$border_width"
	bspc config border_width "$border_width"
	for side in top right bottom left; do
		bspc config "${side}"_padding "$border_width"
	done
else
	bspc config window_gap "${window_gap:=4}"
	bspc config border_width "${border_width:=2}"
	bspc config top_padding "${top_padding:=$panel_height}"
	for side in right bottom left; do
		bspc config "${side}"_padding "0"
	done
fi

bspc config gapless_monocle "${gapless_monocle:=false}"
bspc config paddingless_monocle "${paddingless_monocle:=false}"

[ -n "$panel_status" ] && melonpanel
