%%STARTSHELL%%
# 2.1 90/07/18
if [ $# != 2 ]
then
    echo "Should be mkenctab <in> <out>"
    exit 1
fi
awk '
    {
	if ($2 == "N") Ntab[$1] = $3
	else if ($2 == "S") Stab[$1] = $3
    }
END {
    printf ("#include \"defs.h\"\n");
    printf ("struct enctab encNormal[] = {\n");
    for (i in Ntab) {
	id = ""
	for (k = 1; k <= length(i); k++) {
	    c = substr(i, k, 1)
	    if (c == "\\" && k >= length(i)-1)
		c = "\\\\"
	    else if (c == "\"")
		c = "\\\""
	    id = id c;
	}
	v = Ntab[i]
	nv = ""
	for (k = 1; k <= length(v); k++) {
	    c = substr(v, k, 1)
	    if (c == "\\" && k == length(v))
		c = "\\\\"
	    else if (c == "\"")
		c = "\\\""
	    nv = nv c;
	}
	#if (id == "hy")
	#    printf("\t{\"%s\", \"%s\"},\n", "-", nv);
	if (id == "ul")
	    printf("\t{\"%s\", \"%s\"},\n", "_", nv);
	printf("\t{\"%s\", \"%s\"},\n", id, nv);
    }
    printf("\t{NULL}\n};\n");
    printf ("struct enctab encSymbol[] = {\n");
    for (i in Stab) {
	id = ""
	for (k = 1; k <= length(i); k++) {
	    c = substr(i, k, 1)
	    if (c == "\\" && k >= length(i)-1)
		c = "\\\\"
	    else if (c == "\"")
		c = "\\\""
	    id = id c;
	}
	v = Stab[i]
	nv = ""
	for (k = 1; k <= length(v); k++) {
	    c = substr(v, k, 1)
	    if (c == "\\" && k == length(v))
		c = "\\\\"
	    else if (c == "\"")
		c = "\\\""
	    nv = nv c;
	}
	if (id == "ul")
	    printf("\t{\"%s\", \"%s\"},\n", "_", nv);
	printf("\t{\"%s\", \"%s\"},\n", id, nv);
    }
    printf("\t{NULL}\n};\n");
}' $1 > T2
if [ $? = 0 -a -s T2 ]
then
    mv T2 $2
    exit 0
else
    exit 1
fi
