#!/bin/bash
hc () {
    herbstclient "$@"
}

hc pad 0 14 0 0 0

###################
### Colors      ###
###################

hc set frame_border_active_color '#808080'
hc set frame_border_normal_color '#151515'
hc set frame_bg_transparent 1
hc set frame_border_width 0
hc set window_border_width 2
hc set window_border_normal_color '#151515'
hc set window_border_active_color '#808080'
hc set snap_distance 2
hc set snap_gap 2
hc set window_gap 4
hc set default_frame_layout 2
herbstclient set tree_style '╾│ ├╰╼─╮'

hc emit_hook reload

###################
### Keybindings ###
###################

keyunbind -F

# Modifier variables
s=Shift
c=Control
m=Mod4
a=Mod1

# Layout control
hc keybind $a-r remove
hc keybind $a-space cycle_layout 1
hc keybind $a-v split vertical 0.5
hc keybind $a-h split horizontal 0.5
hc keybind $a-f floating toggle
hc keybind $a-m fullscreen toggle
hc keybind $a-t pseudotile toggle

# Layout manager
hc keybind $m-j spawn hlwm-layout-manager load
hc keybind $m-$s-j spawn hlwm-layout-manager

# Mouse control
hc mousebind $m-Button1 move
hc mousebind $m-Button2 resize
hc mousebind $m-Button3 zoom

# Focus window
hc keybind $c-Left focus left
hc keybind $c-Down focus down
hc keybind $c-Up focus up
hc keybind $c-Right focus right

# Cycle focus
hc keybind $m-BackSpace cycle_monitor
hc keybind $m-k cycle_all +1
hc keybind $m-$s-k cycle_all -1
hc keybind $m-h cycle

# Move window
hc keybind $c-$a-Left shift left
hc keybind $c-$a-Down shift down
hc keybind $c-$a-Up shift up
hc keybind $c-$a-Right shift right

# Resize window
step=0.05
hc keybind $m-$c-j resize left +$step
hc keybind $m-$c-h resize down +$step
hc keybind $m-$c-l resize up +$step
hc keybind $m-$c-k resize right +$step

# Close window
hc keybind $a-q close

# Quit/Reload
hc keybind $m-$c-r reload
hc keybind $m-$s-q spawn wm-session-manager menu
hc keybind $m-$c-q quit

# Apps
hc keybind $m-Return spawn urxvt
hc keybind $m-t spawn urxvt -e tmux
hc keybind $m-a spawn urxvt -e tmux a
hc keybind $m-n spawn urxvt -e ncmpcpp
hc keybind $m-c spawn chromium
hc keybind $m-p spawn pcmanfm
hc keybind $m-g spawn gvim
hc keybind $m-e spawn emacs
hc keybind $m-x spawn dmenu_run -p '>>>' -fn 'bitocrafull' -nb grey10 -nf white -sb grey10 -sf orange

hc keybind $m-$c-Left spawn mpc prev
hc keybind $m-$c-Down spawn mpc toggle
hc keybind $m-$c-Right spawn mpc next

# Tags
TAG_NAMES=(term web dev dl art)
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 "$m-$key" use "${TAG_NAMES[$i]}"
        hc keybind "$m-$s-$key" move "${TAG_NAMES[$i]}"
    fi
done

####################
### Window Rules ###
####################
hc unrule -F
hc rule focus=on
hc rule class~'(.*[Rr]xvt.*|.*[Tt]erm|Konsole)' tag=term focus=on
hc rule class~'([Mm][Pp]layer)' focus=on
hc rule class~'([Mm][Pp]layer)' pseudotile=on
hc rule class~'log' index=10
hc rule class=Deluge tag=dl
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
hc rule class=Chromium tag=web
hc rule class=Gvim tag=dev
hc rule class=Emacs tag=dev

# GIMP
# ensure there is a gimp tag
hc add gimp
hc load gimp '
(split horizontal:0.850000:0
  (split horizontal:0.200000:1
    (clients vertical:0)
    (clients grid:0))
  (clients vertical:0))
'               # load predefined layout
# center all other gimp windows on gimp tag
hc rule class=Gimp tag=gimp index=01 pseudotile=on
hc rule class=Gimp windowrole~'gimp-(image-window|toolbox|dock)' \
    pseudotile=off
hc rule class=Gimp windowrole=gimp-toolbox focus=off index=00
hc rule class=Gimp windowrole=gimp-dock focus=off index=1

#####################
### Start Panel   ###
#####################
( $HOME/.config/herbstluftwm/panel.sh &)