#!/bin/sh

# Variables
term="foot"
term_client="footclient"
term_float="foot --app-id=float -w 1120x700"
browser="librewolf"
email="thunderbird"
emacs="emacsclient --create-frame --alternate-editor='emacs'"
mod=Super
books="$HOME/.local/bin/books.sh"
papers="$HOME/.local/bin/papers.sh"
config="$HOME/.config/river"

wobsock=$XDG_RUNTIME_DIR/wob.sock
rm -f $wobsock && mkfifo $wobsock && tail -f $wobsock | wob &

# Launch things
riverctl map normal $mod Return spawn $term_client
riverctl map normal $mod+Shift Return spawn $term
riverctl map normal $mod R span "bemenu-run -p launch"
riverctl map normal $mod Semicolon spawn "bemenu-run -p launch"
riverctl map normal $mod+Shift P spawn "$config/power_mode.sh"
riverctl map normal $mod BracketRight spawn "$browser"
riverctl map normal $mod T spawn "$email"
riverctl map normal $mod Z spawn "$books"
riverctl map normal $mod Y spawn "$papers"
riverctl map normal $mod E spawn "$emacs"
riverctl map normal $mod+ctrl O spawn "swaylock"
riverctl map normal $mod B spawn "$config/bluetooth.sh --connect"
riverctl map normal $mod+Shift B spawn "$config/bluetooth.sh --disconnect"
riverctl map normal $mod+ctrl S spawn "$config/screenshot.sh clip"
riverctl map normal $mod+Shift E spawn "$config/screenshot.sh menu"

# Close windows & exit wm
riverctl map normal $mod+Shift C close
riverctl map normal $mod+Shift Q exit

# view stuff
riverctl map normal $mod J focus-view next
riverctl map normal $mod K focus-view previous
riverctl map normal $mod+Shift J swap next
riverctl map normal $mod+Shift K swap previous

riverctl map normal $mod+Shift Space zoom

# monitor stuff
riverctl map normal $mod Period focus-output next
riverctl map normal $mod Comma focus-output previous
riverctl map normal $mod+Shift Period send-to-output next
riverctl map normal $mod+Shift Comma send-to-output previous

# mess with main
riverctl map normal $mod H send-layout-cmd rivertile "main-ratio -0.05"
riverctl map normal $mod L send-layout-cmd rivertile "main-ratio +0.05"

riverctl map normal $mod+Shift H send-layout-cmd rivertile "main-count +1"
riverctl map normal $mod+Shift L send-layout-cmd rivertile "main-count -1"

# manipulate views
riverctl map normal $mod+Alt H move left 100
riverctl map normal $mod+Alt J move down 100
riverctl map normal $mod+Alt K move up 100
riverctl map normal $mod+Alt L move right 100

riverctl map normal $mod+Alt+ctrl H snap left
riverctl map normal $mod+Alt+ctrl J snap down
riverctl map normal $mod+Alt+ctrl K snap up
riverctl map normal $mod+Alt+ctrl L snap right

riverctl map normal $mod+Alt+Shift H resize horizontal -100
riverctl map normal $mod+Alt+Shift J resize vertical 100
riverctl map normal $mod+Alt+Shift K resize vertical -100
riverctl map normal $mod+Alt+Shift L resize horizontal 100

riverctl map-pointer normal $mod BTN_LEFT move-view
riverctl map-pointer normal $mod BTN_RIGHT resize-view
riverctl map-pointer normal $mod BTN_MIDDLE toggle-float

# tag stuff
for i in $(seq 1 9)
do
    tags=$((1 << ($i - 1)))

    riverctl map normal $mod $i set-focused-tags $tags
    riverctl map normal $mod+Shift $i set-view-tags $tags
    riverctl map normal $mod+ctrl $i toggle-focused-tags $tags
    riverctl map normal $mod+Shift+ctrl $i toggle-view-tags $tags
done

all_tags=$(((1 << 32) - 1))
riverctl map normal $mod 0 set-focused-tags $all_tags
riverctl map normal $mod+Shift 0 set-view-tags $all_tags

# toggle
riverctl map normal $mod Space toggle-float
riverctl map normal $mod F toggle-fullscreen

# change layout orientation
riverctl map normal $mod+ctrl K send-layout-cmd rivertile "main-location top"
riverctl map normal $mod+ctrl L send-layout-cmd rivertile "main-location right"
riverctl map normal $mod+ctrl J send-layout-cmd rivertile "main-location bottom"
riverctl map normal $mod+ctrl H send-layout-cmd rivertile "main-location left"

# passthrough mode
riverctl declare-mode passthrough
riverctl map normal $mod F11 enter-mode passthrough
riverctl map passthrough $mod F11 enter-mode normal

