#!/bin/sh

set -e

export NOTIFY_LONG=unicode
export NOTIFY_SHORT="${0##*/}"

setup()
{
	curl 'https://www.unicode.org/Public/UNIDATA/UnicodeData.txt' \
	| sed -E '
		s/;[^;]*//2g
		s/\<(.)([A-Z]*)/\1\L\2/2g
		/^[^;]*;</d
		/Compatibility/d
		/Variation Selector/d
		s/[^;]*/\\u&/
	' >"$DATA"
}

readonly DATA="${XDG_DATA_HOME:-$HOME/.local/share}/unicode-data"
[ -f "$DATA" ] || setup

idx="$(cut -d';' -f2 "$DATA" | osel -i)"
code="$(head -n "$idx" "$DATA" | tail -n1 | cut -d';' -f1)"
rune="$(printf $code)"

if [ -t 2 ]
then
	wl-copy -n "$rune" \
	&& notify 'Rune Copied' "The rune ‘$rune’ was copied to the clipboard"
else
	wtype -- "$rune"
fi