#!/bin/bash

# this is a simple config for herbstluftwm

hc() {
    herbstclient "$@"
}

hc emit_hook reload

# shellcheck source={{ .chezmoi.homeDir }}/.config/herbstluftwm/colors.sh
. "$(dirname "$0")/colors.sh"

### disable touchpad, cuz trackpoint rulezzzz
xinput --disable 'SynPS/2 Synaptics TouchPad'
xinput --disable 'SYNA88022:00 06CB:CE67 Touchpad'

xsetroot -solid "${colors[dark0_hard]}"

# remove all existing keybindings
hc keyunbind --all

# keybindings
# if you have a super key you will be much happier with Mod set to Mod4
#Mod=Mod1    # Use alt as the main modifier
Mod=Mod4   # Use the super key as the main modifier

hc keybind $Mod-Shift-q quit
hc keybind $Mod-Shift-r reload
hc keybind $Mod-q close
hc keybind $Mod-Return spawn systemd-run --scope --user kitty
hc keybind $Mod-x spawn systemd-run --scope --user rofi -modi drun -show drun
hc keybind $Mod-Shift-x spawn systemd-run --scope --user rofi -show fb -modi "fb:${HOME}/.config/herbstluftwm/file-browser"
hc keybind $Mod-z spawn systemd-run --scope --user rofi -modi "clipboard:greenclip print" -show clipboard -run-command '{cmd}'
hc keybind $Mod-c spawn systemd-run --scope --user rofi -lines 0 -show calc -modi calc -no-show-match -no-sort -no-history -calc-command "echo -n '{result}' | xclip"
hc keybind $Mod-a spawn systemd-run --scope --user ~/bin/gopassmenu
hc keybind $Mod-v spawn systemd-run --scope --user ~/bin/oncall-menu
hc keybind $Mod-Shift-a spawn systemd-run --scope --user rofi -show pb -modi "pb:$HOME/.config/herbstluftwm/passboltmenu2"
hc keybind $Mod-m spawn systemd-run --scope --user ~/.config/herbstluftwm/pmount-dmenu
hc keybind $Mod-Escape spawn systemd-run --scope --user dunstctl close

# basic movement in tiling and floating mode
# focusing clients
hc keybind $Mod-Left  focus left
hc keybind $Mod-Down  focus down
hc keybind $Mod-Up    focus up
hc keybind $Mod-Right focus right
hc keybind $Mod-h     focus left
hc keybind $Mod-j     focus down
hc keybind $Mod-k     focus up
hc keybind $Mod-l     focus right

# moving clients in tiling and floating mode
hc keybind $Mod-Shift-Left  shift left
hc keybind $Mod-Shift-Down  shift down
hc keybind $Mod-Shift-Up    shift up
hc keybind $Mod-Shift-Right shift right
hc keybind $Mod-Shift-h     shift left
hc keybind $Mod-Shift-j     shift down
hc keybind $Mod-Shift-k     shift up
hc keybind $Mod-Shift-l     shift right

# splitting frames
# create an empty frame at the specified direction
hc keybind $Mod-u       split   bottom  0.5
hc keybind $Mod-o       split   right   0.5
# let the current frame explode into subframes
hc keybind $Mod-Control-space split explode

# resizing frames and floating clients
resizestep=0.02
hc keybind $Mod-Control-h       resize left +$resizestep
hc keybind $Mod-Control-j       resize down +$resizestep
hc keybind $Mod-Control-k       resize up +$resizestep
hc keybind $Mod-Control-l       resize right +$resizestep
hc keybind $Mod-Control-Left    resize left +$resizestep
hc keybind $Mod-Control-Down    resize down +$resizestep
hc keybind $Mod-Control-Up      resize up +$resizestep
hc keybind $Mod-Control-Right   resize right +$resizestep

# tags
tag_names=( {1..9} )
tag_keys=( {1..9} )

hc rename default "${tag_names[0]}" || true
for i in "${!tag_names[@]}" ; do
    hc add "${tag_names[$i]}"
    key="${tag_keys[$i]}"
    if [ -n "$key" ] ; then
        hc keybind "$Mod-$key" use_index "$i"
        hc keybind "$Mod-Shift-$key" move_index "$i"
    fi
done

# cycle through tags
hc keybind $Mod-period use_index +1 --skip-visible
hc keybind $Mod-comma  use_index -1 --skip-visible