# media key bindings
for mode in normal locked
do
    riverctl map $mode None XF86Eject spawn 'eject -T'

    riverctl map $mode None XF86AudioRaiseVolume spawn "$config/volume.sh up 0.02"
    riverctl map $mode None XF86AudioLowerVolume spawn "$config/volume.sh down 0.02"
    riverctl map $mode Shift XF86AudioRaiseVolume spawn "$config/volume.sh up 0.01"
    riverctl map $mode Shift XF86AudioLowerVolume spawn "$config/volume.sh down 0.01"
    riverctl map $mode ctrl XF86AudioRaiseVolume spawn "$config/volume.sh up 0.10"
    riverctl map $mode ctrl XF86AudioLowerVolume spawn "$config/volume.sh down 0.10"
    riverctl map $mode None XF86AudioMute exec spawn "wpctl set-mute @DEFAULT_AUDIO_SINK@ toggle"

    riverctl map $mode None XF86AudioMedia spawn 'playerctl play-pause'
    riverctl map $mode None XF86AudioPlay  spawn 'playerctl play-pause'
    riverctl map $mode None XF86AudioPrev  spawn 'playerctl previous'
    riverctl map $mode None XF86AudioNext  spawn 'playerctl next'

    riverctl map $mode None XF86MonBrightnessUp spawn "brightnessctl set +5% | sed -En 's/.*\(([0-9]+)%\).*/\1/p' > $wobsock"
    riverctl map $mode None XF86MonBrightnessDown spawn "brightnessctl set 5%- | sed -En 's/.*\(([0-9]+)%\).*/\1/p' > $wobsock"
done

# "caffeine"
riverctl map normal $mod+ctrl C spawn "$config/toggle_caffeine.sh"

# colors
riverctl background-color 0x1e1e2e
riverctl border-color-focused 0xf5c2e7
riverctl border-color-unfocused 0x586e75
riverctl border-width 2

# options
riverctl set-repeat 50 300
# riverctl keyboard-layout -options "altwin:swap_lalt_lwin" us
# riverctl keyboard-layout -options "altwin:swap_lalt_lwin,grp:alt_space_toggle" -variant colemak_dh, us,us
riverctl keyboard-layout -options "grp:alt_space_toggle" -variant colemak_dh, us,us
# riverctl keyboard-layout -options "altwin:swap_lalt_lwin,grp:alt_space_toggle" -variant ,colemak_dh,dvorak us,us,us
# riverctl keyboard-layout -options "grp:alt_space_toggle" -variant ,dvorak us,us

riverctl focus-follows-cursor normal
riverctl hide-cursor timeout 10000
riverctl hide-cursor when-typing enabled
riverctl xcursor-theme Adwaita

touchpad=$(riverctl list-inputs | grep -i touchpad)
riverctl input $touchpad disable-while-typing enabled
riverctl input $touchpad natural-scroll enabled
riverctl input $touchpad tap enabled

# Make certain views start floating
riverctl float-filter-add app-id float
riverctl float-filter-add title "popup title with spaces"
riverctl float-filter-add app-id blueman-manager

# "Scratchpad"
scratch_tag=$((1 << 20))
riverctl map normal $mod Slash toggle-focused-tags $scratch_tag
riverctl map normal $mod+Shift Slash set-view-tags $scratch_tag
riverctl map normal $mod+ctrl Slash toggle-view-tags $scratch_tag
riverctl map normal $mod S spawn "$term_float"
riverctl rule-add -app-id float float

# SSD (so GTK apps show border)
riverctl rule-add -app-id librewolf ssd
riverctl rule-add -app-id thunderbird ssd
riverctl rule-add -app-id emacs ssd
riverctl rule-add -app-id gnucash ssd
riverctl rule-add -app-id pcmanfm ssd
riverctl rule-add -app-id steam ssd
riverctl rule-add -app-id org.pwmt.zathura ssd
riverctl rule-add -app-id com.github.xournalpp.xournalpp ssd

# spawn stuff
pgrep swaybg || swaybg -i ~/.config/river/wallpaper.jpg &
wlr-randr --output eDP-1 --scale 1.25 &
pgrep waybar || waybar &

# dunst stuff
pgrep dunst || dunst &
riverctl map normal $mod D spawn 'dunstctl close'
riverctl map normal $mod+Shift D spawn 'dunstctl close-all'

# bluetooth
blueman-applet &

# foot server
foot --server & # TODO: figure out systemd service

# emacs daemon
emacs --daemon &

# nextcloud client
nextcloud &

# pianobar
# riverctl map normal $mod+Shift S spawn '~/.config/pianobar/change_station.sh'
# riverctl map normal $mod P spawn 'echo "p" > ~/.config/pianobar/ctl'
# riverctl map normal $mod N spawn 'echo "n" > ~/.config/pianobar/ctl'
# riverctl map normal $mod C spawn '~/.config/pianobar/display_info.sh'
riverctl map normal $mod P spawn 'mpc toggle'
riverctl map normal $mod+Shift S spawn "$config/music_command.sh --playlist"
riverctl map normal $mod+ctrl S spawn "$config/music_command.sh --song"
riverctl map normal $mod N spawn 'mpc next'

# swayidle
pgrep swayidle || swayidle -w \
    timeout 300 "$config/timeout.sh" \
    before-sleep 'swaylock -f -c 000000' &

# gnome-keyring (needed for nextcloud to remember login)
dbus-update-activation-environment --all
gnome-keyring-daemon --start --components=secrets

# start rivertile
riverctl default-layout rivertile
rivertile -view-padding 6 -outer-padding 6 -main-ratio 0.55 &
