#! /usr/bin/gawk -f
# Last edited on 1999-01-30 21:16:46 by stolfi

# usage: split-pages [-v outdir=DIR] < FILE > PAGELIST 
#
# Splits a multipage EVT-format file into one file per page,
# discarding #-comments. Also writes to stdout a list of the files, in
# the order written

BEGIN {
  ofnum = "";
  ofile = "";
  abort = -1;
  if (outdir == "") { outdir = "."; }
}

(abort >= 0) { exit abort; }

/^#/ { next; }

/./ {
  if (match($0, /^<f[0-9][0-9]*[vr][0-9]*(\.[A-Za-z][A-Za-z0-9]*|)\.[0-9]+[a-z]?;[A-Z]>/)) 
    { tmp = substr($0,2,index($0,">")-2);
      skip = 19;
      # Analyze and regularize location code: 
      gsub(/[.;]/, " ", tmp);
      nf = split(tmp, locf);
      if (nf == 3)
        { fnum = locf[1]; unln = locf[2]; trcd = locf[3]; }
      else if (nf == 4)
        { fnum = locf[1]; unln = (locf[2] "." locf[3]); trcd = locf[4]; }
      else
        { error("bad location format"); }
    }
  else if (substr($0,1,1) == "<") 
    { error("bad location code");
    }
  else 
    { skip = 0;
      fnum = "f0"; 
      unln = ("P." NR);
      trcd = "X";
    }
  
  if (skip >= length($0)) next;
  txt = substr($0,1+skip);
  
  if ( fnum != ofnum )
    { if (ofile != "") { close( ofile ); }
      ofile = ( outdir "/" fnum ".evt" );
      ofnum = fnum;
      printf "%s\n", fnum;
    }

  loc = sprintf ("<%s.%s;%s>", fnum, unln, trcd);
  printf "%-19s%s\n", loc, txt >> ofile;
}

END{
  if (abort >= 0) { exit abort; }
  close( ofile );
}

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