#!/bin/bash
# check the blockend values against the range in the labels
#
# Copyright © 2016 Ken Moffat

# the blocks should be in the current directory
SCRIPTS=${0%/*}

# number of this block
BLOCK=0
# number of last block in current table - should get overwritten by the table
MAXBLOCK=249

let ERRORS=0

if ! [ -r ${SCRIPTS}/unicode-blocks ]; then
	echo "cannot read ${SCRIPTS}/unicode-blocks, oh dear"
	error
fi
. ${SCRIPTS}/unicode-blocks

while [ $BLOCK -le $MAXBLOCK ]; do
	NAME="${blocknames[$BLOCK]}"
	if [ $BLOCK -eq 0 ]; then
		let PREVENDDEC=0
		let ENDSUB=0
	else
		let ENDSUB=$BLOCK-1
		PREVENDDEC=${blockends[$ENDSUB]}
	fi
	NUMS=$(echo $NAME | sed 's/.*U+\(.*\)/\1/')
	TEXT=$(echo $NAME | cut -d ',' -f 1)
	RANGE=$(echo $NUMS | sed 's/-/../')
	STARTHEX=$(echo $RANGE | cut -d '.' -f1)
	if [ -z "$STARTHEX" ]; then
		echo ERROR in hex range for $NAME
		exit
	fi
	STARTDEC=$(printf "%d" 0x${STARTHEX})
	# show the start of the range as a progress indicator
	# this also identifies if the range in the table is not correctly U+xxxx-xxxx
	# or U+xxxxx-xxxxx
	echo STARTHEX is $STARTHEX
	if [ $STARTDEC -ne $PREVENDDEC ]; then
		echo " ERROR at sub $ENDSUB"
		let DIFFERENCE=$STARTDEC-$PREVENDDEC
		echo "difference is $DIFFERENCE"
		echo "new range $RANGE begins at $STARTDEC not $PREVENDDEC"
		echo "--------------------"
		let ERRORS=$ERRORS+1
	fi
	let BLOCK=$BLOCK+1
done

if [ $ERRORS -eq 0 ]; then
	echo "Ranges seems to be all correct"
else
	echo "There are $ERRORS errors"
fi