# layouting
hc keybind $Mod-r remove
hc keybind $Mod-s floating toggle
hc keybind $Mod-f fullscreen toggle
hc keybind $Mod-Shift-f set_attr clients.focus.floating toggle
hc keybind $Mod-Shift-d set_attr clients.focus.decorated toggle
hc keybind $Mod-Shift-m set_attr clients.focus.minimized true
hc keybind $Mod-Control-m jumpto last-minimized
hc keybind $Mod-p pseudotile toggle
# The following cycles through the available layouts within a frame, but skips
# layouts, if the layout change wouldn't affect the actual window positions.
# I.e. if there are two windows within a frame, the grid layout is skipped.
hc keybind $Mod-space                                                           \
            or , and . compare tags.focus.curframe_wcount = 2                   \
                     . cycle_layout +1 vertical horizontal max vertical grid    \
               , cycle_layout +1

# mouse
hc mouseunbind --all
hc mousebind $Mod-Button1 move
hc mousebind $Mod-Button2 zoom
hc mousebind $Mod-Button3 resize
hc mousebind $Mod-Control-Button1 call substitute WID clients.dragged.winid close WID

# focus
hc keybind $Mod-BackSpace   cycle_monitor
hc keybind $Mod-Tab         cycle_all +1
hc keybind $Mod-Shift-Tab   cycle_all -1
hc keybind $Mod-grave cycle
hc keybind $Mod-i jumpto urgent

# music
# hc keybind XF86AudioMute chain , spawn systemd-run --scope --user sh -c 'pactl set-sink-mute $(pactl get-default-sink) toggle' , emit_hook volume
# hc keybind XF86AudioLowerVolume chain , spawn systemd-run --scope --user sh -c 'pactl set-sink-volume $(pactl get-default-sink) -10%' , emit_hook volume
# hc keybind XF86AudioRaiseVolume chain , spawn systemd-run --scope --user sh -c 'pactl set-sink-volume $(pactl get-default-sink) +10%' , emit_hook volume
hc keybind XF86AudioMute spawn wpctl set-mute @DEFAULT_SINK@ toggle
hc keybind XF86AudioLowerVolume spawn wpctl set-volume @DEFAULT_SINK@ 10%-
hc keybind XF86AudioRaiseVolume spawn wpctl set-volume @DEFAULT_SINK@ 10%+
hc keybind XF86AudioMicMute spawn systemd-run --scope --user rofi -show pulse -modi "pulse:$HOME/.config/herbstluftwm/pulsemenu"
hc keybind XF86AudioNext spawn systemd-run --scope --user "$HOME/bin/mprisctl" next
hc keybind XF86AudioPrev spawn systemd-run --scope --user "$HOME/bin/mprisctl" prev
hc keybind XF86AudioPlay spawn systemd-run --scope --user "$HOME/bin/mprisctl" toggle

hc keybind XF86MonBrightnessDown spawn brightnessctl set 10%-
hc keybind XF86MonBrightnessUp spawn brightnessctl set 10%+

# shellcheck disable=2016 # needed to pass whole script to keybind...
hc keybind XF86TouchpadToggle spawn systemd-run --scope --user sh -c 'xinput --set-prop "SynPS/2 Synaptics TouchPad" "Device Enabled"  $(xinput --list-props "SynPS/2 Synaptics TouchPad" |  awk -F ":\t+" "/Device Enabled/ { print (1+\$2)%2}")'

hc keybind XF86Display spawn systemd-run --scope --user ~/.config/herbstluftwm/monitors
hc keybind XF86ScreenSaver spawn systemd-run --scope --user xscreensaver-command -lock
hc keybind Print spawn systemd-run --scope --user sh -c 'maim --hidecursor | xclip -t image/png -selection clip'
hc keybind Shift-Print spawn systemd-run --scope --user sh -c 'maim --select --hidecursor | xclip -t image/png -selection clip'
# shellcheck disable=SC2016
hc keybind Ctrl-Print spawn systemd-run --scope --user sh -c 'maim -i $(xdotool getactivewindow) | xclip -t image/png -selection clip'
hc keybind Ctrl-Print spawn systemd-run --scope --user "$HOME/bin/recordcurrwin"

# theme
hc attr theme.tiling.reset 1
hc attr theme.floating.reset 1
hc set frame_border_active_color "${colors[active]}"
hc set frame_bg_active_color "${colors[active]}"
hc set frame_bg_normal_color "${colors[dark3]}"
hc set frame_border_normal_color "${colors[dark3]}"
hc set frame_border_width 2
hc set always_show_frame on
hc set frame_bg_transparent on
hc set frame_transparent_width 5
hc set frame_gap 0
hc set frame_normal_opacity 100
hc set frame_active_opacity 50

