#!/bin/sh
scratchpad=/tmp/herbstluftwm:spmpv
if [ -f "$scratchpad" ]; then
  if xdotool search -class 'mpv'; then 
    if [ "$(herbstclient list_monitors | grep '[FOCUS]' | cut -d\" -f2)" = "$(herbstclient attr clients.$(cat $scratchpad) | grep 's - - tag' | awk '{ print $6 }' | sed 's/\"//g')" ]; then 
      xdotool search -class 'mpv' windowunmap 
      exit 
    fi 
  fi
  if ! herbstclient bring $(cat $scratchpad); then
    xdotool search -class 'mpv' windowmap && exit
  fi
  if ! xdotool search -class 'mpv' windowmap; then
    xdotool search -sync -onlyvisible -class 'mpv'
    herbstclient attr clients.focus.winid > $scratchpad
  fi
else
  if ! xdotool search -class 'mpv' windowmap; then
    xdotool search -sync -onlyvisible -class 'mpv'
    herbstclient attr clients.focus.winid > $scratchpad
  fi
fi