#!/usr/local/bin/perl5
#
#  Copyright (C) 2006
#  ASTRON (Netherlands Foundation for Research in Astronomy)
#  P.O.Box 2, 7990 AA Dwingeloo, The Netherlands, diepen@astron.nl
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#  $Id: doxygen_pp,v 19.5 2006/12/22 05:36:55 gvandiep Exp $


#  Convert multiple C++ "slash-slash" style comment lines
#  into one spanning C-style comment (/* ... */), ignoring
#  lines that start with "slash-slash-hash style comments.
#  Lines starting with //# are blanked except for the ones at the beginning
#  showing the copyright.
#
#  The cxx2html tags are converted to doxygen or html tags.
#  Code from cxx2html (Html.pm) has been copied for this purpose.
#  When converting, the line numbers should stay the same, so doxygen
#  errors/warnings refer to the correct line. Also the line numbers and
#  source file shown by doxygen will be correct.


$NON_COMMENT_BLOCK = 0;
$COMMENT_BLOCK     = 1;

# If the caller supplied precisely one command line argument, then this script
# can proceed.
###  open OUT, ">> xyz";

$arguments = @ARGV;
if ($arguments == 1)
{
  # The argument represents the name of the file to process.
  $filename = $ARGV[0];

  # Get current directory.
  $cwd = `pwd`;
  chop($cwd);
  $cwd =~ s|/$||;
  # Append file name to it (which may contain a path as well).
  $fullnm = $filename;
  if ($filename !~ m|^/|) {
    $fullnm = $cwd . "/" . $filename;
  }
  $filnm = $fullnm;
  # Get the name of the file from code on.
  $filnm =~ s|.*/code/||;
  # Get the name of the header.
  $hdrnm = $filnm;
  $hdrnm =~ s|.*/||;     # remove directory
  $hdrnm =~ s|\..*||;    # remove extension
  # Get the name of the package and module.
  $isPkgHdr = 0;
  $isModHdr = 0;
  $modnm = "";
  $pkgnm = $filnm;
  $pkgnm =~ s|/implement/.*||;
  if ($pkgnm eq $filnm) {
    # Package header; no module name.
    $pkgnm =~ s|/.*||;
    $isPkgHdr = 1;
  } else {
    $modna = $filnm;
    $modna =~ s|.*/implement/||;
    $modnm = $modna;
    $modnm =~ s|(.*?)/.*|$1|;
    if ($modnm eq $modna) {
      # We have a .h file outside a module.
      $modnm =~ s|(.*?)\..*|$1|;
      # It is a module header if a directory with the same name exists.
      $tmpnm = $fullnm;
      $tmpnm =~ s|\.[^/]*||;
      if (-d $tmpnm) {
	$isModHdr = 1;
      }
    }
  }
###  print OUT "$fullnm $filnm $hdrnm $modnm $pkgnm $isModHdr $isPkgHdr\n";

  # Open the file.
  $status = open(FILEHANDLE, $filename);

  # If the file could be opened then read the contents.
  if ($status)
  {
    # Read the contents of the file.
    @text = <FILEHANDLE>;

    # Close the file.
    close(FILEHANDLE);

    # Process the contents of the file.
    change_comment_style(@text);
  }

  # Otherwise report an error condition.
  else
  {
    print "ERROR: file \"$filename\" could not be opened.\n";
  }
}

# Otherwise, display the proper invocation for this script.
else
{
  print "usage: $0 filename\n";
}


