#!/usr/bin/perl
#
# Copyright (C) 2001-2013 Graeme Walker <graeme_walker@users.sourceforge.net>
# 
# 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 3 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, see <http://www.gnu.org/licenses/>.
# ===
#
# doxygen_fixup.pl
#
# usage: doxygen_fixup.pl [<doc-doxygen-directory>]
#
# Does fixups on the doxygen-generated html files by adding
# css styles that emailrelay-doxygen.css can operate on. 
#
# Note that the html header file speficied in doc/doxygen.cfg 
# (typically doc/doxygen-header.html) should include the 
# emailrelay-doxygen.css stylesheet after including doxygen.css.
#
# Designed for doxygen 1.6.x/1.7.x and does nothing (with an exit 
# code of 1) if there is no suitable version marker in the html files.
#
# Typically run by the emailrelay/doc makefile after it has run 
# doxygen.
#

use strict ;
use FileHandle ;

my $dir = @ARGV ? $ARGV[0] : "." ;

my $any_file_changed = 0 ;
my $marker = 'edited by doxygen_fixup.pl' ;
my $comment = '<!-- '.$marker.' -->' ;
for my $f ( <$dir/*> )
{
	if( $f =~ m/\.html$/ )
	{
		my $input = new FileHandle( $f , "r" ) or die "cannot open $f" ;
		my $output = new FileHandle( "$f.tmp" , "w" ) or die "cannot create $f.tmp" ;
		my $seen_marker = undef ;
		my $file_changed = undef ;
		while ( <$input> )
		{
			chomp( my $line = $_ ) ;
			$seen_marker ||= ( $line =~ m/Generated by Doxygen 1\.6/ ) ;
			$seen_marker ||= ( $line =~ m/Generated by Doxygen 1\.7/ ) ;
			my $old_line = $line ;
			$line =~ s/p>References/p class="references">References/g ;
			$line =~ s/p>Referenced by/p class="referencedby">Referenced by/g ;
			$line =~ s/p>Definition at/p class="definitionat">Definition at/g ;
			$line =~ s/p>Implements /p class="implements">Implements /g ;
			if( $line =~ m/<td class="memname".* <\/td> *$/ )
			{
				$line =~ s/>/> /g ;
				$line =~ s/</ </g ;
				my @word = split( ' ' , $line ) ;
				if( scalar(@word) > 2 )
				{
					my $n = scalar(@word) - 2 ;
					if( $n =~ m/^[A-Za-z0-9&;:_]+$/ )
					{
						$word[$n] = '<span class="realmemname">' . $word[$n] . "</span>" ;
						$line = join( " " , @word ) ;
					}
				}
			}
			my $line_changed = ( $line ne $old_line ) ;
			my $line_has_comment = ( $line =~ m/$marker/ ) ;
			print $output $line , ( ( $line_changed && !$line_has_comment ) ? "" : $comment ) , "\n" ;
			$file_changed ||= $line_changed ;
		}
		$output->close() or die "cannot write $f.tmp" ;
		$input->close() ;
		if( $file_changed && $seen_marker )
		{
			rename( "$f.tmp" , $f ) or die "cannot rename $f.tmp to $f" ;
			$any_file_changed = 1 ;
		}
		else
		{
			unlink( "$f.tmp" ) ;
		}
	}
}
exit( $any_file_changed ? 0 : 1 ) ;
