#! /usr/local/bin/perl
# Convert para breaks from tabs to \n\n:
# Usage: convert-paras [-skipto stuff] [-n] [file...] > outfile
# Also strips ^M from end of lines

$para = '\t'; # default paragraph indication
$stuff = "";  # default no skipto
$toc = '\.\s*' x 4 . '\.'; # table of contents pattern

# Check for -skipto:
if ($ARGV[0] eq "-skipto") {
  shift;
  $stuff = shift;
}

if ($ARGV[0] =~ /^-(\d+)$/) { 
  $para = " " x $1; 
  shift; 
} 

if ($stuff) {
  while (<>) {
    tr/\015\032//d;
    print;
    last if (/^$stuff/);
  }
  die "End of header not found!\n" if (eof);
}

# Convert paragraphs and strip ^Ms
while (<>) {
  tr/\015\032//d;
  s/^$para(\S)/\n$1/ unless (/$toc/);
  print;
}

