#! /bin/sh -f

if make UnicodeData.txt
then	true
else	echo Could not acquire Unicode data file UnicodeData.txt
	exit 1
fi

make_caseconv_subtable () {
	cap=
	low=
	case "$1" in
	-toupper)	cap="[^;]";;
	-tolower)	low="[^;]";;
	esac

	#0061;LATIN SMALL LETTER A;Ll;0;L;;;;;N;;;0041;;0041
	tr -d '\015' < UnicodeData.txt |
	sed -e "s,^\([^;]*\);[^;]*;[^;]*;[^;]*;[^;]*;[^;]*;[^;]*;[^;]*;[^;]*;[^;]*;[^;]*;[^;]*;\($cap[^;]*\);\($low[^;]*\);\([^;]*\)$,src \1 upper '\2' lower '\3' title '\4'," \
	-e t -e d
}

make_caseconv_subtable -tolower > caseconv_tolower
make_caseconv_subtable -toupper > caseconv_toupper
cat caseconv_tolower caseconv_toupper | LC_CTYPE=C sort | uniq > caseconv_list

dec () {
	expr `echo $1 | 
	sed -e "s,\(.\)\(.\)\(.\)\(.\),\1 * 4096 + \2 * 256 + \3 * 16 + \4," \
	-e "s,[Aa],10,g" -e "s,[Bb],11,g" -e "s,[Cc],12,g" \
	-e "s,[Dd],13,g" -e "s,[Ee],14,g" -e "s,[Ff],15,g" \
	-e t -e "s,$,0," `
}

sed	-e "s/src \([^ ]*\) upper '\([^ ]*\)' lower '\([^ ]*\)' title '\([^ ]*\)'/	{0x\1, 0x\2 - 0x\1, 0x\3 - 0x\1},/" \
	-e "s/0x - 0x[^ ,}]*/0/g" \
	caseconv_list > casetabl.h

rm -f caseconv_tolower caseconv_toupper caseconv_list

exit

echo constructing case table - this takes a long while

while read line
do	eval set - $line
#src 0041 upper '' lower '0061' title ''
	base=`dec $2`
	upper=`dec ${4:-$2}`
	lower=`dec ${6:-$2}`
#	title=`dec ${8:-$2}`
	toupper=`expr $upper - $base`
	tolower=`expr $lower - $base`
#	totitle=`expr $title - $base`
#	echo "src $base toupper $toupper tolower $tolower"
	echo "	{$base, $toupper, $tolower},"
done < caseconv_list > casetabl.h
