#!/bin/bash

# this is a simple config for herbstluftwm

hc() {
    cmds="$cmds , $@"
}

hc emit_hook reload

# Scripts directory
HERB=~/.config/herbstluftwm

#Set wallpaper/background
xsetroot -solid '#444444'

# autostart daemons'n'stuff
# spacefm -d &
leafpad $HERB/help.txt &
xrdb merge ~/.Xresources

# remove all existing keybindings
hc keyunbind --all

# keybindings
Mod=Mod4
hc keybind $Mod-Shift-q quit
hc keybind $Mod-Shift-r reload
hc keybind $Mod-Shift-c close
hc keybind $Mod-Shift-b spawn sudo persist-config --shutdown --command reboot

# Open apps
hc keybind $Mod-Return spawn urxvt
hc keybind $Mod-d spawn dmenu_run 

# screenshots
hc keybind $Mod-F12 spawn scrot -t 20 -d 2 ~/%Y-%m-%d-%T-screenshot.png

# Media volume
hc keybind $Mod-Shift-Up spawn amixer sset Master 3.00dB+
hc keybind $Mod-Shift-Down spawn amixer sset Master 3.00dB-

# tags
TAG_NAMES=( {1..6} )
TAG_KEYS=( {1..6} 0 )

hc rename default "${TAG_NAMES[0]}" || true
for i in ${!TAG_NAMES[@]} ; do
    hc add "${TAG_NAMES[$i]}"
    key="${TAG_KEYS[$i]}"
    if ! [ -z "$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
hc keybind $Mod-Right use_index +1 --skip-visible
hc keybind $Mod-Left  use_index -1 --skip-visible

# layouting
hc keybind $Mod-r remove
hc keybind $Mod-space cycle_layout 1
hc keybind $Mod-u split vertical 0.5
hc keybind $Mod-Shift-u split vertical 0.3
hc keybind $Mod-o split horizontal 0.5
hc keybind $Mod-Shift-o split horizontal 0.3
hc keybind $Mod-s floating toggle
hc keybind $Mod-f fullscreen toggle
hc keybind $Mod-p pseudotile toggle

# load layout
hc load 4 "(split horizontal:0.500000:1 (clients vertical:2 0xc00009 0x2200009 0x1a00009) (clients vertical:0 0x2a00009))"

# resizing
RESIZESTEP=0.05
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

# mouse
hc mouseunbind --all
hc mousebind $Mod-Button1 move
hc mousebind $Mod-Button2 resize
hc mousebind $Mod-Button3 zoom

# 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-c cycle
hc keybind $Mod-h focus left
hc keybind $Mod-j focus down
hc keybind $Mod-k focus up
hc keybind $Mod-l focus right
hc keybind $Mod-i jumpto urgent
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

# colors
hc set frame_border_active_color '#222222'
hc set frame_border_normal_color '#101010'
hc set frame_bg_normal_color '#565656'
hc set frame_bg_active_color '#444444'
hc set frame_border_width 0
hc set window_border_width 0
hc set window_border_inner_width 0
hc set window_border_normal_color '#454545'
hc set window_border_active_color '#8c8c8c'
hc set always_show_frame 0
hc set frame_gap 0
# add overlapping window borders
hc set window_gap -0
hc set frame_padding 0
hc set smart_window_surroundings 0
hc set smart_frame_surroundings 0
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=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)' pseudotile=on
hc rule windowtype='_NET_WM_WINDOW_TYPE_DIALOG' focus=on
hc rule windowtype~'_NET_WM_WINDOW_TYPE_(NOTIFICATION|DOCK)' manage=off

# unlock, just to be sure
hc unlock

herbstclient set tree_style '╾│ ├└╼─┐'
hc set frame_bg_transparent 1

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

herbstclient chain $cmds&

# find the panel
panel=~/.config/herbstluftwm/panel.sh
[ -x "$panel" ] || panel=/etc/xdg/herbstluftwm/panel.sh
for monitor in $(herbstclient list_monitors | cut -d: -f1) ; do
    # start it on each monitor
    $panel $monitor &
done
