#!/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 = ".";

if (1) {
#######################################################################	
# top of html file
#######################################################################	
print "<html>\n";
print "<head>\n";
print "<title>CISM_DX Demos</title>\n";
print "</head>\n";
print "<body text=black bgcolor=\"#000000\" link=\"#0000EF\"";
print " vlink=\"#51188E\" alink=\"#FF0000\" background=black>";
print "<table BORDER COLS=2 WIDTH=\"800\" bgcolor=white>";
print "<td VALIGN=TOP WIDTH=\"800\" NOSAVE bgcolor=black>";
print "<font COLOR=\"white\">You need CISM_DX installed ";
print "to execute networks from this page. ";
print "If, after selecting \"Execute Network\"";
print " for the first time you are prompted with an \"Open With\"";
print " dialog box, select the file cismdx_opendx_webcall,\n";
print " which is located in /usr/local/CISM_DX.\n";
print " Note that all browsers may not prompt you with an \"Open With\"\n";
print " dialog box.<br>";
print "<b>Konqueror</b> right click on Execute Network line, select Open With, Other, and /usr/local/CISM_DX/cismdx_opendx_webcall.<br>";
print "<b>Other browsers</b>: You may need to define a mime type.</b>";
print "</tr></td>";
#######################################################################	
}

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

#######################################################################	
# end of html file
#######################################################################	
print "</body>\n";
print "</html>\n";
#######################################################################	


sub wanted {

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

	# 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/;

	open(DATA_in,"$network");
	
	$image =~ s/\.net/\.png/; # replace string .net with .png

	open(DATA_in,"$network");

	#################################################################### 
	# html stuff
	#####################################################################
	print "\n";
	$i = 0;

	print "<table BORDER COLS=2 WIDTH=\"1200\" bgcolor=white>\n";

	# Deal with case of .png but not _[0-9].png
	if (-e "$image") {
	    print "<td VALIGN=TOP WIDTH=\"800\" NOSAVE bgcolor=black>\n";
	    print "<IMG SRC=\"$image\" WIDTH=800>\n";
	    $i = 1;
	}

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

	while (-e "$image") {

	    if ($i == 0) {
		# Case of no .png but _[0-9].png
		print "<td VALIGN=TOP WIDTH=\"800\" NOSAVE bgcolor=black>\n";
	    } else {
		print "<IMG SRC=\"$image\" WIDTH=800>\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
	    }
	}

	if ($i == 0) {
	    # Why is 850 needed for alignment needed if there is no image?
	    print "<td VALIGN=TOP WIDTH=\"850\" NOSAVE bgcolor=black>\n";
	}

	print "<td VALIGN=TOP WIDTH=\"400\" NOSAVE>\n";
	print "<b>$network</b>\n";
	print "<p>\n";
	print "<a href=\"$BASE_URI$BASE_DIR/$network\">Execute Network</a>\n";
	print "<p>\n";
	print "<a href=\"$BASE_URI$BASE_DIR/$network?EDIT\">Edit Network</a>\n";
	print "<p>\n";
        ####################################################################

        ################################################################
	# Grab comments and annotation from network file
        ################################################################
	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 $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 $line;
		}

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

	if (1){
	print "</td>";
#	print "</tr>";
	print "\n\n";
	#print DATA_out "\n";
	#print "\n";
    }

	close(DATA_in);
    }
    ###########################################################################

}

