#! /usr/bin/env perl
# This script requires that Perl be in the path, and that the
# environment variable SST_DIR be set to the location of the prolat
# binary.

   $C = 1;
   $fortran = 0;
   $prologue = 0;
   $name = "";
   $name_section = 0;
   $key = "";
   $unix_script = 0;
   $cmt = quotemeta( "*" );

#  Read switches.
   while ( $_ = $ARGV[0], /^-/ ) {
      shift;
      last if /^--$/;
      if ( /^-f/ ) { $fortran = 1; $C = 0; };
      if ( /^-u/ ) { $unix_script = 1; $cmt = quotemeta( "#" ); };
      if ( /^-att/ ) { $key = "att" };
      if ( /^-class/ ) { $key = "class" };
   }

#  Set up pattern for matching language-dependent lines.
   $language_symbol = "c";
   if ( $fortran ) { $language_symbol = "f"; }
   if ( $unix_script ) {
      $lang = $cmt . "[fc]";
      $thislang = quotemeta( "#" . $language_symbol );
   } else {
      $lang = "[fc]";
      $thislang = quotemeta( $language_symbol );
   }

#  Read input lines.
   line: while (<>) {

#  Skip language-specific lines that aren't wanted.
      if ( /^$lang/ ) {
          next line if ( !/^$thislang/ );

#  Put "*" in first column.
         s/^$thislang/\*/;
      }

#  Detect end of prologue.
      if ( /^$cmt$key--/o ) {
         $prologue = 0;

#  Append "*-" to prologue text and save the whole prologue as an element
#  in the "pro" associative array.
         $pro{ $name } = $text . "*-\n";

#  Clear the name ready for next prologue.
         $name = "";
      }

#  Process the prologue contents.
      if ( $prologue ) {

#  Remove trailing blanks.
         s/ *$//;

#  Convert/remove sections that don't appear in external documentation.
         if ( $unix_script ) {
            ;
         } elsif ( !$key && $C ) {
            s/^($cmt *)Synopsis:$/$1Invocation:/;
            s/^($cmt *)Parameters:$/$1Arguments:/;
         } elsif ( !$key && $fortran ) {
            s/^($cmt *)Synopsis:$/$1Invocation:/;
            s/^($cmt *)Parameters:$/$1Arguments:/;
         } elsif ( $key =~ /att/ ) {
            s/^($cmt *)Synopsis:$/$1Invocation:/;
         } elsif ( $key =~ /class/ ) {
            s/^($cmt *)Constructor Function:$/$1Invocation:/;
         }

#  Skip the contents of these sections...
         if ( /^$cmt *Type:$/ ||
              /^$cmt *Class Membership:$/ ||
              /^$cmt *Copyright:$/ ) { 
             while ( <> ) { if ( /^ *$/ ) { next line } };
         }

#  Remove any #include directives from the C Synopsis (Invocation) section.
         if ( !$key && $C && !$unix_script ) {
            if ( /^$cmt *Invocation:$/ ) {
                while ( <> && /^$cmt *#include/ ) {};
            }
         }

#  If in the "Name:" section, search for the routine name, noting when
#  found.
         if ( $name_section && ( ( $name ) = /^. *([^ ]*) *$/ ) ) {
            $name_section = 0;
         };

#  Detect the "Name:" line itself.
         if ( /^$cmt *Name:$/ ) { $name_section = 1 };

#  Change to use the standard comment character "*".
         s/^$cmt/\*/;

#  Append each prologue line to the end of the prologue text.
         $text = $text . $_;
      }

#  Detect start of prologue and initialise prologue text.
      if ( /^$cmt$key\+\+/o ) {
         $prologue = 1;
         $name_section = 0;
         $text = "*+\n";
      };
   }

#  Output prologues in alphabetical order to a scratch file.
   open( TEMP, '>/tmp/getatt' );
   foreach $name ( sort( keys( %pro ) ) ) { print( TEMP $pro{ $name } ); }
   close( TEMP );

#  Write the names to a log file, escaping special (to Latex) characters.
   open( NAMES, '>getatt.labels' );
   foreach $name ( sort( keys( %pro ) ) ) {
      $name =~ s/_/\\_/g;
      $name =~ s/\>/\$\>\$/g;
      $name =~ s/\</\$\<\$/g;
      print( NAMES $name );
   }
   close( NAMES );
   
#  Run PROLAT on the scratch file and then delete it.
      print( STDERR
         `rm -f /tmp/prolat.tex;
         \$SST_DIR/prolat in=/tmp/getatt atask=f single=f page=f document=f out=/tmp/prolat.tex;
         rm -f /tmp/getatt`
      );

#  Read the output from PROLAT and edit the Latex macro invocations into
#  their final form.
   open( LATEX, '/tmp/prolat.tex' );
   while( <LATEX> ) {
      if ( $unix_script ) {
         ;
      } elsif ( !$key && $C ) {
         s/\\sstinvocation{/\\sstsynopsis{/;
         s/\\sstarguments{/\\sstparameters{/;
      } elsif ( $key =~ /att/ ) {
         s/\\sstinvocation{/\\sstattributetype{/;
      } elsif ( $key =~ /class/ ) {
         s/\\sstinvocation{/\\sstconstructor{/;
      }

#  Fix up constructs that don't otherwise convert to HTML properly.
      s/{\\tt \'}/'/g;
      s/{\\tt \"}/{\\tt{\"}}/g;
      print;
   }
   close( LATEX );

# Delete the output file from PROLAT.
   print( STDERR `rm -f /tmp/prolat.tex` );