#!/bin/bash

source ~/.config/herbstluftwm/config_vars

hc() {
    herbstclient "$@"
}

# always start frames max (pt. 1)
hc set default_frame_layout 2

hc emit_hook reload

xsetroot -solid '#5A8E3A'

# remove all existing keybindings
hc keyunbind --all

# keybindings

hc keybind Mod4-Shift-r reload
hc keybind Mod4-e close_or_remove

# basic movement
# focusing clients
hc keybind Mod4-h     focus left
hc keybind Mod4-j     focus down
hc keybind Mod4-k     focus up
hc keybind Mod4-l     focus right

# moving clients
hc keybind Mod4-Shift-h     shift left
hc keybind Mod4-Shift-j     shift down
hc keybind Mod4-Shift-k     shift up
hc keybind Mod4-Shift-l     shift right

# cycle through clients
hc keybind Mod4-d cycle  1
hc keybind Mod4-a cycle -1

# splitting frames
# create an empty frame at the specified direction
hc keybind Mod4-s chain .-. lock .-. split bottom 0.5 .-. focus -e d .-. unlock
hc keybind Mod4-v chain .-. lock .-. split right  0.5 .-. focus -e r .-. unlock
# create an empty frame and shift the currently focused client into it
hc keybind Mod4-Control-s chain .-. lock .-. split bottom 0.5 .-. shift down .-. unlock
hc keybind Mod4-Control-v chain .-. lock .-. split right  0.5 .-. shift right .-. unlock
# let the current frame explode into subframes
hc keybind Mod4-Shift-space split explode

# resizing frames
resizestep=0.04
hc keybind Mod4-Control-h       resize left +$resizestep
hc keybind Mod4-Control-j       resize down +$resizestep
hc keybind Mod4-Control-k       resize up +$resizestep
hc keybind Mod4-Control-l       resize right +$resizestep

# tags
tag_names=( {1..5} )
tag_keys=( {1..5} 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 "Mod4-$key" use_index "$i"
        hc keybind "Mod4-Control-$key" move_index "$i"
    fi
done

# always start frames max (pt. 2)
hc set_layout max

# cycle through tags
hc keybind Mod4-n use_index +1 --skip-visible
hc keybind Mod4-p use_index -1 --skip-visible

# jump to last used tag
hc keybind Mod4-Escape use_previous

# layouting
hc keybind Mod4-t pseudotile toggle
hc keybind Mod4-f fullscreen toggle

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

# focus
hc keybind Mod4-BackSpace   cycle_monitor
hc keybind Mod4-i jumpto urgent

################ launchers ################

# tegaki
hc keybind Mod4-r spawn tegaki-recognize
# mate
hc keybind XF86PowerOff spawn mate-session-save --shutdown-dialog
hc keybind Mod1-XF86PowerOff spawn mate-session-save --shutdown-dialog
hc keybind Print        spawn mate-screenshot

# ranger
hc keybind Mod4-Shift-Control-Return spawn urxvt -name "ranger" -e ranger /home/shmibs
hc keybind Mod4-Shift-Control-b      spawn urxvt -name "ranger" -e ranger /home/shmibs/backdrops
hc keybind Mod4-Shift-Control-c      spawn urxvt -name "ranger" -e ranger /home/shmibs/comics
hc keybind Mod4-Shift-Control-d      spawn urxvt -name "ranger" -e ranger /home/shmibs/downloads
hc keybind Mod4-Shift-Control-s      spawn urxvt -name "ranger" -e ranger /home/shmibs/desktop
hc keybind Mod4-Shift-Control-m      spawn urxvt -name "ranger" -e ranger /home/shmibs/music
hc keybind Mod4-Shift-Control-g      spawn urxvt -name "ranger" -e ranger /home/shmibs/games
hc keybind Mod4-Shift-Control-p      spawn urxvt -name "ranger" -e ranger /home/shmibs/projects
hc keybind Mod4-Shift-Control-r      spawn urxvt -name "ranger" -e ranger /home/shmibs/romz
hc keybind Mod4-Shift-Control-i      spawn urxvt -name "ranger" -e ranger /home/shmibs/images
hc keybind Mod4-Shift-Control-t      spawn urxvt -name "ranger" -e ranger /home/shmibs/thcool
hc keybind Mod4-Shift-Control-v      spawn urxvt -name "ranger" -e ranger /home/shmibs/videos

# grab colour to clipboard
hc keybind Mod4-c spawn ~/.config/herbstluftwm/colourgrab.sh

# spawn
hc keybind Mod4-Return  spawn urxvt
hc keybind Mod4-Shift-d spawn ~/games/desura/desura
hc keybind Mod4-Shift-f spawn firefox
hc keybind Mod4-Shift-g spawn gwaei
hc keybind Mod4-Shift-i spawn gimp
hc keybind Mod4-Shift-m spawn mathematica
hc keybind Mod4-Shift-o spawn ~/stuffs/tor/start-tor-browser
hc keybind Mod4-Shift-t spawn transmission-gtk
hc keybind Mod4-Shift-v spawn urxvt -name "vim" -e vim
hc keybind Mod4-Shift-w spawn urxvt -name "weechat" -e weechat
hc keybind Mod4-Shift-p spawn urxvt -e pms

# dmenu
hc keybind Mod4-0 spawn dmenu_run -fn $efont -p "Run:" -h 18 -nb $bg_normal -nf $fg_normal \
	-sb $bg_focus  -sf $fg_focus
hc keybind Mod4-9 spawn ~/.config/herbstluftwm/switcher.sh
hc keybind Mod4-8 spawn ~/.config/herbstluftwm/calc.sh

############## end launchers ##############


# borders
hc set frame_border_normal_color $bg_normal
hc set frame_border_active_color $bg_focus
hc set frame_bg_normal_color $bg_normal
hc set frame_bg_active_color $bg_focus
hc set window_border_normal_color $bg_normal
hc set window_border_active_color $bg_focus
hc set frame_border_width 2
hc set frame_border_inner_width 0
hc set window_border_width 2
hc set window_border_inner_width 0
hc set always_show_frame 0
hc set frame_bg_transparent 1
hc set frame_normal_opacity 100
hc set frame_active_opacity 100
hc set frame_transparent_width 0
hc set frame_gap 0
hc set frame_padding 0
hc set smart_frame_surroundings 1
hc set smart_window_surroundings 1

# rules
hc unrule -F
hc rule focus=on 
# unmanage docks, dropdowns, etc
hc rule windowtype~'_NET_WM_WINDOW_TYPE_(NOTIFICATION|DOCK)' manage=off
# set apppropriate things to pseudotile
hc rule windowtype~'_NET_WM_WINDOW_TYPE_(DIALOG|UTILITY|SPLASH)' pseudotile=on focus=on
hc rule class~'(SleepIsDeathApp|freeciv-sdl|milkytracker|FTL|Tegaki-recognize|PlayOnLinux|Pcsx2|Tilem2|Wine)' pseudotile=on focus=on

# unlock, just to be sure
hc unlock

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

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

# 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

# run dunst with theme colours
dunst -lb $bg_normal -nb $bg_normal -cb $bg_focus \
	-lf $fg_normal -nf $fg_normal -cf $fg_focus \
	-frame_color $bg_focus