# Handle changing a linkto tag.
# Replace linkto by the class linked to; remove possible ':description'
# Similarly replace module link; link ':classes' to the same.
# The closing </linkto> has already been removed.
sub handleLinkto
{
  my $linkstr = shift(@_);
  $linkstr =~ s|<linkto\s+class\s*=\s*['"]?(.*?)(:description)?['"]?\s*>| \\link $1 |i;
  $linkstr =~ s|<linkto\s+module\s*=\s*['"]?(.*?):classes['"]?\s*>| \\link $1 |i;
  $linkstr =~ s|<linkto\s+module\s*=\s*['"]?(.*?)['"]?\s*>| \\link $1 |i;
  if ($linkstr =~ m|<linkto\s+group\s*=\s*['"]?(.*?)['"]?\s*>|) {
    my $linkgrp = $1;
    $linkstr =~ s|<linkto.*?>|<a href="$linkgrp">|i;
    $linkstr .= "</a>";
  } else {
    $linkstr .= " \\endlink";
  }
  # For the time being cannot really handle other linkto commands.
  $linkstr =~ s|<linkto\s+(.*?)>(.*?)\\endlink|(see $2 ($1))|;
  return $linkstr;
}

# Handle changing a reviewed tag.
sub handleReviewed
{
  # Get argument and remove tags delimiters.
  my $attr = shift(@_);
  $attr =~ s|^\s*<\s*reviewed\s*||;
  $attr =~ s|\s*>\s*$||;
  # Organize review info nicely.
  @attr = split(/(?:=["'][^'"]*['"]|=[^\s>]*)\s*/,$attr);
  $attr =~ s/.*?=//;
  my @val = split(/\s*\w+=/,$attr);
  my %attr = ();
  my $attrcnt = 0;
  while ( ($_ = shift(@attr)) && ($val = shift(@val)) ) {
    $val =~ s/^["']\s*//;
    $val =~ s/\s*["']$//;
    $val =~ s/@/\\@/g;       # doxygen treats @ as special, so escape
    $val =~ s/\.cc//g;       # doxygen cannot deal with . in <dd>
    if ( $val ) {
      $attr{$_} = $val;
      $attrcnt++;
    }
  }
  my $str = "";
  if ( $attrcnt ) {
    $str = "<h3>Review Status</h3><dl>";
    $str .= "<dt>Reviewed By:<dd>" . $attr{'reviewer'}
      if defined $attr{'reviewer'};
    $str .= "<dt>Date Reviewed:<dd>" . $attr{'date'}
      if defined $attr{'date'};
    $str .= "<dt>Test programs:<dd>" . $attr{'tests'}
      if defined $attr{'tests'};
    $str .= "<dt>Demo programs:<dd>" . $attr{'demos'}
      if defined $attr{'demos'};
    $str .= "</dl>";
  }
  return $str;
}


# Loop through all lines and change comments as needed.
sub change_comment_style
{
  $keephash = 1;
  $state = $NON_COMMENT_BLOCK;
  $newstate = $NON_COMMENT_BLOCK;
  @spanning_comment = ();
  $indent = "";
  $comment = "";
  $extraline = "";
  $grouplevel = 0;
  $namedgrouplevel = 0;
  $code = 0;           # no <src> block found
  $link = 0;           # no <linkto> block found
  $linkstr = "";
  $review = 0;         # no <reviewed> block found
  $reviewstr = "";
  $nadded = 0;         # nr of added lines for which blank lines are removed
  $summinx = -1;       # index of summary line in spanning_comment

  foreach $line (@_)
  {
    $skip = 0;
    $newstate = $NON_COMMENT_BLOCK;
    # slash-slash-hash is blanked if not at beginning of file.
    if ($keephash == 0  &&  $line =~ m|^\s*//\#|)
    {
      $line = "\n";
    }
    $comment = $line;
    # If this line contains only a slash-slash comment, then
    # the comment block might need to be converted.
    if ($line =~ m|(^\s*)(//)(.*)|)
    {
      # Keep track of how far the comment block is indented.
      $indent = $1;
      $comment = $3;
      # slash-slash-slash/hash are kept as such (is doxygen already).
      # Four slashes is comment in code examples, thus is normal comment.
      if ($comment !~ m|^[/#][^/]|  &&  $comment !~ m|^[/#]$|)
      {
        $keephash = 0;
	$newstate = $COMMENT_BLOCK;

	# Doxygen treats #name as a request to link to name.
	# So escape the #.
	$comment =~ s|([ (])#([a-z]+)|$1\\#$2|ig;

	# A module header is turned into a defgroup and added to the package.
	if ($isModHdr == 1) {
	  if ($comment =~ m|^\s*<module>|i) {
	    $comment =~ s|<module>|\\ingroup $pkgnm\n\\defgroup $modnm $modnm|i;
	    $nadded += 2;
	  }
	  $comment =~ s|</module>|<a class=anchor name=${modnm}_Class_List></a>*/\n/** \\ingroup $modnm\n\\defgroup ${modnm}_internal_classes ${modnm}_internal_classes\n\\brief Internal $modnm classes and functions|i;
	  $comment =~ s|^\s*<summary>| \\brief |ig;
	  $comment =~ s|</summary>|<p>See <a href=#${modnm}_Class_List>below</a> for an overview of the classes in this module.|i;
	} else {
	  # A class or group header's summary is added to the module's defgroup.
	  if ($comment =~ m|^\s*<summary>|i) {
	    $comment =~ s|<summary>|\\ingroup $modnm \n\\brief |i;
	    $nadded += 1;
	    $summinx = @spanning_comment + 1;   # index of ingroup line
	  }
	  $comment =~ s|</summary>||i;
	}

	# Handle group tags (also the old fashioned +grp/-grp).
	# Turn a named group with a summary into a struct, so doxygen
	# puts it in the module header at the right place.
	# Keep track of the group level, so such a named group is ended with };.
	# Named groups are always preceeded by an anchor for linkto references.
	$comment =~ s|^\s*\+grp|<group>|;
	$comment =~ s|^\s*-grp|</group>|;
	if ($comment =~ m|<group\s+name\s*=\s*(.*?)\s*>|i) {
	  $grpnm = $1;
	  $comment = "$indent<a class=anchor name=$grpnm></a>";
	  # Only a named group at the highest level is turned into a struct.
	  # Do this only if it has a summary, which indicates it describes
	  # a group of functions outside a class.
	  if ($grouplevel == 0  &&  $summinx >= 0) {
	    $namedgrouplevel += 1;
	    $grpnm =~ s|\s*['".]\s*||g;
	    $grpnm =~ s|\s+|_|g;
	    $extraline = "struct ${hdrnm}_global_functions_${grpnm} {\n";
	    $nadded += 1;
	  } else {
	    # Otherwise open a doxygen group.
	    $extraline = "$indent//@\{\n";
	    $nadded += 1;
	  }
	  $grouplevel += 1;
        } elsif ($comment =~ m|<group>|) {
##	  $comment =~ s|<group>| \\name='' @\{|i;
	  $comment =~ s|<group>|@\{|i;
	  $grouplevel += 1;
        } elsif ($comment =~ m|</group>|) {
	  if ($namedgrouplevel == $grouplevel) {
	    $line = "\};\n";
	    $newstate = $NON_COMMENT_BLOCK;
	    $namedgrouplevel -= 1;
	  } else {
	    $comment =~ s|</group>|@\}|i;
	  }
	  $grouplevel -= 1;
	}

	# Handle linkto tags. There can be multiple of them and they can be
	# spread over multiple lines.
	if ( $link==1 ) {
	  if ( $comment =~ m|(.*?)</linkto>(.*)|i ) {
	    my $b = $2;
	    $linkstr = handleLinkto ($linkstr . " $1");
	    $comment = "$linkstr $b";
	    $link = 0;
	  } else {
	    $linkstr .= " $comment";
	    $comment = "";
	  }
	}
	while ( $link==0  &&  $comment =~ m|<linkto|i ) {
	  if ( $comment =~ m|(.*?)<linkto(.*?)</linkto>(.*)|i ) {
	    my $a = $1;
	    my $c = $3;
	    my $linkstr = handleLinkto ("<linkto $2");
	    $comment = "$a $linkstr $c";
	  } elsif ( $comment =~ m|(.*?)<linkto(.*)|i ) {
	    $link = 1;
	    $linkstr = "<linkto $2";
	    $comment = $1;
	  }
	}

	# Convert the section tags to h3.
	$comment =~ s|<srcblock>| \\code |ig;
	$comment =~ s|</srcblock>| \\endcode |ig;
	$comment =~ s|<prerequisite>|<h3>Prerequisite</h3><ul>|ig;
	$comment =~ s|</prerequisite>|</ul>|ig;
	$comment =~ s|<motivation>|<h3>Motivation</h3>|ig;
	$comment =~ s|</motivation>||ig;
	$comment =~ s|<synopsis>|<h3>Synopsis</h3>|ig;
	$comment =~ s|</synopsis>||ig;
	$comment =~ s|<example>|<h3>Example</h3>|ig;
	$comment =~ s|</example>||ig;
	$comment =~ s|<etymology>|<h3>Etymology</h3>|ig;
	$comment =~ s|</etymology>||ig;
	$comment =~ s|<motivation>|<h3>Motivation</h3>|ig;
	$comment =~ s|</motivation>||ig;
	$comment =~ s|<thrown>|<h3>Thrown Exceptions</h3><ul>|ig;
	$comment =~ s|</thrown>|</ul>|ig;
	# Remove obsolete category tag (used in e.g. Map.h>).
	$comment =~ s|<category.*?>||i;

	# Use current date in todo if needed.
	if ( $comment =~ /<todo\s*(?:asof\s*=\s*["']?(.*?)['"]?.*?)?>/ )
	{
	  my $date = $1;
	  if ( $date ) {
	    $comment =~ s|<todo.*?>|<h3>To Do ($date)</h3><ul>|ig;
	  } else {
	    $comment =~ s|<todo.*?>|<h3>To Do</h3><ul>|ig;
	  }
	}
	$comment =~ s|</todo>|</ul>|ig;

	# Fill in templating tags.
	# Use % to prevent doxygen from making a link to class Template.
	if ( $comment =~ /<templating\s*(?:arg\s*=\s*(\w+).*?)?>/ ||
	     $comment =~ /<templating\s*(?:arg\s*=\s*["']([^'"]+)['"].*?)?>/ )
	{
	  my $arg = $1;
	  if ( $arg ) {
	    $comment =~ s|<templating.*?>|<h3>%Template Type Argument Requirements ($arg)</h3><ul>|ig;
	  } else {
	    $comment =~ s|<templating.*?>|<h3>%Template Type Argument Requirements</h3><ul>|ig;
	  }
	}
	$comment =~ s|</templating>|</ul>|ig;

	# Fill in the visibility.
	# Use that in the ingroup used in the summary.
	if ( $comment =~ /<use\s+visibility\s*=\s*(.*?)\s*>/ )
	{
	  my $arg = $1;
	  if ( $arg =~ /export/i ) {
	    $comment =~ s|<use.*?>|<h3>Intended use:</h3> Part of API|ig;
	  } elsif ( $arg =~ /local/i ) {
	    $comment =~ s|<use.*?>|<h3>Intended use:</h3> Internal|ig;
	    # Change group in summary.
	    if ($summinx > 0) {
	      $modnmi = $modnm . "_internal_classes";
	      $last = @spanning_comment[$suminx];
	      $last =~ s|ingroup $modnm|ingroup $modnmi|;
	      @spanning_comment[$suminx] = $last;
	    }
	  } else {
	    $comment =~ s|<use.*?>|<h3>Intended use:</h3> $1|ig;
	  }
	}
	$comment =~ s|</use>||ig;

	# Organize review info nicely.
	if ( $review==1 ) {
	  if ( $comment =~ m|(.*?)>(.*)|i ) {
	    my $b = $2;
	    $reviewstr = handleReviewed ($reviewstr . " $1");
	    $comment = "$reviewstr $b";
	    $review = 0;
	  } else {
	    $reviewstr .= " $comment";
	    $comment = "";
	  }
	}
	while ( $review==0  &&  $comment =~ m|<reviewed|i ) {
	  if ( $comment =~ m|(.*?)<reviewed(.*?)>(.*)|i ) {
	    my $a = $1;
	    my $c = $3;
	    my $reviewstr = handleReviewed ("<reviewed $2");
	    $comment = "$a $reviewstr $c";
	  } elsif ( $comment =~ m|(.*?)<reviewed(.*)|i ) {
	    $review = 1;
	    $reviewstr = "<reviewed $2";
	    $comment = $1;
	  }
	}
	$comment =~ s|</reviewed>||i;

	# Turn note to <em>.
	# Capitalize the type.
	my $outtype = "Note";
	if ( $comment =~ /<note\s+role\s*=\s*['"]?(tip|caution|warning)["']?.*?>/i )
	{
	  my $type = lc $1;
	  $outtype = ucfirst $type;
	}
	$comment =~ s|<note.*?>|<strong>$outtype:</strong><em> |i;
	$comment =~ s|</note>|</em>|i;

	# Replace <src>. Inside the block the special characters have to
	# be replaced. Note that multiple <src> can be on a line, but it
	# is also possible that the closing </src> is on another line.
	if ( $code==1 ) {
	  if ( $comment =~ m|(.*?)</src>(.*)|i ) {
	    my $a = $1;
	    my $b = $2;
	    $a =~ s/&/&amp;/g;
	    $a =~ s/"/&quot;/g;
	    $a =~ s/</&lt;/g;
	    $a =~ s/>/&gt;/g;
	    $comment = "$a</tt>$b";
	    $code = 0;
	  } else {
	    $comment =~ s/&/&amp;/g;
	    $comment =~ s/"/&quot;/g;
	    $comment =~ s/</&lt;/g;
	    $comment =~ s/>/&gt;/g;
	  }
	}
	while ( $code==0 && $comment =~ m|<src>|i ) {
	  if ( $comment =~ m|(.*?)<src>(.*?)</src>(.*)|i ) {
	    my $a = $1;
	    my $b = $2;
	    my $c = $3;
	    $b =~ s/&/&amp;/g;
	    $b =~ s/"/&quot;/g;
	    $b =~ s/</&lt;/g;
	    $b =~ s/>/&gt;/g;
	    $comment = "$a<tt>$b</tt>$c";
	  } elsif ( $comment =~ m|(.*?)<src>(.*)|i ) {
	    my $a = $1;
	    my $b = $2;
	    $b =~ s/&/&amp;/g;
	    $b =~ s/"/&quot;/g;
	    $b =~ s/</&lt;/g;
	    $b =~ s/>/&gt;/g;
	    $comment = "$a<tt>$b";
	    $code = 1;
	  }
	}
	# Remove this line because doxygen cannot handle a line in a <ul>
	# before an <li>.
	$comment =~ s|You should have at least a preliminary understanding of these classes:||;
	# Doxygen treats .. as end of list. So escape second one.
	$comment =~ s|\.\.|.\\.|g;
	# Skip an empty line if needed.
	if ($nadded > 0) {
	  if ($comment =~ /^\s*$/) {
	    $nadded -= 1;
	    $skip = 1;
	  }
	}
      }
      # doxygen group tags need slashes.
      if ($comment =~ /^\s*@[\{\}]\s*$/) {
        $comment =~ s/\s+//g;
        $line = "$indent//$comment\n";
        $newstate = $NON_COMMENT_BLOCK;
      }
    }
    # A blank line does not change anything.
    elsif ($line =~ /^\s*$/) {
      $newstate = $state;
      if ($nadded > 0) {
	$nadded -= 1;
	$skip = 1;
      }
      $comment = "";
    } else {
      $keephash = 0;
    }

    # Act depending on new and old state.
    if ($skip == 0) {
      if ($newstate == $COMMENT_BLOCK) {
        if ($state == $COMMENT_BLOCK) {
	  # Add comment to block.
	  push @spanning_comment, "\n$indent   $comment";
        } else {
	  # Begin the spanning comment block with given indentation.
	  push @spanning_comment, "$indent/**$comment";
        }
	if ($extraline ne "") {
	  $line = $extraline;
	  $extraline = "";
	  $state = $newstate;
	  $newstate = $NON_COMMENT_BLOCK;
	}
      }
      if ($newstate == $NON_COMMENT_BLOCK) {
        if ($state == $COMMENT_BLOCK) {
	  # End comment block and write the block.
	  push @spanning_comment, " */\n";
	  print @spanning_comment;
	  @spanning_comment = ();
	  $summinx = -1;
        }
        # Write the line.
        print $line;
	if ($extraline ne "") {
	  print $extraline;
	  $extraline = "";
	}
      }
      $state = $newstate;
    }
  }

  # Output remaining comments if there.
  if ($state == $COMMENT_BLOCK)
  {
    push @spanning_comment, " */\n";
    print @spanning_comment;
  }
}
