#! /usr/bin/gawk -f
# Last edited on 1998-11-26 14:59:03 by stolfi

# Inserts location codes in Takeshi's transcription.
# After cleaning up the file (see Note-044.txt),
# the format is
#
#   page header lines: "# page FNUM"
#   comments: "#...."
#   other lines: EVA text, starting col. 1
#

BEGIN {
  abort = -1;
  fnum = "";
}

/^[ ]*$/{
  if (abort >= 0) { exit abort; }
  print "#"; next;
}

/^[#][#]/{
  if (abort >= 0) { exit abort; }
  if (! match($0, /<f[0-9][0-9]*[rv][0-9]*>/))
    { format_error("bad page header line"); }
  fnum = substr($0,RSTART+1,RLENGTH-2);
  print ("## <" fnum "> {}");
  printf "%s\n", fnum >> "/dev/stderr";
  line = 0;
  next;
}

/^[#]/{
  if (abort >= 0) { exit abort; }
  print; next;
}

//{
  if (abort >= 0) { exit abort; }
  line++;
  loc = sprintf("<%s..%02d;H>", fnum, line);
  printf "%-18s %s\n", loc, $0;
  if (match($0, /[=]$/)) { print "#"; }
  next;
}

function format_error(msg)
{
  printf "file %s, line %d: %s\n", FILENAME, FNR, msg >> "/dev/stderr";
  abort =1;
  exit 1;
}

function arg_error(msg)
{
  printf "%s\n", msg >> "/dev/stderr";
  printf "usage: %s\n", usage >> "/dev/stderr";
  abort = 1;
  exit 1;
}