#!/bin/sh
# xsel as clipmenud sees it, written by the mesh (module clipmenu). Replaced at every push.
#
# clipmenud records text, and reads a selection with `timeout 1 xsel -o`. A selection holding an
# image (a screenshot copied as image/png) is sent in pieces; one second is too short for megabytes,
# timeout kills xsel half way, and the program owning the image waits for ever for a reader that is
# gone. From then on nothing can ask the clipboard anything: pastes hang, and an Electron app that
# asks on its main thread freezes. So a read goes ahead only when the selection offers text.
case " $* " in
*" -o "* | *" --output "*)
	selection=clipboard
	case " $* " in
	*" --primary "* | *" -p "*) selection=primary ;;
	*" --secondary "* | *" -s "*) selection=secondary ;;
	esac
	targets=$(timeout 1 xclip -selection "$selection" -t TARGETS -o 2>/dev/null) || exit 1
	printf '%s\n' "$targets" | grep -qxE 'UTF8_STRING|STRING|TEXT|text/plain(;charset=utf-8)?' || exit 1
	;;
esac
exec /usr/bin/xsel "$@"
