#!/usr/bin/env bash

winid=$(xdotool search --classname QuakeConsole)
mongeom=$(herbstclient attr monitors.focus.geometry)
monw=${mongeom%x*}
monw=$(( monw - 4 ))
curtag=$(herbstclient attr tags.focus.name)

if [ -z "$winid" ]; then
  (
    rules=( floating=on floating_geometry="${monw}x512+1+1" )
    herbstclient rule once pid=$BASHPID maxage=10 "${rules[@]}"
    exec st -c QuakeConsole -n QuakeConsole -t QuakeConsole
  ) &
else
  hcid=$(printf "0x%x" "$winid")
  visible=$(herbstclient attr clients."$hcid".visible)
  wintag=$(herbstclient attr clients."$hcid".tag)
  if [ "$visible" = "false" ] || [ "$wintag" != "$curtag" ]; then
    herbstclient chain , lock , set_attr clients."$hcid".floating_geometry "${monw}x512+1+1" , bring "$winid" , unlock
  else
    herbstclient set_attr clients."$hcid".minimized true
  fi
fi
