#!/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
# -----------
#
# This script has a dual purpose when executed from my laptop:
#
# 1. To be run at the start of a `bspwm` session in order to determine
#    whether to apply my dual monitor setup.
# 2. To configure the display manager so that its output is as intended.
#
# The `lightdm` display manager (the login screen) can have trouble
# setting the correct layout for a multihead setup where the monitors
# have different display resolutions.  On my ThinkPad X220 I often
# connect a 1920x1080 monitor via the VGA port.  What this SIMPLISTIC
# script does is make sure that `lightdm` runs the appropriate `xrandr`
# command, depending on whether the external monitor is connected or
# not.
#
# This script is part of my custom desktop session.  For more, see my
# dotfiles https://gitlab.com/protesilaos/dotfiles.
#
# Last full review on 2019-04-21

# Configure this for LightDM
#
# To instruct `lightdm` to run this at startup on a machine running
# Debian Buster/Sid, you must edit `/etc/lightdm/lightdm.conf` (requires
# root privileges, but make sure you back up the original):
#
# 1. Under the `[Seat:*]` section, search for "display-setup-script".
# 2. Comment it out to enable it and add the path to this script.
#
# Example:
#
# display-setup-script=/home/prot/bin/own_script_laptop_dual_monitor
#
# Note that when searching for "display-setup-script" the first result
# is the description.  Make sure you find the one below the `[Seat:*]`
# which has no comment mark in front of it.
#
# Dependencies:
#
# sudo apt install lightdm lightdm-gtk-greeter
#
# This might also work with other greeters, such as `slick-greeter`.
# Needs further testing.

# XrandR functions
# ----------------
# XXX NOTE XXX These will have no permanent effect if another program
# adjusts the screen's colours and brightness, such as `redshift`.

laptop_coordinates() {
	if [ "$#" == 1 ]; then
		xrandr --output LVDS1 --primary --mode 1366x768 --pos 0x1080
	else
		xrandr --output LVDS1 --mode 1366x768 --pos 0x1080
	fi
}

laptop_brightness() {
	# I define my host names in a local file.  The format is
	# `my_MACHINE="VALUE"`
	local laptop_host
	laptop_host="$(sed '/my_laptop/!d ; s,.*"\([a-zA-Z0-9]*\)",\1,' "$HOME/.my_hostnames")"

	# This is specific to my Lenovo ThinkPad X220.  It improves the
	# reproduction of colours, especially when working with light
	# themes.
	if [ "$laptop_host" == "$(hostname)" ]; then
		xrandr --output LVDS1 --brightness 1.0 --gamma 0.76:0.75:0.68
	fi
}

multihead_coordinates() {
	echo "Configuring LVDS1 + VGA1 XrandR coordinates"
	# pass a single argument to activate the --primary option
	laptop_coordinates 'primary'

	# configure the external display on the VGA port
	xrandr --output VGA1 --mode 1920x1080 --pos 0x0
}

multihead_brightness() {
	echo "Configuring LVDS1 + VGA1 XrandR properties"
	laptop_brightness
	xrandr --output VGA1 --brightness 1 --gamma 1:1:1
}

# Conditions for running this script
# ----------------------------------

# If the VGA1 port connects to a monitor, the output of the variable
# will not be empty.  In which case it is assumed that I am using my
# external display, whose resolution I know in advance.  If the variable
# is empty, then no external monitor is connected.
#
# This is a simplistic approach which will not work if the external
# monitor has another resolution.  It will also fail if executed from
# another machine, say, another laptop that uses an HDMI connection
# instead.
my_laptop_external_monitor=$(xrandr --query | grep 'VGA1 connected')

# We check for DESKTOP_SESSION to make sure the brightness/gamma
# settings are not executed in places such as the lightdm login
# screen.
if [ -n "$my_laptop_external_monitor" ] && [ "$DESKTOP_SESSION" == 'bspwm' ]; then
	multihead_coordinates
	multihead_brightness
elif [ -n "$my_laptop_external_monitor" ]; then
	multihead_coordinates
else
	echo "Configuring LVDS1 XrandR settings"
	laptop_coordinates
	laptop_brightness
fi
