#!/bin/bash

# this is a simple config to herbstluftwm

pad="17 0 0 0"
if [ "$HOSTNAME" = towi04 ] ; then
    pad="16 0 10 0"
fi

function hc() {
    herbstclient "$@"
}
[[ "$HOSTNAME" == faui* ]] && hc() { ~/bin/herbstclient "$@" ; }

hc emit_hook reload

#xsetroot -solid '#D47A55'
hc set window_gap 8
# raindrops
#feh --bg-center ~/Documents/wallpaper/water/rain.jpg
case "$HOSTNAME" in
    ghul)
        feh --bg-fill ~/Documents/wallpaper/flowers/DSC02801.jpg
        ;;
    towi04)
        nitrogen --restore
        ;;
    *)
        ;;
esac

# keybindings
Mod=Mod4
if [ $HOSTNAME = faui03a ] ; then
    Mod=Mod1
fi
hc keybind $Mod-Shift-r reload
hc keybind $Mod-Shift-c close
hc keybind $Mod-Return spawn urxvt
hc keybind $Mod-x spawn ~/scripts/menu/custommenu.sh apps
hc keybind $Mod-F2 spawn gmrun
hc keybind $Mod-e spawn thunar


# tags
TAG_NAMES=( cip vim video float music web im irc mail xn )
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" use "${TAG_NAMES[$i]}"
        hc keybind "$Mod-Shift-$key" move "${TAG_NAMES[$i]}"
    fi
done

function dualhead() {
    width="$1"
    height="$2"
    # todo: rememberlast tag on monitor 1
    hc focus_monitor 0
    hc remove_monitor 1
    hc move_monitor 0 $((width/2))x$((height))+0+0 $pad
    hc use "${TAG_NAMES[0]}"
    hc add_monitor $((width/2))x$((height))+$((width/2))+0 "${TAG_NAMES[1]}" $pad
}

function singlehead() {
    width="$1"
    height="$2"
    hc focus_monitor 0
    hc remove_monitor 1
    hc move_monitor 0 $((width))x$((height))+0+0 $pad
}


# multimonitors
case "$HOSTNAME" in
    towi04)
        dualhead $((2*1280)) 1024
        ;;
    dachs)
        hc keybind $Mod-Return spawn xterm
        #dualhead 1920 1200
        ;;
    faui*)
        resolution=$(xrandr 2> /dev/null |grep -E '\* $'|awk '{ print $1 ; }')
        case "$resolution" in
            1280x1024)
                singlehead ${resolution/x/ }
                ;;
            1920x1200)
                singlehead ${resolution/x/ }
                #dualhead ${resolution/x/ }
                #dualhead 1920 1200
                ;;
            2560x1024)
                dualhead ${resolution/x/ }
                #dualhead 2560 1024
                ;;
            *)
                # do nothing
                ;;
        esac
        ;;
    *)
        ;;
esac

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

# 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 mousebind $Mod-Button1 move
hc mousebind $Mod-Button2 resize
hc mousebind $Mod-Button3 zoom

# focus
hc keybind $Mod-Tab cycle_monitor
hc keybind Alt-Tab          cycle_all 1
hc keybind Alt-Shift-Tab    cycle_all -1
hc keybind $Mod-c cycle
hc keybind $Mod-y cycle -1
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-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

## other things
if [ "$HOSTNAME" = ghul ] ;then
    hc keybind $Mod-q quit
    # TODO: why are there problems with XGrabKey for XF86ScreenSaver;
    hc keybind XF86ScreenSaver spawn ~/bin/lock.sh
    hc keybind XF86Launch1 spawn ~/bin/suspend.sh --lock
    hc set frame_bg_transparent 1
fi
if [ "$HOSTNAME" = towi04 ] ; then
    hc keybind $Mod-Control-q spawn ~/scripts/logout-dialog.sh
    hc keybind $Mod-Shift-q spawn ~/scripts/logout-dialog.sh
    hc keybind $Mod-Control-Shift-q quit
    hc keybind Redo spawn ~/scripts/mpd/mpc.sh seek 0
    hc keybind Mod4+F1 spawn ~/scripts/utils/lock.sh
    hc keybind $Mod-F3 spawn mygtkmenu ~/.config/mygtkmenu.conf
    hc set frame_bg_transparent 1
fi
if [[ "$HOSTNAME" =~ ^(towi04|ghul|x40|eule)$ ]] ; then
    hc keybind Pause spawn ~/scripts/mpd/mpc.sh toggle
    hc keybind Mod4+q spawn ~/scripts/mpd/dmenu.sh play
    hc keybind XF86Stop spawn ~/scripts/mpd/mpc.sh toggle
    hc keybind Mod4+w spawn ~/scripts/mpd/dmenu.sh totem
    hc keybind Mod4+t spawn ~/scripts/utils/iptv/watch-dmenu.sh
    hc keybind Mod1+Mod4+y spawn ~/scripts/mpd/mpc.sh random
    hc keybind Mod1+Mod4+u spawn ~/scripts/mpd/mpc.sh prev
    hc keybind Mod1+Mod4+i spawn ~/scripts/mpd/mpc.sh toggle
    hc keybind Mod1+Mod4+o spawn ~/scripts/mpd/mpc.sh next
fi

hc keybind Mod4+n spawn ~/scripts/utils/utf8select.sh

