#!/usr/local/bin/perl -w
# 
# ncrease v0.5, Harry Plantinga, Nov. 8, 1998. This program may be 
# distributed and used under the terms of the Artistic License.
#
# ncrease: in pb and div tags, interpolate n and href attributes: 
# if there is an n attribute for a div or pb and no n attribute for 
# the next one at same level, make it previous value plus one.
#
# Also increment pb href attributes intelligently, so you can add
# an href for a page image to the first two pages and let this program
# figure out what they ought to be for the remaining pages..
#
use strict;

my $input;

while (<>) 
  { $input .= $_; }

  $input = &ncrease($input);	#increment n and href in pb tags
  $input = &divcrease($input);	#increment n in div tags

  print $input;			#wrap fails under dos for lack of memory
  exit(0);
  


#-------------------------ncrease stuff-----------------------



sub divcrease
{
  $_ = shift;
  my $output = "";
  my $oldn = "";
  my $n = "";
  my $div = "";
  my $level = "";
  my $oldlevel = "";
  while (m|^(.*?)(<div.*?>)|s)
    {
    s|^(.*?)(<div.*?>)||s;
    $output .= $1;
    $div = $2;
#   print STDERR "Found $div\n";

    $oldn = $n;
    $n = "";
    $n = $1 if $div =~ m|n="(.*?)"|;
    $oldlevel = $level;
    $level = "";
    $level = $1 if $div =~ m|<div(.)|;

#   print STDERR " oldn=$oldn n=$n   oldlevel=$oldlevel level=$level\n";
    if ($level eq $oldlevel and $oldn and !$n)
    {
      $n = &inc($oldn);
      $div =~ s|(<div.*?)>|$1 n="$n" >| if $n;
    }
    $output .= $div;
  }

  $output .= $_;
  return $output;
}


sub ncrease
{
$_ = shift;
#first determine the number of pages per image, if there are images.
my $col = 0;
my $output = "";
my ($pb, $oldhref, $foundhref, $newn, $oldn);
my $href = "";

# now loop through $_, stopping to process each <pb...> element.
while (m|^(.*?)(<pb.*?/>)|s)
  {
  s|^(.*?)(<pb.*?/>)||s;
  $output .= $1;
  my $pb = $2;
# print STDERR "$pb --> ";

  #
  #this section handles hrefs. 
  #If there is not an href, but there was one previously, inc previous one.
  #If there is an href, remember it, and if it is the same as previous one, 
  # set $col to 2. (2 pages per image; this is second column)
  #
  $oldhref = $href;		
  $foundhref = "";
  $foundhref = $1 if $pb =~ m|href="(.*?)"|;
  if (!$foundhref && $oldhref)
    { 
#   print STDERR "noref oldref";
    ($href, $col) = incpage($oldhref, $col); 
    $pb =~ s|/>| href="$href" />|;
    }
  elsif (length($foundhref)>0)
    {
#   print STDERR "href";
    $href=$foundhref;
    $col=2 if $href eq $oldhref;
    }
# print STDERR " col=$col ";

  #this section increments n, the page number
  my $n = "";
  $n = $1 if $pb =~ m|n="(.*?)"|;
  if ($n)
    { $newn = $n; }
  else
    { $newn = &inc($oldn); }
  $oldn = $newn;
  $pb =~ s|/>| n="$newn" />| unless $pb =~ m|n=|;

  #increment href, the page image

# print STDERR "$pb\n";
  $output .= $pb;
  }
  $output .= $_;
  return $output;
}


#  smart-increment a picture href
#
#  Two sequences are supported: xx001.gif, xx002.gif, etc and xx001a.gif, 
#  xx001b.gif, xx002a.gif, xx002b.gif, etc. In addition, there may be one
#  or two pages per image.
#
#  This subroutine takes a filename and $col=0, 1, or 2.  If 0, there is
#  one page per image.  If 1, there are two pages per image, and this is
#  the first.  If two, there are two pages per image, and this is the
#  second.
#
#  The return values are the new $filename and the new $col value.
#
sub incpage	
{
  my $filename = shift;
  my $col = shift;

  return ($filename, 2) if $col==1; 
				#if there's another page on this pic, use it.

  $col=1 if $col==2;
  if ($filename =~ m|a\.[^\.]+$|)
    { $filename =~ s|a(\.[^\.]+$)|b$1|; }
  else
    {
    $filename =~ s|b(\.[^\.]+$)|a$1|;

    $filename =~ m|([0-9]+)[^0-9]*$|;
    my $oldn = $1;
    my $newn = $1 + 1;
    while ( length($newn) < length($oldn) )
      {$newn = "0" . $newn; }

    $filename =~ s|$oldn|$newn|;
    }

  return ($filename, $col);
}


sub inc				#increment a page number
{
  my $n = shift;

  if ($n =~ m|^[mdclxvi]*$|)
    { $n = &incroman($n); }
  elsif ($n =~ m|^[MDCLXVI]*$|)
    { $n = &incROMAN($n); }
  elsif ($n =~ m|^[0-9]+$|)
    { $n++; }
  else
    { $n = ""; }

  return $n; 
} 


sub incroman			#increment lower-case roman numerals
{
  my $r = shift;

  $r .= "i";
  $r =~ s|iiii|iv|;
  $r =~ s|ivi|v|;
  $r =~ s|viv|ix|;
  $r =~ s|ixi|x|;
  $r =~ s|xxxx|xl|;
  $r =~ s|xlx|l|;
  $r =~ s|lxl|xc|;
  $r =~ s|xcx|c|;
  $r =~ s|cccc|cd|;
  $r =~ s|cdc|d|;
  $r =~ s|dcd|cm|;
  $r =~ s|cmc|m|;

  return $r;
}

sub incROMAN			#increment lower-case roman numerals
{
  my $r = shift;

  $r .= "I";
  $r =~ s|IIII|IV|;
  $r =~ s|IVI|V|;
  $r =~ s|VIV|IX|;
  $r =~ s|IXI|X|;
  $r =~ s|XXXX|XL|;
  $r =~ s|XLX|L|;
  $r =~ s|LXL|XC|;
  $r =~ s|XCX|C|;
  $r =~ s|CCCC|CD|;
  $r =~ s|CDC|D|;
  $r =~ s|DCD|CM|;
  $r =~ s|CMC|M|;

  return $r;
}