hc attr theme.title_height 15
hc attr theme.title_when multiple_tabs
hc attr theme.title_font 'Dejavu Sans:pixelsize=12'  # example using Xft
# hc attr theme.title_font '-*-fixed-medium-r-*-*-13-*-*-*-*-*-*-*'
hc attr theme.title_depth 3  # space below the title's baseline
hc attr theme.active.color "${colors[active]}"
hc attr theme.title_color "${colors[light0_hard]}"
hc attr theme.normal.color "${colors[dark3]}"
hc attr theme.urgent.color "${colors[urgent]}"
hc attr theme.tab_color "${colors[dark0_hard]}"
hc attr theme.active.tab_color "${colors[dark3]}"
hc attr theme.active.tab_outer_color "${colors[dark3]}"
hc attr theme.active.tab_title_color "${colors[light0]}"
hc attr theme.normal.title_color "${colors[light3]}"
hc attr theme.inner_width 0
hc attr theme.inner_color "${colors[dark0_hard]}"
hc attr theme.border_width 3
hc attr theme.floating.border_width 4
hc attr theme.floating.outer_width 1
hc attr theme.floating.outer_color "${colors[dark0_hard]}"
hc attr theme.active.inner_color "${colors[active]}"
hc attr theme.urgent.inner_color "${colors[urgent]}"
hc attr theme.normal.inner_color "${colors[dark3]}"
# copy inner color to outer_color
for state in active urgent normal ; do
    hc substitute C theme.${state}.inner_color \
        attr theme.${state}.outer_color C
done
hc attr theme.tiling.outer_width 1
hc attr theme.background_color "${colors[dark0_hard]}"

hc set window_gap 0
hc set frame_padding 0
hc set smart_window_surroundings off
hc set smart_frame_surroundings on
hc set mouse_recenter_gap 0

# rules
hc unrule -F
#hc rule class=XTerm tag=3 # move all xterms to tag 3
hc rule focus=on # normally focus new clients
hc rule floatplacement=center
#hc rule focus=off # normally do not focus new clients
# give focus to most common terminals
#hc rule class~'(.*[Rr]xvt.*|.*[Tt]erm|Konsole)' focus=on
hc rule windowtype~'_NET_WM_WINDOW_TYPE_(DIALOG|UTILITY|SPLASH)' floating=on
hc rule windowtype='_NET_WM_WINDOW_TYPE_DIALOG' focus=on
hc rule windowtype~'_NET_WM_WINDOW_TYPE_(NOTIFICATION|DOCK|DESKTOP)' manage=off
hc rule fixedsize floating=on
hc rule class='pinentry-qt' floating=on
hc rule class='mpv' floating=on

hc set tree_style '╾│ ├└╼─┐'




# do multi monitor setup here, e.g.:
# hc set_monitors 1280x1024+0+0 1280x1024+1280+0
# or simply:
hc detect_monitors

### switch off disconnected
#for mon in "$(xrandr | grep -E 'disconnected [[:digit:]]{3,4}' | awk '{ print $1}')" ; do
	#xrandr --output $mon --off
#done

hc attr settings.auto_detect_monitors 1

# unlock, just to be sure
hc unlock

hc setenv SSH_AUTH_SOCK "$(gpgconf --list-dirs agent-ssh-socket)"
systemctl --user import-environment DISPLAY XAUTHORITY XDG_SESSION_PATH

systemctl restart --no-block --user "hlwm.target"

#monitors=$(hc list_monitors | awk -F":" '{print $1}')
#panel_height=18
#for monitor in $monitors ; do
	#hc pad "$monitor" "$panel_height"
#done
#hc pad "0" "$panel_height"

### set my favourite keymap (and map compose key to <> key i dont use anyway)
# set this via localectl
# setxkbmap -option compose:102 us cz_sk_de

xset s off
xset -dpms

{{if eq .chezmoi.hostname "archdudr"}}
hc attr tags.0.tiling.root.algorithm max

hc rule once 'instance~microsoft teams' tag=1
hc rule once 'class=thunderbird' tag=1
hc rule once 'class=discord' tag=1
hc rule once 'class=qutebrowser' tag=2

hc spawn systemd-run --scope --user teams
hc spawn systemd-run --scope --user thunderbird
hc spawn systemd-run --scope --user discord
hc spawn systemd-run --scope --user qutebrowser
{{end}}

### set wallpaper
#feh --bg-center  ${HOME}/config/wallpaper/RoyalRadish-Wallpaper_practice_12.png
