#! /usr/bin/gawk -f
# Last edited on 1998-12-06 05:53:59 by stolfi

BEGIN {
  usage = ( "sync-clip-evt [-v pageSize=NUM] < FILE.evt > FILE.evt" );
  
  # Reads an EVT file, discards #-comments, {}-comments and fillers,
  # truncates each line to 29 characters, and prints the line.
  # Inserts extra blank lines between paragraphs, and also 
  # at the end of each page, so that every page ends up with the 
  # same number NUM of lines.

  abort = -1;
  if (pageSize == "") { arg_error("must define \"pageSize\""); }
  fnum = ""
}

//{ if (abort >= 0) { exit abort; } }

/^(|[#][#]) *<[^;]*> *(|{[^{}]*}) *$/{
  pgun = substr($0, index($0, "<"));
  pgun = substr(pgun, 1, index(pgun, ">"));
  if (! match(pgun, /[.]/)) 
    {
      if ((fnum != "") && (nlines > 0))
        { 
          for( ; nlines < pageSize; nlines++) { printf "%-29s\n", ""; }
          if (nlines > pageSize) 
            { printf "warning: page %s has %d lines\n", \
                fnum, nlines > "/dev/stderr";
            }
        }
      nlines = 0; 
      fnum = pgun;
    }
  next;
}

/^[#][#]/{
  format_error("bad ## line");
}

/^[#]/{
  next;
}

/^[<]/{
  if (nlines == 0) { 
    printf "%-29.29s\n", ("## " fnum);
  }
  gsub(/{[^{}]*}/, "", $0);
  gsub(/[%!]/, "", $0);
  printf "%-29.29s\n", $0;
  nlines++;
  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;
}