#!/bin/bash
# Window splitting script for bspwm. Requires wmutils and xdotool. Splits window in its biggest dimension
# in the direction that is closer to the cursor. Inspired by MouseLaunch and pseudo_automatic_rule.
# Get the necessary info

eval $(xdotool getmouselocation --shell)
IFS=" " read -a window <<< $(wattr whxy $(bspc query -N -n focused))

# Distance to each side
north=$(( $Y - ${window[3]}))
south=$(( ${window[3]} + ${window[1]} - $Y ))
west=$(( $X - ${window[2]}))
east=$(( ${window[2]} + ${window[0]} - $X ))
# Dimensions of window
widht=${window[0]}
height=${window[1]}

# Determine which window edge in biggest dimension is closer
  if [[ "$widht" -lt "$height" ]] && [[ "$north" -lt "$south" ]]; then
    dir_final=north
  elif [[ "$widht" -lt "$height" ]] && [[ "$north" -ge "$south" ]]; then
    dir_final=south
  elif [[ "$widht" -ge "$height" ]] && [[ "$east" -ge "$west" ]]; then
    dir_final=west  
  else
    dir_final=east
  fi
  
# Split the window accordingly
bspc node -p \~$dir_final
#echo "north=$north east=$east south=$south west=$west widht=$widht Height=$height $dir_final"
