#!/bin/bash

# hlwm autostart --- Initialisation script for HerbstluftWM
#
# Copyright (c) 2021-2024  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>.

## Code:

# 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"


### Essentials

_hc ()
{
    herbstclient "$@"
}

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

_hc emit_hook reload

# Remove all existing keybindings.  We set them further below via
# SXHKD.  The only exception is the mouse where we want Super+Right
# Click drag to move a floating window and Super+Left Click drag to
# resize any window at its nearest side.
_hc keyunbind --all
_hc mousebind Mod4-Button1 move
_hc mousebind Mod4-Button3 resize

### Tags and monitors

# unlock, just to be sure
_hc unlock

# NOTE that tags must be defined before the monitors, while the
# default_frame_layout has to be set before the tags as it affects all
# new frames.

_hc set default_frame_layout max

tag_names=( {0..9} )

_hc rename default "${tag_names[0]}" || true

for i in "${!tag_names[@]}"
do
    _hc add "${tag_names[$i]}"
done

if [ -f "$HOME"/.config/herbstluftwm/my_herbstluftwm_workspaces.txt ]
then
    if [ "$(xrandr | grep "\\<connected\\>" | awk -F ' ' '{ print $3; exit; }' | awk -F 'x' '{ print $1; exit; }')" -gt 1920 ]
    then
        _check herbstluftwm_loadstate.sh && herbstluftwm_loadstate.sh < "$HOME"/.config/herbstluftwm/my_herbstluftwm_workspaces.txt
    else
        _check herbstluftwm_loadstate.sh && echo "0: (clients max:0)" | herbstluftwm_loadstate.sh
    fi
fi

# NOTE 2024-12-06: I am no longer using this, but I am keeping it here
# because it is a useful feature.
#
# Set up virtual monitors.  On my desktop computer, the monitor's
# resolution is 2560x1080 (and what inspired me to set up
# HerbstluftWM).  Also check my script 'herbstluftwm_cycle_monitors'.
# [ "$HOSTNAME" = "kronos" ] && _hc set_monitors 640x1080+0+0 1920x1080+640+0

### Configuration

_hc set auto_detect_panels on

_hc set window_gap 0
_hc set frame_gap 0
_hc set frame_border_width 0
_hc set frame_padding 0
_hc set smart_window_surroundings off
_hc set smart_frame_surroundings off
_hc set mouse_recenter_gap 0
_hc set focus_follows_mouse on
_hc set always_show_frame on
_hc set frame_bg_transparent off
_hc set frame_normal_opacity 15
_hc set frame_active_opacity 50

_hc attr theme.inner_width 1
_hc attr theme.border_width 2
_hc attr theme.active.outer_width 1
_hc attr theme.floating.border_width 1
_hc attr theme.floating.outer_width 1

#### Frame title
_hc attr theme.title_height 10
_hc attr theme.title_depth 3
_hc attr theme.title_font "Monospace:size=7:weight=bold" # see my fontconfig setup
_hc attr theme.normal.title_font "Monospace:size=7"
_hc attr theme.title_align center
_hc attr theme.title_when multiple_tabs # only with the max layout

#### Colours
_check herbstluftwm_conf_colors.sh && herbstluftwm_conf_colors.sh

#### Rules
# TODO 2021-11-08: I have kept the defaults here.  Needs review.

_hc unrule -F
_hc rule focus=on # normally focus new clients
_hc rule floatplacement=none
_hc rule fixedsize floating=on
_hc rule windowtype~'_NET_WM_WINDOW_TYPE_(DIALOG|UTILITY|SPLASH)' floating=on
_hc rule windowtype='_NET_WM_WINDOW_TYPE_DIALOG' focus=on floatplacement=center
_hc rule title='prot-window-popup' focus=on floatplacement=center floating=on
_hc rule windowtype~'_NET_WM_WINDOW_TYPE_(NOTIFICATION|DOCK|DESKTOP)' manage=off

# Empty frames function as the equivalent of BSPWM's receptacles: they
# receive the next client.  However, this only works for the focused
# tag.
_hc rule index=e

### 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
