#!/usr/bin/env bash

PATH="$PATH:$HOME/.local/bin"

hc() {
  herbstclient "$@"
}

# don't need sticky win on pinebook
hc new_attr string my_sticky
refresh_panel="pgrep -f 'bash.*panel\.sh' | xargs -n1 kill -s USR1"

hc emit_hook reload

if hc silent new_attr bool my_picom_is_running; then
  picom -b
fi

hc lock
hsetroot -solid "#1e1e2e"
feh --bg-fill ~/.config/herbstluftwm/wallpaper.png
xobpipe="$XDG_RUNTIME_DIR/xobpipe"

# theme
hc attr theme.tiling.reset 1
hc attr theme.floating.reset 1
hc set always_show_frame on
hc set frame_bg_transparent off

hc attr theme.active.color '#a6e3a1'
hc attr theme.normal.color '#89b4fa'
hc attr theme.urgent.color '#f38ba8'
hc attr theme.inner_color black
hc attr theme.border_width 1
hc attr theme.floating.border_width 2
hc attr theme.floating.outer_width 1
hc attr theme.floating.outer_color black
hc attr theme.background_color '#1e1e2e'

hc set window_gap 0
hc set frame_padding 0
hc set smart_window_surroundings on
hc set smart_frame_surroundings on
hc set mouse_recenter_gap 0
hc set frame_active_opacity 1
hc set frame_normal_opacity 1
hc set frame_bg_active_color '#0000000000'
hc set snap_distance 0
hc set snap_gap 0
hc unlock

# remove all existing keybindings
hc keyunbind --all
hc set default_frame_layout horizontal
hc set_layout horizontal

# keybindings
Mod=Mod4   # Use the super key as the main modifier

hc keybind $Mod-Shift-q quit
hc keybind $Mod-Control-r reload
hc keybind $Mod-Shift-c close
hc keybind $Mod-Return spawn st
hc keybind $Mod-grave spawn $HOME/skynet/bin/quake-term
hc keybind $Mod-Control-grave spawn $HOME/skynet/bin/xnots-toggle
hc keybind $Mod-Alt-Return spawn ~/skynet/bin/zenvim
hc keybind $Mod-Control-Return spawn firefox
hc keybind Print spawn flameshot gui
hc keybind $Mod-Tab cycle_all +1

hc keybind $Mod-d spawn rofi -show drun -show-icons
hc keybind $Mod-r spawn rofi -show run
hc keybind $Mod-F1 spawn sh -c 'CM_LAUNCHER=rofi clipmenu'
hc keybind $Mod-F2 spawn rofi -show calc -modi calc -no-show-match -no-sort
hc keybind $Mod-F3 spawn sh -c 'clipctl disable; bwmenu; clipctl enable'

hc keybind XF86AudioPlay spawn playerctl play-pause
hc keybind XF86AudioPause spawn playerctl pause-pause
hc keybind XF86AudioRaiseVolume spawn sh -c "pamixer -i 5; pamixer --get-volume > \"$xobpipe\"; $refresh_panel"
hc keybind XF86AudioLowerVolume spawn sh -c "pamixer -d 5; pamixer --get-volume > \"$xobpipe\"; $refresh_panel"
hc keybind XF86AudioMute spawn sh -c "pamixer -t; pamixer --get-volume-human | sed 's/muted/0/' > \"$xobpipe\"; $refresh_panel"
hc keybind XF86Launch7 spawn sh -c "sleep 0.15 && xdotool keydown ctrl key w keyup ctrl"
hc keybind XF86PowerOff spawn battlvl
hc keybind XF86MonBrightnessUp spawn sh -c "light -A 5; light > \"$xobpipe\""
hc keybind XF86MonBrightnessDown spawn sh -c "light -U 5; light > \"$xobpipe\""

# mouse
hc mouseunbind --all
hc set focus_follows_mouse true
hc set focus_follows_monitor_boundaries true
hc mousebind $Mod-Button1 move
hc mousebind $Mod-Button3 resize

# 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

# 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-j spawn ~/skynet/bin/stickyctl stick
hc keybind $Mod-Shift-j spawn ~/skynet/bin/stickyctl unstick

# tags
tag_names=( {1..9} 0 )
tag_keys=( {1..9} 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" chain , lock , use_index "$i" , spawn ~/skynet/bin/stickyctl get-sticky , unlock
        hc keybind "$Mod-Shift-$key" move_index "$i"
    fi
done

# cycle through tags
hc keybind $Mod-Control-Right use_index +1 --skip-visible
hc keybind $Mod-Control-Left use_index -1 --skip-visible
hc keybind XF86Paste use_index +1 --skip_visible
hc keybind XF86Copy use_index -1 --skip_visible

# layouting
hc keybind $Mod-f fullscreen toggle
hc keybind $Mod-Shift-f set_attr clients.focus.floating 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


# 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-Alt-Tab jumpto urgent

# 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=smart
#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 class='nvim-zen' fullscreen=on
hc rule class='mpv' floating=on
hc rule class='sim_arduboy' floating=on
hc rule class='XNots' floating=on

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

# unlock, just to be sure
hc unlock

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

# restart panel
~/.config/herbstluftwm/killpanel.sh
{ exec ~/.config/herbstluftwm/panel.sh; } &

# restart xob
pgrep xob | xargs -n1 kill -9
if [ ! -p "$xobpipe" ]; then
  mkfifo "$xobpipe"
fi
{ tail -f "$xobpipe" | xob; } &
