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

echo Generating Unicode width data for newlib/libc/string/wcwidth.c

cd `dirname $0`
PATH="$PATH":.	# ensure access to uniset tool

#############################################################################
# checks and (with option -u) downloads

case "$1" in
-h)	echo "Usage: $0 [-h|-u|-i]"
	echo "Generate width data tables ambiguous.t, combining.t, wide.t"
	echo "from local Unicode files UnicodeData.txt, Blocks.txt, EastAsianWidth.txt."
	echo ""
	echo "Options:"
	echo "  -u    download files from unicode.org first, download uniset tool"
	echo "  -i    copy files from /usr/share/unicode/ucd first"
	echo "  -h    show this"
	exit
	;;
-u)
	wget () {
		ref=`basename $1`
		ref=`ls "$ref" 2> /dev/null || echo 01-Jan-1970`
		curl -R -O --connect-timeout 55 -z "$ref" "$1"
	}

	echo downloading uniset tool
	wget https://www.cl.cam.ac.uk/~mgk25/download/uniset.tar.gz
	gzip -dc uniset.tar.gz | tar xvf - uniset

	echo downloading data from unicode.org
	for data in UnicodeData.txt Blocks.txt EastAsianWidth.txt
	do	wget http://unicode.org/Public/UNIDATA/$data
	done
	;;
-i)
	echo copying data from /usr/share/unicode/ucd
	for data in UnicodeData.txt Blocks.txt EastAsianWidth.txt
	do	cp /usr/share/unicode/ucd/$data .
	done
	;;
esac

echo checking uniset tool
type uniset || exit 9

echo checking Unicode data files
for data in UnicodeData.txt Blocks.txt EastAsianWidth.txt
do	if [ -r $data ]
	then	true
	else	echo $data not available, skipping table generation
		exit
	fi
done

echo generating from Unicode version `sed -e 's,[^.0-9],,g' -e 1q Blocks.txt`

#############################################################################
# table generation

echo generating combining characters table
uniset +cat=Me +cat=Mn +cat=Cf -00AD +1160-11FF +200B +D7B0-D7C6 +D7CB-D7FB c > combining.t

echo generating ambiguous width characters table
sh ./mkwidthA && uniset +WIDTH-A -cat=Me -cat=Mn -cat=Cf c > ambiguous.t

echo generating wide characters table
sh ./mkwide

#############################################################################
# end
