#! /bin/sh
# Copyright(c) 2018 Thomas Wolff <towo@towo.net>

# generate list of wide characters, with convex closure

skipcheck=false

if [ ! -r EastAsianWidth.txt ]
then	ln -s /usr/share/unicode/ucd/EastAsianWidth.txt . || exit 1
fi
if [ ! -r UnicodeData.txt ]
then	ln -s /usr/share/unicode/ucd/UnicodeData.txt . || exit 1
fi
if [ ! -r Blocks.txt ]
then	ln -s /usr/share/unicode/ucd/Blocks.txt . || exit 1
fi

sed -e "s,^\([^;]*\);[NAH],\1," -e t -e d EastAsianWidth.txt > wide.na
sed -e "s,^\([^;]*\);[WF],\1," -e t -e d EastAsianWidth.txt > wide.fw

PATH="$PATH:." # for uniset

nrfw=`uniset +wide.fw nr | sed -e 's,.*:,,'`
echo FW $nrfw
nrna=`uniset +wide.na nr | sed -e 's,.*:,,'`
echo NAH $nrna

extrablocks="2E80-303E"

# check all blocks
includes () {
	nr=`uniset +wide.$2 -$1 nr | sed -e 's,.*:,,'`
	test $nr != $3
}
echo "adding compact closure of wide ranges, this may take ~10min"
for b in $extrablocks `sed -e 's,^\([0-9A-F]*\)\.\.\([0-9A-F]*\).*,\1-\2,' -e t -e d Blocks.txt`
do	range=$b
	echo checking $range $* >&2
	if includes $range fw $nrfw && ! includes $range na $nrna
	then	echo $range
	fi
done > wide.blocks

(
sed -e "s,^,//," -e 1q EastAsianWidth.txt
sed -e "s,^,//," -e 1q Blocks.txt
uniset `sed -e 's,^,+,' wide.blocks` +wide.fw c
) > wide.t

rm -f wide.na wide.fw wide.blocks
