#!/usr/bin/perl

if ($ARGV[0] eq "-h"){
    $dir = $ARGV[1];
    $html = 1;
    shift @ARGV;
    shift @ARGV;
}

if ($html){
    opendir (D, "$dir/sources/") || die "Can not open $dir";
    while ($n = readdir (D)){
	if ($n =~ /mono-api-.*\.html$/){
	    open (IN, "$dir/sources/$n") || die "Can not open $n";
	    $files[$filecount] = $n;
	    while (<IN>){
		@files_content[$filecount] .= $_;
	    }
	    $filecount++;
	    close IN;
	}
    }
}

while (<ARGV>){
	if (/\/\*\* *\n/){
		&process_doc;
	} else {
		#print "IGNORING: $_";
	}
}

if ($html){
    for ($f = 0; $f < $filecount; $f++){
	$name = $files[$f];
	open (OUT, "> $dir/html/$name") || die "Can not create $dir/html/$name";
	print "Merging: $name\n";
	print OUT<<EOF;
<?xml version="1.0" encoding="utf-8"?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
   <title>$name</title>
</head>
<body>
EOF
	@a = split (/\n/, $files_content[$f]);

	for ($ai = 0; $ai < $#a; $ai++){
	    $line = $a[$ai];

	    ($api,$caption) = $line =~  /<h4><a name=\"api:(\w+)\">(\w+)<\/a><\/h4>/;
	    if ($api ne ""){
		$proto = $prototype{$api};
		if ($proto eq ""){
		    $proto = "Prototype: $api";
		}

print OUT<<EOF;
<blockquote>
    <a name="api:$api"></a>
                <table summary="" class="HeaderTable" width="100%" cellpadding="5">
                        <tr bgcolor="#b0c4de"><td>
			<h3 class="api">$api</h3>
			</td></tr>
		</table>

    <blockquote>
        <table summary="" class="SignatureTable" bgcolor="#c0c0c0" cellspacing="0" width="100%">
                <tr><td>
                        <table summary="" class="InnerSignatureTalbe" cellpadding="10" cellspacing="0" width="100%">
                        <tr bgcolor="#f2f2f2"><td>
			<h4>$proto</h4>
                        </td></tr>
                        </table>
	        </td></tr>
	</table>
    </blockquote>
<p>
EOF
		    if ($arguments{$api} ne ""){
                        print OUT "<h4>Parameters</h4>";
			print OUT "<blockquote><dl>";

		        print OUT "$arguments{$api}\n";
			print OUT "</dl></blockquote>";
		    }
		    if ($returns{$api} ne ""){
		        print OUT "<h4>Returns</h4>";
			print OUT "<blockquote>";
		        print OUT "$returns{$api}\n";
			print OUT "</blockquote>";
                    }
		    if ($bodies{$api} ne ""){
			print OUT "<h4>Remarks</h4>";
			print OUT "<blockquote>";
		        print OUT "$bodies{$api}\n";
			print OUT "</blockquote>";
		    }
		    print OUT "\n";
                    print OUT "</blockquote>";
	    } else {
		print OUT "$line\n";
	    }
	}
	print OUT<<EOF;
</body>
</html>
EOF
	close OUT;
	system ("mono convert.exe $dir/html/$name $dir/deploy/$name");
    }
}

sub process_doc {
	$doc = "";
	$func = <>;
	chop $func;
	$func =~ s/^ \* //;
	$func =~ s/:$//;
	print "Function: $func\n" if (!$html);
	$args = "";
	$inbody = 0;
	$returns = "";
	$body = "";
	$functions[$fn++] = $func;

	# Process arguments
	while (<>){
		if (/^ \*\*?\//){
		    $body =~ s/[@#](\w+)/<i>\1<\/i>/g;
		    $returns =~ s/[@#](\w+)/<i>\1<\/i>/g;

		    $args =~ s/@(\w+)/<i>\1<\/i>/g;
		    $bodies{$func} = $body;
		    $arguments{$func} = $args;
		    $returns{$func} = $returns;
		    $proto = "";
		    while (<>){
			$proto .= $_;
			last if (/\{/);
		    }
		    $proto =~ s/{//;
		    $prototype{$func} = $proto;
		    return;
		}
		chop;
		s/^\ \*//;
		$_ = "\n<p>" if (/^\s+$/);
				
		if ($inbody == 0){
		    if (/\s*(\w+):(.*)/){
			$args .= "<dt><i>$1:</i></dt><dd>$2</dd>";
		    } else {
			
			$body = "\t$_\n";
			$inbody = 1;
		    }
		} elsif ($inbody == 1) {
		    if (/Returns:/){
			s/Returns://;
			$returns = "\t$_\n";
			$inbody = 2;
		    } else {
			$body .= "\n\t$_";
		    }
		} else {
		    $returns .= "\n\t$_";
		}
		   
	}
}
