#! /bin/sh

compile1=true
compile2=true
case "$1" in
-E)	compile1=false; shift;;
-S|-)	compile2=false; shift;;
esac

charmap=`basename $1 .map`

name=`echo $charmap | sed -e "s,\..*,," -e "s,-,_,g" -e "s,^cjk_,,"`

case $name in
gb*)	split=splitgb;;
*)	split=split;;
esac

CC=${CC-cc}

(
cat <</eoc
#include <stdio.h>
#include "charmaps.h"
struct {unsigned int cjk; unsigned int unicode;} raw_table [] = {
/eoc

# include mapping table rather than #include statement;
# needed for cygwin where compiler would otherwise fail on 
# some of the files with weird messages
cat charmaps/$charmap.map

cat <</eoc
};

void
split (cjk, unicode)
	unsigned int cjk;
	unsigned int unicode;
{
	printf ("	{0x%02X, 0x%02X, 0x%04X, 0x%04X},\n",
		(cjk >> 16) & 0xFF, unicode >> 16, 
		cjk & 0xFFFF, unicode & 0xFFFF);
}

void
splitgb (cjk, unicode)
	unsigned int cjk;
	unsigned int unicode;
{
    if (cjk < 0x10000) {
	printf ("	{0x%02X, 0x%02X, 0x%04X, 0x%04X},\n",
		0xFF, unicode >> 16, cjk, unicode & 0xFFFF);
    } else {
	printf ("	{0x%02X, 0x%02X, 0x%04X, 0x%04X},\n",
		((cjk >> 12) & 0xF0) | (cjk & 0x0F), (unicode) >> 16, 
		(cjk & 0xFF00) | (cjk >> 24), (unicode) & 0xFFFF);
    }
}

int
main () {
	unsigned int i;
	for (i = 0; i < arrlen (raw_table); i ++) {
		$split (raw_table [i].cjk, raw_table [i].unicode);
	}
}
/eoc
) > charmaps/$charmap-gen.c

if $compile1 && $CC -I. -o charmaps/$charmap-gen.exe charmaps/$charmap-gen.c
then	(
	echo '#include "charmaps.h"'
	echo "struct cjk_table_entry ${name}_table [] = {"

	# make generated program executable;
	# needed for cygwin if compiling on remote nfs file system
	# - with Hummingbird nfs, this only works if Hummingbird chmod 
	# - is being invoked (by PATH arrangement or shell script)
	chmod +x charmaps/$charmap-gen.exe
	# use generated program to generate table;
	charmaps/$charmap-gen.exe > charmaps/$charmap.cc
	# filter out junk interspersed to stdout by EMX compiler
	sed -e "/^Warning/ d" charmaps/$charmap.cc
	rm -f charmaps/$charmap.cc

	echo "};"
	echo "unsigned int ${name}_table_len = arrlen (${name}_table);"
	) > charmaps/$charmap.c

	if $compile2 && $CC -I. -c charmaps/$charmap.c -o charmaps/$charmap.o
	then	rm -f charmaps/$charmap-gen.* charmaps/$charmap.c*
	fi
fi
