#!/usr/bin/perl
## ----------------------------------------------------------------------
## bin/saspconvert2: 
## ----------------------------------------------------------------------
## Copyright (C) 1998 Ardo van Rangelrooij
## Copyright (C) 1996 Ian Jackson
##
## This is free software; see the GNU General Public Licence
## version 2 or later for copying conditions.  There is NO warranty.
## ----------------------------------------------------------------------

## ----------------------------------------------------------------------
sub usage
{
    print STDERR "$_[0]\n\n" if length( $_[0] );
    print STDERR "usage: saspconvert2 <footnotes> [<inputfile> ...]
 <footnotes> may be: 
   - one of the element names \`P' \`CHAPT' \`BOOK' - footnotes are replaced
           with FOOTNOTEREF elements, and FOOTNOTEBODY is delayed until
           a FOOTNOTES element inserted just before the end of the element type
           specified
   - one of those element names, but with a \`+' appended - footnotes are
           delayed as before, but inserted just _after_ the element end
   - \`.' - no processing of footnotes is done
";
    exit( 3 );
}

## ----------------------------------------------------------------------
sub error
{
    my ( $m,$l ) = @_;
    my $emsg = "saspconvert2: ";
    $emsg .= "$srclinefile[$l] " if length( $srclinefile[$l] );
    $emsg .= "(input line $l): $m";
    print( STDERR "$emsg\n" ) || die $!;
    $errors++;
    if ( $errors >= 20 )
    {
        print( STDERR "saspconvert2: too many errors, stopping\n" );
        exit( 2 );
    }
}

## ----------------------------------------------------------------------
sub getid
{
    if ( $attrib{'ID'} =~ m/^CDATA / )
    {
        $userrefid = $';
        $sysrefid = $_[0] . '-' . $userrefid;
        if ( defined( $defpos{$userrefid} ) )
	{
            &error( "indentifier $userrefid is defined more than once", $i );
            &error( "indentifier $userrefid originally defined here",
		    $defpos{$userrefid} ) if length( $defpos{$userrefid} );
            $defpos{$userrefid} = '';
        }
	else
	{
            $defpos{$userrefid} = $i;
        }
    }
    else
    {
        $userrefid = '';
        $sysrefid = $_[0] . $num{'CHAPT'} . $sectionstring;
    }
}

## ----------------------------------------------------------------------
sub initfootnotes
{
    @footnum = ();
    @footdata = ();
    @footstack = ();
    $lastfootnum = 0;
    $currentfootnum = 0;
}

## ----------------------------------------------------------------------
$footnotes = shift( @ARGV );
$footnotes = uc $footnotes;
$footnotesafter = ( $footnotes ne '.+' && $footnotes =~ s/\+$// );
$footnotes eq 'P'
    || $footnotes eq 'CHAPT'
    || $footnotes eq 'BOOK' 
    || $footnotes eq '.' 
    || &usage( "footnotes \`$footnotes' must be \`P', \`CHAPT', \`BOOK' or \`.'" );

## ----------------------------------------------------------------------
@data = <>;
&initfootnotes;
$cifile = '';
$ciline = 0;

## ----------------------------------------------------------------------
for ( $i = 0; $i <= $#data; $i++ )
{
    $_ = $data[$i];
    $footchange = 0;
    if ( m/^L(\d+)$/ )
    {
        $ciline = $1;
        $cilinefile = "L$ciline $cifile\n" if defined( $cifile );
    }
    elsif ( m/^L(\d+) (.*)\n$/ )
    {
        $ciline = $1;
        $cifile = $2;
        $cilinefile = $_;
    }
    $srclinefile[$i] = "$cifile:$ciline" if defined( $ciline );
    if ( m/^A(\S+) (.*)\n$/ )
    {
        $attribcollect{$1} = $2;
    }
    elsif ( m/^\(/ )
    {
        %attrib = %attribcollect;
        undef %attribcollect;
    }
    if ( m/^\(CHAPT$/ )
    {
        $level = 'CHAPT';
        $num{'CHAPT'}++;
        $num{'SECT0'} = 0;
        &getid('ch');
        $chaptsysrefid = $sysrefid;
    }
    elsif ( m/^\(APPENDIX$/ )
    {
        $level = 'CHAPT';
	( $num{'CHAPT'} =~ m/[0-9]$/ ) ? $num{'CHAPT'} = 'A' : $num{'CHAPT'}++;
        $num{'SECT0'} = 0;
        &getid('ap');
        $chaptsysrefid = $sysrefid;
    }
    elsif ( m/^\(FOOTNOTE$/ && $footnotes ne '.' )
    {
        push( @footstack, $currentfootnum );
        $currentfootnum = ++$lastfootnum;
        $data[$i]= ( "ANUMBER CDATA $currentfootnum\n"
		     . "(FOOTNOTEREF\n" );
        $footdata[$currentfootnum] = ( $cilinefile
				       . "ANUMBER CDATA $currentfootnum\n" );
        $footdata[$currentfootnum] .= ( "ACHAPT CDATA $num{'CHAPT'}\n"
					. "ACSRID CDATA $chaptsysrefid\n" )
            if $footnotes eq 'BOOK';
        $footdata[$currentfootnum] .= "(FOOTNOTEBODY\n";
        $footchange = 1;
    }
    elsif ( $currentfootnum && m/^\)FOOTNOTE$/ )
    {
        $footdata[$currentfootnum] .= ")FOOTNOTEBODY\n";
        $currentfootnum = pop( @footstack );
        $data[$i] = ")FOOTNOTEREF\n" . $cilinefile;
        $footchange = -1;
    }
    if ( $addtofootnum = $footchange > 0 
	 ? $footstack[$#footstack] : $currentfootnum )
    {
        $footdata[$addtofootnum] .= $data[$i];
        $data[$i] = '';
    }
    elsif ( $lastfootnum && m/\)(\w+)$/ && $1 eq $footnotes )
    {
        $footdata = "(FOOTNOTES\n";
        for ( $j = 1; $j <= $lastfootnum; $j++ )
	{
	    $footdata .= $footdata[$j];
	}
        $footdata .= ")FOOTNOTES\n" . $cilinefile;
        if ( $footnotesafter )
	{
	    $append[$i] .= $footdata;
	}
        else
	{
	    $insert[$i] .= $footdata;
	}
        &initfootnotes;
    }
}

## ----------------------------------------------------------------------
if ( $lastfootnum )
{
    die "leftover footnotes $lastfootnum";
}

## ----------------------------------------------------------------------
for ( $i = 0; $i <= $#data; $i++ )
{
    print( $insert[$i], $data[$i], $append[$i] ) || die $!;
}

## ----------------------------------------------------------------------
close( STDOUT ) || die $!;

## ----------------------------------------------------------------------
exit( $errors ? 1 : 0 );

## ----------------------------------------------------------------------
