#! /usr/bin/gawk -f
# Last edited on 1999-07-21 16:24:02 by stolfi

# Reads a list of records of the form FNUM BNUM where FNUM
# is a page f-number (e.g. f85v2) and BNUM the corresponding
# bifolio b-number (e.g. bM5).
#
# Assigns to each *folio* a distinct letter. The first half
# of each bifolio is assigned a capital letter in sequence, A-Z.
# The second half (if it shows up) gets assigned the corresponding
# lowercase letter.  Assumes all pages of a folio occur consecutively
# in the input.  Outputs a table FNUM FLET that maps each *page* to 
# the corresponding *folio* letter.

BEGIN{ 
  # Letters to be assigned in sequence
  s="AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz";
  
  # "bs[b]" is the index in "s" of the capital letter
  # assigned to bifolio "b".                         
  split("",bs);
  of = "";
  n = -1;
}

/^[#]/{next;}
/^ *$/{next;}
/./{ 
  f = $1; b = $2; 
  gsub(/[rv][0-9]*$/, "", f);
  if(b in bs)
    { if (f != of) { bs[b]++; } i = bs[b]; }
  else
    { n += 2; bs[b] = n; i = n; }
  print $1, substr(s,i,1);
  of = f;
}