#!/bin/sh

# tail -f test-out | awk -v qt='""' '
# $1 ~ "HSuper_L" { checkstart=1 }
# /^BBegin chain/ && checkstart {
	# checkstart=0
	# print "rofi -normal-window -e " qt "sxhkd: Super ;" qt
# }
# /^EEnd chain/ {
	# print "kill " qt "$pid" qt
# }'

checkstart=false
theme='
window {
	anchor: southwest;
	location: southwest;
	x-offset: 4;
	y-offset: -4;
	width: 200;
}

listview {
	border-color: @blue;
}'

display_message() {
	rofi \
		-normal-window \
		-e "sxhkd: $1" \
		-theme-str "$theme" &
	pid=$!
}

close_message() {
	[ -n "$pid" ] && kill "$pid"
	pid=""
}

tail -f test-out | while read -r line; do
	case $line in
		HSuper_L)
			checkstart=true
			;;

		HSuper_L*)
			if [ "$inchain" = "false" ]; then
				continue
			fi
			echo "Extra chain"
			close_message
			display_message "$line"
			;;

		"BBegin chain")
			if [ "$checkstart" = "false" ]; then
				continue
			fi
			echo "In chain"
			checkstart=false
			inchain=true
			display_message "HSuper_L"
			;;

		"EEnd chain")
			if [ "$inchain" = "false" ]; then
				continue
			fi
			echo "End chain"
			close_message
			;;
	esac
done
