#!/bin/sh

spec='https://unicode.org/Public/emoji/14.0/emoji-test.txt'
cache=$HOME/.cache/emoji.txt

sync() {
	curl -sL $spec \
		| sed '/^#/d;/^$/d' \
		| grep fully-qualified \
		| sed 's/.*#\s*//;s/\s*E[0-9.]\+\s*/\t/;/:/d' \
		> $cache
}

[ -f $cache ] || sync

if tty -s; then
	pick < $cache | cut -f1
elif [ -n "$WAYLAND_DISPLAY" ]; then
	sed 's/\t/    /' $cache \
		| tofi --prompt-text "emoji" \
		| sed -r 's/\s+.*$//' | tr -d '\n' | wl-copy -p
else
	cat $cache
fi
