#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define LATIN_SMALL_LETTER_N_WITH_TILDE 0xF1

// pre-process FISE root list to make for easier web display
int main(int argc, char **argv)
{
  int c, lineno = 0;
  static char line[1024], root[1024], tile[1024], *s;
  for (;;) {
    do {
    s = line;
    for (;;) {
      c = fgetc(stdin);
      if (c == EOF) break;
      if (c == '\r') continue;
      if (c == LATIN_SMALL_LETTER_N_WITH_TILDE) c = '4';
      if (c == '\n') {lineno++; break;};
      *s++ = c;
    }
    *s = '\0';
    if (c == EOF) break;
    } while (*line == '\0');
    if (c == EOF) break;
    // should be <TILE> line...
    if (strncmp(line, "<TILE> ", strlen("<TILE> ")) != 0) {
      fprintf(stderr, "Expecting '<TILE>', found '%s' at line %d\n", line, lineno);
      exit(1);
    }
    strcpy(tile, line+strlen("<TILE> "));
    // now expect <MARK> on next line, followed by blank line
    do {
    s = line;
    for (;;) {
      c = fgetc(stdin);
      if (c == EOF) break;
      if (c == '\r') continue;
      if (c == '\n') {lineno++; break;}
      *s++ = c;
    }
    *s = '\0';
    if (c == EOF) break;
    } while (*line == '\0');
    if (strncmp(line, "<MARK> ", strlen("<MARK> ")) != 0) {
      fprintf(stderr, "Expecting '<MARK>', found '%s' at line %d\n", line, lineno);
      exit(1);
    }
    strcpy(root, line+strlen("<MARK> "));
    s = root;
    for (;;) {
      if (*s == '\0') break;
      if (*s == ' ') {
        if (isdigit(s[1])) s += 1; else *s = ';';        
      }
      s += 1;
    }
    fprintf(stdout, "~%s=%s\n", tile, root); /* end of alphabet, so can cat >> prev file */
  }
}