hc keybind Shift+XF86AudioMute spawn amixer -q sset PCM toggle
hc keybind XF86AudioMute spawn amixer -q sset Master toggle
hc keybind XF86AudioLowerVolume spawn amixer -q sset Master 2-
hc keybind XF86AudioRaiseVolume spawn amixer -q sset Master 2+

# some settings
hc set ignore_class 'Xfce4-notifyd|Conky'
hc set focus_follows_mouse 1

### COLORS ###
colors_thin_red() {
    hc set frame_border_active_color '#b20000'
    hc set frame_border_normal_color 'black'
    hc set frame_bg_normal_color '#141414'
    hc set frame_bg_active_color '#580000'
    hc set always_show_frame 0
    hc set frame_border_width 1
    hc set frame_bg_transparent 1
    hc set window_border_width 1
    hc set window_border_normal_color '#242424'
    hc set window_border_active_color '#b20000'
}

colors_thick_brown() {
    hc set frame_border_active_color '#49351D'
    hc set frame_border_normal_color '#73532D'
    hc set frame_bg_normal_color '#BD9768'
    hc set frame_bg_active_color '#BD8541'
    hc set frame_border_width 2
    hc set window_border_width 3
    hc set window_border_normal_color '#AE8451'
    hc set window_border_active_color '#F6FF00'
}

colors_pwmt_green() {
    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 '#345F0C'
    hc set frame_border_width 1
    hc set window_border_width 2
    hc set window_border_normal_color '#454545'
    hc set window_border_active_color '#9fbc00'
    hc set always_show_frame 0
}

colors_snow() {
    hc set frame_border_active_color '#00182F'
    hc set window_border_active_color '#1361AD'
    hc set frame_border_width 1
    hc set frame_bg_transparent 0
    hc set frame_bg_active_color '#103462'
    hc set window_border_width 2
    hc set frame_border_normal_color 'black'
    hc set frame_bg_normal_color '#141414'
    hc set window_border_normal_color '#242424'
}


# new colors
case "$HOSTNAME" in
    ghul)   colors_pwmt_green ;;
    x40)    colors_thin_red ;;
    faui0*) colors_snow ;;
    *)      colors_pwmt_green ;;
esac

SCRIPTSDIR=$HOME/dev/c/herbstluftwm/scripts/

if [ -d "$SCRIPTSDIR" ] ; then
    hc keybind $Mod-g spawn $SCRIPTSDIR/execwith.sh \
        ~/.bash_settings $SCRIPTSDIR/dmenu.sh use
    hc keybind $Mod-Shift-g spawn $SCRIPTSDIR/execwith.sh \
        ~/.bash_settings $SCRIPTSDIR/dmenu.sh move
fi

case "$HOSTNAME" in
    faui0*)
        ~/git/herbstluftwm/share/restartpanels.sh ~/.config/herbstluftwm/panel.sh
        hc keybind $Mod-x spawn ~/scripts/menu.sh
        hc keybind $Mod-F1 spawn ~/scripts/lock.sh
        hc keybind $Mod-Shift-q quit
        hc keybind $Mod-Control-q quit
        xset -b
        ;;
    faui4*)
        hc keybind $Mod-x spawn ~/scripts/menu.sh
        ;;
    x40)
        killall cippanel.sh || true
        ~/.config/herbstluftwm/cippanel.sh &
        ;;
    dachs)
        killall cippanel.sh || true
        ~/.config/herbstluftwm/cippanel.sh &
        hc keybind $Mod-x spawn ~/build/dwm-samoht/dmenu_run
        hc keybind $Mod-q quit
        ;;
    faui4*)
        panel=~/.config/herbstluftwm/panel.sh
        ~/build/herbstluftwm/share/restartpanels.sh $panel
        ;;
    *)
        panel=
        [[ "$HOSTNAME" =~ towi04|ghul ]] &&
            panel=~/.config/herbstluftwm/panel.sh
        ~/dev/c/herbstluftwm/share/restartpanels.sh $panel
        hc focus_monitor 1
    ;;
esac

# nice tree styles:
#herbstclient set tree_style '╾│ ├└╼─┐'
herbstclient set tree_style '╾│ ├╰╼─╮'
#herbstclient set tree_style '*| +`>-.'
#herbstclient set tree_style '*| +`-->'

### RULES ###
hc unrule -F
hc rule focus=on
hc rule class=Vlc tag=video index=/ focus=off
hc rule class=Totem tag=video index=/ focus=off
hc rule class=Gmrun pseudotile=on 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

hc load float \
   "(split vertical:0.800000:1
        (clients max:0)
        (split horizontal:0.500000:0
            (clients horizontal:0)
            (clients horizontal:0)))"
hc rule class=Xephyr tag=float pseudotile=on index=0 focus=off
hc rule class=MPlayer tag=float index=0 focus=off
hc rule class='logout-dialog' pseudotile=on focus=on
hc rule instance=irc tag=irc
hc rule instance=finch tag=im
hc rule instance=mutt tag=mail

STATEFILE=~/.config/herbstluftwm/state

# 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

### SCRIPTS ###
c=~/.config/herbstluftwm/
hc keybind $Mod-F10         spawn $c/fastterminal.sh -t
hc keybind $Mod-Shift-F10   spawn $c/fastterminal.sh -b
hc keybind $Mod-F12         spawn $c/toggledualhead.sh
hc keybind $Mod-F9          emit_hook togglehidepanel

[ -f "$STATEFILE" ] && ~/dev/c/herbstluftwm/scripts/loadstate.sh < "$STATEFILE"
rm -f "$STATEFILE"


