#!/bin/bash

# EXPERIMENTAL 2019-11-30: It works after connecting a monitor to the
# laptop, but not when disconnecting.  The problem seems to be with
# BSPWM, since `bspc query -M' continues to list the second monitor…
# Will investigate further.
#
# 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/>.
#
## Commentary:
#
# Run this after {dis,}connecting an external monitor.  It will
# recognifure the layout, based on the functions provided in the scripts
# it calls.

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

# In case the Hyper key ever stops working, we can call this script
# (Ctrl+Alt+Delete on my sxhkdrc) and apply the modifications again.
_kbd_reset_and_xmodmap() {
    xmod="$HOME"/.Xmodmap

    setxkbmap -layout 'us,el' -option 'caps:hyper,compose:menu'
    [ -f "$xmod" ] && xmodmap "$xmod"
}

_check bspwm_conf_xrandr && bspwm_conf_xrandr
_check bspwm_conf_desktops && bspwm_conf_desktops
_kbd_reset_and_xmodmap

# Apply the last defined wallpaper
fehbg="$HOME"/.fehbg

if [ -f "$fehbg" ] && _check feh; then
    sh -c "$fehbg"
fi

# Reload the panel
pkill -x melonpanel && melonpanel

# Switch my font sizes in Emacs
pgrep -x emacs && emacsclient -e '(prot/fonts-per-monitor)'
