#!/usr/bin/env bash

running=$(pgrep xnots)
curtag=$(herbstclient attr tags.focus.name)

if [ -z "$running" ]; then
  exec xnots
else
  visible=
  for winid in $(xdotool search --class XNots); do
    hcid=$(printf "0x%x" "$winid")
    if [ -z "$visible" ]; then
      visible=$(herbstclient attr clients."$hcid".visible)
    fi
    wintag=$(herbstclient attr clients."$hcid".tag)
    if [ "$visible" = false ] || [ "$wintag" != "$curtag" ]; then
      herbstclient bring "$winid"
    else
      herbstclient set_attr clients."$hcid".minimized true
    fi
  done
fi
