#!/usr/bin/perl

###############################################################################
#  
#  Usage: generate_html.pl
#
#  Recursively searches for any *.png files and uses text in corresponding 
#  *.net file to create html documentation. 
#
#  R.S. Weigel 02/17/2005.
###############################################################################

use File::Find;
use Cwd;

$num  = scalar(@ARGV);
$dir1 = ".";

$js = "demoFramelessNodes.js";
open(DATA_js,">>$js");
@directory = ($dir1);
open(DATA_out,">index.txt");
find(\&wanted, @directory);
close(DATA_out);
close(DATA_js);


sub wanted {


    ##########################################################################
    # If a file with a .net extension is found
    ##########################################################################
    if ( ($_ =~ m/\.net/) && ($_ !~ m/\.svn/) ) {
	
	$network = $_;
	$image = $_;
	$html = $_;

	# If in macros directory, don't generate html if "Demo" is
	# not in name.
	$curr_dir = getcwd();
	if ($curr_dir =~ m/macros/) {
	    if ($_ !~ m/Demo/) {
		return;
	    }
	}

	$BASE_URI = "cismdx://";
	$BASE_DIR = $curr_dir;
	$BASE_DIR =~ s/.*(OpenDX.*)/$1/;
	$BASE_DIR =~ s/.*(modules.*)/OpenDX\/$1/;
	$image =~ s/\.net/\.png/; # replace string .net with .png
	$html =~ s/\.net/\.htm/; # replace string .net with .htm

	open(DATA_in,"$network");
	open(DATA_html,">$html");

	#################################################################### 
	# html stuff
	#####################################################################
	$i = 0;

	print(DATA_js "aux1 = insFld(foldersTree, gFld(\"$network\",  \"demoFrameless.html?pic=%22$html%22\"))\n");

	# Deal with case of .png but not _[0-9].png
	if (-e "$image") {
	    print(DATA_js "insDoc(aux1, gLnk(\"S\", \"$image\", \"demoFrameless.html?pic=%22$image%22\"))\n");
	    $i = 1;
	}

	$image =~ s/\.png/_1\.png/;

	while (-e "$image") {

	    if ($i == 0) {
		# Case of no .png but _[0-9].png
		print(DATA_js "insDoc(aux1, gLnk(\"S\", \"$image\", \"demoFrameless.html?pic=%22$image%22\"))\n");
	    } else {
		print(DATA_js "insDoc(aux1, gLnk(\"S\", \"$image\", \"demoFrameless.html?pic=%22$image%22\"))\n");
	    }

	    $i = $i + 1;
	    $image =~ s/(.*)_[0-9].png/$1_$i\.png/;
	    if ($i == 10) {
		print "Warning from generate_html.pl: ";
		print "Only 9 images are used but more than 9 found.\n";
		last; # exit loop
	    }
	}

        ####################################################################

        ################################################################
	# Grab comments and annotation from network file
        ################################################################
	print(DATA_html "<a href=\"$BASE_URI$BASE_DIR/$network\">Execute Network</a>\n");
	print(DATA_html " | \n");
	print(DATA_html "<a href=\"$BASE_URI$BASE_DIR/$network?EDIT\">Edit Network</a> ");
	while ($line = <DATA_in>){
	    # Assumes string _Author_ is on the first line of the README page
	    if ($line =~ m/annotation.*Author/) {

		# Remove leading .net markup text
		$line =~ s/.*\/\/ annotation user: (.*)$/<br>$1/;

		# Color key words
		$line =~ s/Author\:/<font color \= \"red\">Author\:<\/font>/; 
		print(DATA_html $line);
#		$line =~ s/.*\/\/ comment: (.*)$/<br>$1/;
		while ( ($line = <DATA_in>) && ($line !~ m/user_end/) ){
		    # Remove string <NULL> 
		    $line =~ s/<NULL>/<p>/; 

		    $line =~ s/.*\/\/ annotation user: (.*)$/$1/;

		    # Color key words
		    $line =~ 
			s/Category\:/<font color \= \"red\">Category\:<\/font>/; 
		    $line =~ 
			s/Date\:/<font color \= \"red\">Date\:<\/font>/; 

		    $line =~ 
			s/Modified\:/<font color \= \"red\">Modified\:<\/font>/; 

		    $line =~ 
			s/Function\:/<font color \= \"red\">Function\:<\/font>/; 
		    $line =~
			s/To Do\:/<font color \= \"red\">To Do\:<\/font>/i; 
		    $line =~ 
			s/See also\:/<font color\=\"red\">See also\:<\/font>/i; 

		    print(DATA_html $line);
		}

	    }		    
	}
        ################################################################

	print(DATA_html $line);
	close(DATA_in);
	close(DATA_html);
    }
    ###########################################################################

}

